Inicio de la animación marco por cuadro

Tengo una pregunta básica sobre cómo iniciar una animación marco por cuadro.

Cuando llamo el método AnimationDrawable.start () desde mi código directamente, no parece funcionar.

public void onCreate(Bundle savedInstanceState) { ... mAnimation.start(); ... } 

Pero si pongo esta línea en el método de devolución de llamada onClick () de un botón, al presionar el botón se inicia la animación.

¿Por qué esta línea no funciona en el código?

¡Gracias!

Código:

 public class MyAnimation extends Activity { @Override public void onCreate(Bundle savedInstanceState) { AnimationDrawable mframeAnimation = null; super.onCreate(savedInstanceState); setContentView(R.layout.my_animation); ImageView img = (ImageView) findViewById(R.id.imgMain); BitmapDrawable frame1 = (BitmapDrawable) getResources().getDrawable( R.drawable.splash1); BitmapDrawable frame2 = (BitmapDrawable) getResources().getDrawable( R.drawable.splash2); int reasonableDuration = 250; mframeAnimation = new AnimationDrawable(); mframeAnimation.setOneShot(false); mframeAnimation.addFrame(frame1, reasonableDuration); mframeAnimation.addFrame(frame2, reasonableDuration); img.setBackgroundDrawable(mframeAnimation); mframeAnimation.setVisible(true, true); //If this line is inside onClick(...) method of a button, animation works!! mframeAnimation.start(); } 

}

Es importante tener en cuenta que el método start () llamado en AnimationDrawable no se puede llamar durante el método onCreate () de su Actividad, ya que AnimationDrawable aún no está completamente conectado a la ventana. Si desea reproducir la animación de inmediato, sin requerir interacción, es posible que desee llamarla desde el método onWindowFocusChanged () de su Actividad, que se llamará cuando Android enfoque su ventana. Muy al final de la página http://developer.android.com/guide/topics/graphics/2d-graphics.html

  ImageView img = (ImageView)findViewById(R.id.some layout); AnimationDrawable frameAnimation = (AnimationDrawable)img.getDrawable(); frameAnimation.setCallback(img); frameAnimation.setVisible(true, true); frameAnimation.start(); 

Y para agregar animación puedes hacer algo como

 <animation-list android:id="@+id/my_animation" android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/frame1" android:duration="150" /> <item android:drawable="@drawable/frame2" android:duration="150" /> </animation-list> 

Utilice un Runnable para insertar el mensaje start () en la cola de mensajes, simplemente agregue este LOC para reemplazar su mFrameAnimation.start ();

 img.post(new Starter()); 

Clase interna del ayudante:

 class Starter implements Runnable { public void run() { mFrameAnimation.start(); } } 

Para jugar una animación sólo en onCreate (…) add:

 ImageView mImageView=(ImageView) findViewById(R.id.image); mImageView.setBackgroundResource(R.anim.film); mFrameAnimation = (AnimationDrawable) mImageView.getBackground(); mImageView.post(new Runnable(){ public void run(){ mFrameAnimation.start(); } }); 
  • Cómo animar el cambio de un gestor de diseño de la vista de reciclador
  • Cómo hacer la animación 2D en Unity
  • ¿Cómo puedo programar una animación circular rotatoria como esta (foto adjunta)? Androide
  • Actualizar la animación del elemento de menú en ActionBarSherlock
  • Android translate ImageView sobre SurfaceView
  • Android - ImageView bottomCrop en lugar de centerCrop
  • ProgressDialog Actividad de fondo Fade Out Animation
  • ¿Cómo llevar la vista al frente de todo?
  • Implemente la animación de la tarjeta entre dos vistas usando AnimatorSet
  • Mostrar y ocultar una vista con una animación de diapositiva arriba / abajo
  • Animar las dimensiones superior e inferior de una vista
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.