17

I have a basic question about starting a frame-by-frame animation.

When I call the AnimationDrawable.start() method from my code directly, it doesn't seem to work.

public void onCreate(Bundle savedInstanceState) {  
   ...  
   mAnimation.start();  
   ...  
}

But if I put this line inside the onClick() callback method of a button, pressing the buton starts the animation.

Why doesn't this line work in the code?

Thanks!

Code:

public class MyAnimation extends Activity {
@Override

public void onCreate(Bundle savedInstanceState) {

    AnimationDrawable mframeAnimation = null;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_animation);

    ImageView img = (ImageView) findViewById(R.id.imgMain);

    BitmapDrawable frame1 = (BitmapDrawable) getResources().getDrawable(
            R.drawable.splash1);
    BitmapDrawable frame2 = (BitmapDrawable) getResources().getDrawable(
            R.drawable.splash2);

    int reasonableDuration = 250;
    mframeAnimation = new AnimationDrawable();
    mframeAnimation.setOneShot(false);
    mframeAnimation.addFrame(frame1, reasonableDuration);
    mframeAnimation.addFrame(frame2, reasonableDuration);

    img.setBackgroundDrawable(mframeAnimation);

    mframeAnimation.setVisible(true, true);
    //If this line is inside onClick(...) method of a button, animation works!!
    mframeAnimation.start(); 
}

}

OceanBlue
  • 9,142
  • 21
  • 62
  • 84

3 Answers3

36

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. Very end of the page http://developer.android.com/guide/topics/graphics/2d-graphics.html

 ImageView img = (ImageView)findViewById(R.id.some layout);
 AnimationDrawable frameAnimation =    (AnimationDrawable)img.getDrawable();
 frameAnimation.setCallback(img);
 frameAnimation.setVisible(true, true);
 frameAnimation.start();

and to add animation you can do something like

<animation-list   android:id="@+id/my_animation" android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/frame1" android:duration="150" />
    <item android:drawable="@drawable/frame2" android:duration="150" />

 </animation-list>  
Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54
  • Tried putting the whole code inside onStart(). Tried it with onResume too. Neither works :-( Not sure what I am doing wrong. – OceanBlue May 07 '10 at 19:11
  • add callback view( in my case animation defined in xml ) something like see edit in the answer – Alex Volovoy May 07 '10 at 19:18
  • Sorry Alex, adding the callback view, even changing the animation to XML instead of programmatically, doesn't work. And yet, when I put the start() method of the animation in the callback method of button and click the button, the animation works just fine. Quite a mystery. – OceanBlue May 07 '10 at 22:55
  • strange it works for me. i'm calling it onStart(). I'm at other computer now and don't have access to the code but i'll dig later. One thing you can try - instead of setting background, set image src. – Alex Volovoy May 07 '10 at 23:04
  • Alex, Thanks for taking the time to try to help me figure this out. I think I am doing something very basic wrong. When I use frame-by-frame animation with XML(instead of programmatically), it doesn't work at all. No exceptions, just does not work. If I figure out the problem, I'll post here. NEways, Thanks! – OceanBlue May 07 '10 at 23:28
  • hey, sorry about that - looking in the code - i was calling that in a thread. But if found this http://developer.android.com/guide/topics/graphics/2d-graphics.html ( very end of the page ) and updated the answer – Alex Volovoy May 10 '10 at 14:10
  • Thanks a ton Alex. Makes perfect sense now :-) – OceanBlue May 10 '10 at 22:49
  • is there a way to select to reset the start frame when I call frameAnimation.stop()? thanks anyway – Blackbelt Jun 14 '13 at 10:44
22

Use a Runnable to insert the start() message to message queue, just add this LOC to replace your mFrameAnimation.start();

img.post(new Starter());

Helper inner class:

class Starter implements Runnable {
        public void run() {
            mFrameAnimation.start();
        }
}
will
  • 221
  • 2
  • 3
5

to play an animation just in onCreate(...) add:

ImageView mImageView=(ImageView) findViewById(R.id.image);          
mImageView.setBackgroundResource(R.anim.film);    
mFrameAnimation = (AnimationDrawable) mImageView.getBackground();    
mImageView.post(new Runnable(){    
    public void run(){    
        mFrameAnimation.start();        
}
});
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
javanayomnik
  • 89
  • 1
  • 1
  • It's not working for me in a dialog, anybody any success with that? – Diego Aug 12 '13 at 01:00
  • 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. – Govtart Oct 09 '14 at 10:19