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

时间:2012-02-22 15:53:47

标签: 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>

我使用android:background

属性在主布局中将动画列表放在3个不同视图(三个轮廓)中
android:background="@drawable/animwheel"

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

提前谢谢!

1 个答案:

答案 0 :(得分:13)

我有同样的问题,尝试过每个函数,并且没有成功,因为像'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'...)。