Where should I start an AnimationDrawable
that needs to animate when the activity is shown?
The developer guide recommends using onWindowFocusChanged
, but this isn't always called when the activity is part of a TabHost
.
I quote:
It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.
I know this question is a little bit old, but this may be helpful to someone happening across this question like I did. One way that I start my AnimationDrawable's is by creating a new Runnable and using the post method from the ImageView.
You can do like:
ImageView spinner = (ImageView) findViewById(R.id.my_imageView);
spinner.setBackgroundResource(R.drawable.spinner);
spinner.post(new Runnable() {
public void run() {
AnimationDrawable anim = (AnimationDrawable) spinner.getBackground();
anim.start();
}
});