Android:如何获取AnimationDrawable中的当前帧

发布于 2025-01-07 16:44:59 字数 797 浏览 0 评论 0原文

我需要有关 Android 的帮助。 我正在尝试创建一个老虎机游戏。因此,我将 AnimationDrawable 用于三个轮子。这是 animwheel.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/animWheel"
android:oneshot="false" >

<item
    android:drawable="@drawable/fruits1"
    android:duration="200"/>
<item
    android:drawable="@drawable/fruits2"
    android:duration="200"/>
<item
    android:drawable="@drawable/fruits3"
    android:duration="200"/>
</animation-list>

我使用属性 android:background 将动画列表放在主布局中的 3 个不同视图(三个轮子)中

android:background="@drawable/animwheel"

现在,用户可以通过单击三个不同的按钮来停止动画。问题是我如何知道视图当前显示的是哪张图片? 是否有 getCurrentFrame() 方法或类似的方法?

先感谢您!

I need help with Android.
I'm trying to create a slot machine game. So, I'm using the AnimationDrawable for the three wheels. Here is the animwheel.xml file:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/animWheel"
android:oneshot="false" >

<item
    android:drawable="@drawable/fruits1"
    android:duration="200"/>
<item
    android:drawable="@drawable/fruits2"
    android:duration="200"/>
<item
    android:drawable="@drawable/fruits3"
    android:duration="200"/>
</animation-list>

I putted the animation-list in 3 different views (the three whells) in the main layout using the attribute android:background

android:background="@drawable/animwheel"

Now, the user can stop the animations by clicking on three different buttons. The problem is how can I know which is the picture that the view is displaying currently?
Is there any getCurrentFrame() method or something like that?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2025-01-14 16:44:59

我有同样的问题,尝试了每个函数,但没有成功,因为像“getCurrentFrame()”这样的函数返回一个十六进制值,每次运行应用程序时该值都会改变,所以,我做了一些事情来解决我的问题,并希望解决你的问题:

您不必尝试获取帧图像的名称(“fruits1”),而是必须获取帧的位置,假设用户在帧“fruits2”中停止动画,因此,帧的编号是 1,因为 0 是帧“fruits1”。你会怎么做?

// Create the animation
myImage.setBackgroundResource(R.anim.myanimation);
myAnimation = (AnimationDrawable) myImage.getBackground();

当您停止动画时,您会执行以下操作:

myAnimation.stop();

// The variable that will guard the frame number
int frameNumber;

// Get the frame of the animation
Drawable currentFrame, checkFrame;
currentFrame = myAnimation.getCurrent();

// Checks the position of the frame
for (int i = 0; i < myAnimation.getNumberOfFrames(); i++) {
    checkFrame = myAnimation.getFrame(i);
    if (checkFrame == currentFrame) {
        frameNumber = i;
        break;
    }
}

因此,变量“frameNumber”将保护帧的编号,在您的情况下为 0、1 或 2。
如果有很多帧,您可以执行一个函数,使用数组根据帧的编号返回名称(“fruits1”、“fruits2”...)。

I have the same problem, tried every function, and don't have success because the functions like 'getCurrentFrame()' returns a hexadecimal value that changes every time you run your application, so, I did something that resolve my problem, and hope that resolves yours:

Instead of trying to get the name of image of the frame ('fruits1'), you have to get the position of the frame, supose that user stoped animation in frame 'fruits2', so, the number of the frame is 1, because 0 is the frame 'fruits1'. How will you do that?

// Create the animation
myImage.setBackgroundResource(R.anim.myanimation);
myAnimation = (AnimationDrawable) myImage.getBackground();

When you stops the animation you do something like this:

myAnimation.stop();

// The variable that will guard the frame number
int frameNumber;

// Get the frame of the animation
Drawable currentFrame, checkFrame;
currentFrame = myAnimation.getCurrent();

// Checks the position of the frame
for (int i = 0; i < myAnimation.getNumberOfFrames(); i++) {
    checkFrame = myAnimation.getFrame(i);
    if (checkFrame == currentFrame) {
        frameNumber = i;
        break;
    }
}

So, the variable 'frameNumber' will guard the number of the frame, in your case, 0, 1, or 2.
If will have a lot of frames, you could do a function that return the name ('fruits1', 'fruits2' ...) based on the number of the frame, using an array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文