studio que programacion movimiento interpolacion fotograma forma clave clasica animacion android animation

que - programacion android pdf 2018



Iniciar la animaciĆ³n fotograma a fotograma (3)

Tengo una pregunta básica sobre cómo iniciar una animación fotograma a fotograma.

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

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

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

¿Por qué no funciona esta línea 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 no se puede llamar al método start () llamado en AnimationDrawable durante el método onCreate () de su actividad, porque AnimationDrawable aún no está completamente conectado a la ventana. Si desea reproducir la animación de inmediato, sin necesidad de interacción, puede llamarla desde el método onWindowFocusChanged () en su Actividad, que se activará cuando Android ponga su ventana en foco. 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>


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

img.post(new Starter());

Ayudante de clase interna:

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


para reproducir una animación solo en onCreate (...) agregue:

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