57

Right now I am detecting the end of my ValueAnimator by checking when the progress has reached 100...

//Setup the animation
ValueAnimator anim = ValueAnimator.ofInt(progress, seekBar.getMax());

//Set the duration

anim.setDuration(Utility.setAnimationDuration(progress));

anim.addUpdateListener(new AnimatorUpdateListener() 
{

    @Override
    public void onAnimationUpdate(ValueAnimator animation) 
    {
        int animProgress = (Integer) animation.getAnimatedValue();

        if ( animProgress == 100)
        {
            //Done
        }

        else
        {
            seekBar.setProgress(animProgress);
        }
    }
});

Is this the correct way? I read through the docs and couldn't find any kind listener or callback for when it completes. I tried using isRunning() but it didn't work as well.

Tyler
  • 15,509
  • 9
  • 45
  • 81

3 Answers3

147

You can do something like:

ValueAnimator anim = ValueAnimator.ofInt(progress, seekBar.getMax());
anim.setDuration(Utility.setAnimationDuration(progress));
anim.addUpdateListener(new AnimatorUpdateListener() 
{
    @Override
    public void onAnimationUpdate(ValueAnimator animation) 
    {
        int animProgress = (Integer) animation.getAnimatedValue();
        seekBar.setProgress(animProgress);
    }
});
anim.addListener(new AnimatorListenerAdapter() 
{
    @Override
    public void onAnimationEnd(Animator animation) 
    {
        // done
    }
});
anim.start();
Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Felipe Vasconcelos
  • 2,326
  • 1
  • 19
  • 26
  • This is a very old topic, sorry, but I'm having a problem related to this. I'm using a listener to detect when my animation is finished, but it is executing before the actual animation is done. Has anyone else run in to this? I've seen topics with something similar for animating a custom view and the suggestion was to override the `AnimationFinished` method, but that doesn't work for a value animator. – Nigel DH Jan 02 '18 at 20:54
  • 1
    update: `android.animation.Animator.AnimatorListener` lets you override `onAnimationEnd(Animator animation, boolean isReverse)` method. – Valery Mar 18 '19 at 13:13
21

On Kotlin with Android KTX (Core):

animator.doOnEnd {
    // done
}
Cristan
  • 6,550
  • 4
  • 45
  • 46
0

i logged results for ValueAnimator and saw that it does not generate all values, just look this:

03-19 10:30:52.132 22170-22170/com.sample.project D/View:  next = 86
03-19 10:30:52.148 22170-22170/com.sample.project D/View:  next = 87
03-19 10:30:52.165 22170-22170/com.sample.project D/View:  next = 89
03-19 10:30:52.181 22170-22170/com.sample.project D/View:  next = 91
03-19 10:30:52.198 22170-22170/com.sample.project D/View:  next = 92
03-19 10:30:52.215 22170-22170/com.sample.project D/View:  next = 94
03-19 10:30:52.231 22170-22170/com.sample.project D/View:  next = 96
03-19 10:30:52.248 22170-22170/com.sample.project D/View:  next = 97
03-19 10:30:52.265 22170-22170/com.sample.project D/View:  next = 99
03-19 10:30:52.282 22170-22170/com.sample.project D/View:  next = 101

So asking you questuion i say to check value isn't correct way. You need add onAnimationEnd listener, described in the post

Serg Burlaka
  • 1,867
  • 19
  • 31