Michael
Michael

Reputation: 44160

Android: Two ScaleAnimations performed sequentially

I want to animate a scale an ImageView vertically, wait, then perform another scale on the same ImageView. I've simplified the code below but it's essentially what I want to do:

ScaleAnimation animate = new ScaleAnimation(1,1,1,2);
animate.setDuration(1000);
animate.fillAfter(true);

ScaleAnimation animateAgain = new ScaleAnimation(1,1,2,1);
animate.setDuration(1000);
animate.fillAfter(true);

view.startAnimation(animate);
view.startAnimation(animateAgain);

I've tried multiple ways of waiting for the first animation to finish (Thread.sleep etc.) but it doesn't seem to make any difference. I assume the animation is rendered in the onDraw method or something? I'm not entirely sure how scale animations work so I can't really grasp how to solve my problem.

What's the simplest way of doing what I want to accomplish?

Thanks guys :)

Upvotes: 0

Views: 493

Answers (1)

Catherine
Catherine

Reputation: 14010

There are a couple of ways:

Attach an AnimationListener to your animations.

ScaleAnimation animate = new ScaleAnimation(1,1,1,2);
animate.setDuration(1000);
animate.fillAfter(true);
animate.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationEnd(Animation animation) {
        ScaleAnimation animateAgain = new ScaleAnimation(1,1,2,1);
        animateAgain.setDuration(1000);
        animateAgain.fillAfter(true);
        view.startAnimation(animateAgain);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }});

view.startAnimation(animate);

Alternately, you can set an animation delay for your second animation.

ScaleAnimation animateAgain = new ScaleAnimation(1,1,2,1);
animateAgain.setDuration(1000);
animateAgain.fillAfter(true);
animateAgain.setStartOffset(firstAnimationDuration + delay);

Upvotes: 1

Related Questions