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(); } }); 
  • Uso de fragmento anidado y objeto animador al lado
  • La animación de Android reduce el tartamudeo
  • Animación de texto en lienzo - Android
  • Cargar dinámicamente un AnimationDrawable en un TextView
  • Detección de la dirección de desplazamiento en el adaptador (arriba / abajo)
  • Implementación del efecto KenBurns en Android con el ajuste dinámico de mapas de bits
  • Cómo hacer un gesto de deslizamiento en el elemento RecyclerView sin librería de terceros
  • Velocímetro Android (indicador de aguja)
  • Animar elementos ListView en notifyDataSetChanged ()
  • Dejar una animación de Android en su estado final
  • Android: Animación e InterpolatedTime
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.