I used AnimationDrawable in a project development, but after calling start, it didn't run. I was puzzled. I searched on Google. Let me record it.

This AnimationDrawable.start cannot be directly written in onClick, onStart, onResume; it is invalid and the animation cannot be started. It can only be written in, for example, event listeners.

Below are several ways to run AnimationDrawable.

First: start AnimationDrawable in an event listener. The following example demonstrates an event that occurs when a view tree is about to be drawn.

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; //注意此行返回的值
       }
};

Second way to start animation: (the animation will run automatically when the Activity starts)

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

Third way to start animation: (the animation will run automatically when the Activity starts)

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

Fourth way to start animation: (the animation will run automatically when the Activity starts)

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