检测ValueAnimator何时完成

Tyl*_*ler 41 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);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?我阅读了文档,但在完成时无法找到任何类型的监听器或回调.我试过使用,isRunning()但它没有用.

Fel*_*los 120

你可以这样做:

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();
Run Code Online (Sandbox Code Playgroud)


Cri*_*tan 8

在具有Android KTX的 Kotlin上:

animator.doOnEnd {
    // done
}
Run Code Online (Sandbox Code Playgroud)