tags:

views:

649

answers:

4

I want to do an animation with several image-files, and for this the AnimationDrawable works very well. However, I need to know when the animation starts and when it ends (i.e add a listener like the Animation.AnimationListener). After having searched for answers, I'm having a bad feeling the AnimationDrawable does not support listeners..

Does anyone know how to do frame-by-frame image animation on Android?

A: 

You should know when it starts, as you're the one starting it. And, since you know the duration (since you specify it), you should know when it ends.

You can also iterate over all of the frames (getNumberOfFrames()) and sum up the durations of each (getDuration()), if you do not want to hard-wire a duration in your Java code.

CommonsWare
A: 

Hello, i encountered the same problem and tried starting a timer with a delay that is equal to the time of the first animation. and when that timer runs out a timertask poked and in that run() I try to start a reverse animation. But it does not play.

Here is the code I used:

    public class Main extends Activity {

 AnimationDrawable sybAnimation;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView imageView = (ImageView)findViewById(R.id.ImageView01);        
        imageView.setBackgroundResource(R.anim.testanimation);
        sybAnimation = (AnimationDrawable) imageView.getBackground();

        imageView.post(new Starter());
    }

    class Starter implements Runnable {

  public void run() {
   sybAnimation.start();
   long totalDuration = 0;
   for(int i = 0; i< sybAnimation.getNumberOfFrames();i++){
    totalDuration += sybAnimation.getDuration(i);
   }
   Timer timer = new Timer(); 
      timer.schedule(new AnimationFollowUpTimerTask(R.id.ImageView01, R.anim.testanimation_reverse),totalDuration);

  }

    }

    class AnimationFollowUpTimerTask extends TimerTask {

     private int id;
     private int animationToRunId;

     public AnimationFollowUpTimerTask(int idOfImageView, int animationXML){

      id = idOfImageView;
      animationToRunId = animationXML;

     }

  @Override
  public void run() {
   ImageView imageView = (ImageView)findViewById(id);
         imageView.setBackgroundResource(animationToRunId);
         AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
         anim.start();
  }

    }
}
Syb
A: 

Hi Syb. I guess your Code does not work, because you try to modify a View from a non-UI-Thread. Try to call runOnUiThread(Runnable) from your Activity. I used it to fade out a menu after an animation for this menu finishes. This code works for me:

Animation ani =  AnimationUtils.loadAnimation(YourActivityNameHere.this, R.anim.fadeout_animation);
menuView.startAnimation(ani);

// Use Timer to set visibility to GONE after the animation finishes.            
TimerTask timerTask = new TimerTask(){
    @Override
    public void run() {
        YourActivityNameHere.this.runOnUiThread(new Runnable(){
            @Override
            public void run() {
                menuView.setVisibility(View.GONE);
            }
        });}};
timer.schedule(timerTask, ani.getDuration());
phantomas
A: 

Here is an interesting link for a solution: http://www.facebook.com/topic.php?uid=128857303793437&amp;topic=74

HowsItStack