Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do something when an animation finishes?

I have an ImageView that I use to show progress via an AnimationDrawable. When I want to show my progress spinner, I do this:

animDrawable.start(); ObjectAnimator.ofFloat(view, "alpha", 1.0f).setDuration(300).start(); 

When I want to hide the spinner, I do this:

ObjectAnimator.ofFloat(view, "alpha", 0.0f).setDuration(300).start(); animDrawable.stop(); 

However, this has the effect that the animation stops immediately. I would like it to stop only after the ObjectAnimator has completely faded to 0.0 alpha. Is there a way I can setup something along the lines of an "AnimationCompleted" callback?

like image 576
i_am_jorf Avatar asked Sep 01 '11 17:09

i_am_jorf


1 Answers

The more modern way of doing this is to use the ViewPropertyAnimator:

view.animate()     .alpha(0f)     .withEndAction(new Runnable() {       @Override       public void run() {         // Do something.       }     })     .start(); 

Or, if you're using RetroLambda:

view.animate()     .alpha(0f)     .withEndAction(() -> {       // Do something.     })     .start(); 
like image 195
i_am_jorf Avatar answered Sep 30 '22 19:09

i_am_jorf