gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:18 25 4
gpt4 key购买 nike

我需要 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 个不同 View (三个轮子)中

android:background="@drawable/animwheel"

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

提前致谢!

最佳答案

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

关于Android:如何在 AnimationDrawable 中获取当前帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9398206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com