Android 检测 ValueAnimator 何时完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20233558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:30:09  来源:igfitidea点击:

Detecting when ValueAnimator is done

androidandroid-animation

提问by Tyler

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

现在我通过检查进度何时达到 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);
        }
    }
});

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.

这是正确的方法吗?我通读了文档,但在完成时找不到任何类型的侦听器或回调。我尝试使用,isRunning()但效果不佳。

回答by Felipe Vasconcelos

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

回答by Cristan

On Kotlin with Android KTX(Core):

在带有Android KTX(核心)的Kotlin 上:

animator.doOnEnd {
    // done
}

回答by Serg Burlaka

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

我记录了 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

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

所以问你问题我说检查价值不是正确的方法。您需要添加onAnimationEnd listener,在帖子中描述