AnimationDrawable, when does it end?
- by Syb
I know there have been several people with the same question. Which is: How do i know when a frame by frame animation has ended? I have not had any useful answer on fora i visited. So i thought, let's see if they know at stackoverflow. But I could not sit still in the mean time, so i made a work around of this, but it does not really work the way i would like it to.
here is the code:
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();
}
}
basically I make a timertask which is scheduled with the same time as the animation to take. In that run() I want to load a new animation into the imageView and start that animation, this however does not work. Does anyone know how to get this to work, or even better, have a better way to find out when an AnimationDrawable has ended its animation?