【问题标题】:Detecting when ValueAnimator is done检测 ValueAnimator 何时完成
【发布时间】:2013-11-27 03:53:34
【问题描述】:

现在我通过检查进度何时达到 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(),但效果不佳。

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    你可以这样做:

    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 方法,但这不适用于值动画师。
    • 更新:android.animation.Animator.AnimatorListener 允许您覆盖 onAnimationEnd(Animator animation, boolean isReverse) 方法。
    【解决方案2】:

    在 Kotlin 上使用 Android KTX(核心):

    animator.doOnEnd {
        // done
    }
    

    【讨论】:

      【解决方案3】:

      我记录了 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,在the post 中描述

      【讨论】:

        猜你喜欢
        • 2019-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多