检测ValueAnimator何时完成

63

现在我正在检测ValueAnimator的结束,通过检查进度是否已达到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);
        }
    }
});

这是正确的方式吗?我阅读了文档,但找不到任何在其完成时进行监听或回调的方法。我尝试使用 isRunning() 但它同样不起作用。

3个回答

161

你可以像这样做:

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();

这是一个非常老的话题,抱歉,但我遇到了与此相关的问题。我正在使用监听器来检测我的动画何时完成,但它在实际动画完成之前执行。还有其他人遇到过这个问题吗?我看到了一些类似于为自定义视图进行动画处理的主题,并建议重写“AnimationFinished”方法,但这对值动画器无效。 - Nigel DH
1
android.animation.Animator.AnimatorListener 允许您重写 onAnimationEnd(Animator animation, boolean isReverse) 方法。 - Valery
谢谢,ValueAnimator的惊人解决方案。 - Steve

35

在使用Android KTX(核心)时,需要了解Kotlin:

animator.doOnEnd {
    // done
}

1
我记录了ValueAnimator的结果,并发现它并不会生成所有的值,只看这个例子:
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

所以,询问您问题时,我说检查值不是正确的方法。您需要添加onAnimationEnd listener,在这篇文章中进行了描述。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接