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

Dan*_*ali 3 android frame

我需要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>
Run Code Online (Sandbox Code Playgroud)

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

android:background="@drawable/animwheel"
Run Code Online (Sandbox Code Playgroud)

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

先感谢您!

小智 13

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

你不必试图得到帧的图像名称('fruits1'),而是必须得到帧的位置,将用户停止的动画置于帧'fruits2'中,因此,帧的数量是1,因为0是帧'fruits1'.你会怎么做?

// Create the animation
myImage.setBackgroundResource(R.anim.myanimation);
myAnimation = (AnimationDrawable) myImage.getBackground();
Run Code Online (Sandbox Code Playgroud)

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,变量'frameNumber'将保护帧的数量,在你的情况下,0,1或2.如果有很多帧,你可以做一个返回名称的函数('fruits1','fruits2 '...)基于帧的数量,使用数组.