Android: Cómo obtener el marco actual en AnimationDrawable

Necesito ayuda con Android. Estoy tratando de crear un juego de máquinas tragamonedas. Por lo tanto, estoy usando el AnimationDrawable para las tres ruedas. Aquí está el archivo 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> 

Puse la lista de animación en 3 vistas diferentes (los tres whells) en el diseño principal usando el atributo android: background

 android:background="@drawable/animwheel" 

Ahora, el usuario puede detener las animaciones haciendo clic en tres botones diferentes. El problema es cómo puedo saber cuál es la imagen que la vista está mostrando actualmente? ¿Hay algún método getCurrentFrame () o algo así?

¡Gracias de antemano!

Tengo el mismo problema, he probado todas las funciones y no tengo éxito porque las funciones como 'getCurrentFrame ()' devuelven un valor hexadecimal que cambia cada vez que ejecutas tu aplicación, así que hice algo que resuelve mi problema y espero que resuelve el suyo:

En lugar de intentar obtener el nombre de la imagen del marco ('fruits1'), tienes que obtener la posición del marco, supone que el usuario stoped animación en el marco 'fruits2', por lo que el número del marco es 1, porque 0 es el marco 'fruits1'. ¿Cómo lo harás?

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

Cuando se detiene la animación hacer algo como esto:

 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; } } 

Por lo tanto, la variable 'frameNumber' guardará el número del marco, en su caso, 0, 1 o 2. Si tiene muchos fotogramas, podría hacer una función que devuelva el nombre ('fruits1', 'fruits2 '…) basado en el número de la trama, utilizando una matriz.

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.