57

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?

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • Couldn't you sleep the thread for the duration, then call .stop()? – Rob Sep 01 '11 at 17:23
  • I'd rather not sleep the UI thread. I could do a postDelayed() Runnable, but that seems very hacky. I suspect there is a correct way to do this. Furthermore, I'm not sure how accurate the whole duration thing is. – i_am_jorf Sep 01 '11 at 17:25
  • You could have the runnable check the alpha at a satisfying interval – Will Kru Sep 01 '11 at 17:28
  • 5
    There is no interval that will satisfy me. – i_am_jorf Sep 01 '11 at 17:36

5 Answers5

53

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();
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • 1
    As a side note thats perfect solution for single view, for multiple views there is no clean solution of using ex. AnimatorSet and ViewPropertyAnimator's. – kolboc May 10 '19 at 11:33
20

To your original question about the ObjectAnimator object you can set up an Animator.AnimatorListener object which defines several animation state callbacks. You want to override public void onAnimationEnd(Animator animation)

animation.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    Toast.makeText(VideoEditorActivity.this, "animation ended", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
Aaron Dancygier
  • 1,996
  • 19
  • 20
  • From my own experience `.onAnimationEnd` has proven to be very unreliable. I would recommend @i_am_jorf answer using `.withEndAction(...)` – Oush Mar 07 '22 at 09:14
9

With androidx you can use doOnEnd method which invoked when the animation has ended

val anim = ObjectAnimator.ofFloat(eva_image, View.TRANSLATION_Y, 0f, 500f)
anim.setDuration(500)
anim.doOnEnd { Toast.makeText(this, "Anim End", Toast.LENGTH_SHORT).show() }
anim.start()
Ajay
  • 4,850
  • 2
  • 32
  • 44
1

You could also look into postOnAnimation(Runnable)

Link to Docs: postOnAnimation(java.lang.Runnable)

ProjectDelta
  • 351
  • 4
  • 12
0

You can use this code with Object animator on Kotlin

ObjectAnimator.ofInt(
                view,
                "alpha",
                1.0f
            ).apply {
                duration = 300
                // some other params here
                doOnEnd { // your logic when the animation ended }
                start()
            }