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();
}
});
The parallel thread approach seems to be the most popular one, but it does raise 2 major issues:
So, I suggest using the very aptly named runOnUiThread() method. Calling it in onResume() will assure you that your animation code will run on the main thread, that it would run after the window is attached, you'd know where exactly the message is about to be processed and no kittens need to lose their lives:
@Override
protected void onResume()
{
super.onResume();
runOnUiThread(new Runnable()
{
@Override
public void run()
{
animation.start();
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With