检测ValueAnimator何时完成

时间:2013-11-27 03:53:34

标签: android android-animation

现在我通过检查进度何时达到100来检测我的ValueAnimator的结束......

//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 个答案:

答案 0 :(得分:110)

您可以执行以下操作:

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

答案 1 :(得分:3)

Android KTX的Kotlin上:

animator.doOnEnd {
    // done
}

答案 2 :(得分:0)

我记录了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 监听器,如the post

中所述