En el desarrollo del proyecto usé AnimationDrawable, llamé a start pero no se ejecutó, fue extraño. Busqué en Google. Lo registro aquí.
Este AnimationDrawable.start no se puede escribir directamente dentro de onClick, onStart u onResume; no es válido, no puede iniciar la animación. Solo se puede escribir, por ejemplo, dentro de un listener de eventos.
A continuación hay varias formas de ejecutar AnimationDrawable.
Primera forma: iniciar AnimationDrawable en un listener de eventos. El siguiente ejemplo muestra un evento que se genera cuando un árbol de vistas está a punto de dibujarse.
AnimationDrawable ad;
ImageView iv = (ImageView) findViewById(R.id.animation_view);
iv.setBackgroundResource(R.drawable.animation);
ad = (AnimationDrawable) iv.getBackground();
iv.getViewTreeObserver().addOnPreDrawListener(opdl);
OnPreDrawListener opdl=new OnPreDrawListener(){
@Override
public boolean onPreDraw() {
ad.start();
return true; //注意此行返回的值
}
};
Segunda forma de iniciar la animación: (la animación se ejecuta automáticamente al iniciar la Activity)
ImageView image = (ImageView) findViewById(R.id.animation_view);
image.setBackgroundResource(R.anim.oldsheep_wait);
animationDrawable = (AnimationDrawable) image.getBackground();
RunAnim runAnim=new RunAnim();
runAnim.execute("");
class RunAnim extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params)
{
if (!animationDrawable.isRunning())
{
animationDrawable.stop();
animationDrawable.start();
}
return "";
}
}
Tercera forma de iniciar la animación: (la animación se ejecuta automáticamente al iniciar la Activity)
ImageView image = (ImageView) findViewById(R.id.animation_view);
image.setBackgroundResource(R.anim.oldsheep_wait);
animationDrawable = (AnimationDrawable) image.getBackground();
image.post(new Runnable()
{
@Override
public void run()
{
animationDrawable.start();
}
});
Cuarta forma de iniciar la animación: (la animación se ejecuta automáticamente al iniciar la Activity)
ImageView image = (ImageView) findViewById(R.id.animation_view);
image.setBackgroundResource(R.anim.oldsheep_wait);
animationDrawable = (AnimationDrawable) image.getBackground();
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
animationDrawable.start();
super.onWindowFocusChanged(hasFocus);
}