Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AnimationDrawable not playing

Tags:

So I want my animation to start as soon as the activity is created, but for some reason no matter what I try will get it to start. I can get it to start by having a click event but I want it to start all on its own.

Here's what I have and how do I get this to work?

package tween.learn;  import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.widget.ImageView;  public class Animate extends Activity {      public ImageView image;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);           ImageView tweenImage = (ImageView) findViewById(R.id.imageView1);         tweenImage.setBackgroundResource(R.anim.cubicfacetween);          AnimationDrawable frameAnimation =                             (AnimationDrawable) tweenImage.getBackground();         frameAnimation.start();          }    } 

Thanks

like image 946
Joshua Sutherland Avatar asked Mar 30 '11 18:03

Joshua Sutherland


1 Answers

I think you have to start the animation after initialization of the view in question is complete. You should be able to do something like:

final ImageView tweenImage = (ImageView) findViewById(R.id.imageView1); tweenImage.setBackgroundResource(R.anim.cubicfacetween);       tweenImage.post(new Runnable() {     @Override     public void run() {         AnimationDrawable frameAnimation =             (AnimationDrawable) tweenImage.getBackground();         frameAnimation.start();     } } 

Edit - this question led me to believe that the onWindowFocusChanged method won't always work. It does seem simpler and is probably a better idea if it works for you.

like image 83
Matthew Willis Avatar answered Oct 20 '22 18:10

Matthew Willis