【问题标题】:Is it possible to change start/end values of ValueAnimator on animation repeat?是否可以在动画重复时更改 ValueAnimator 的开始/结束值?
【发布时间】:2017-03-18 06:25:42
【问题描述】:

我想设置无限颜色动画,将颜色从随机开始颜色更改为随机结束颜色,然后从该随机结束颜色更改为下一个随机结束颜色等。我尝试设置此值动画器:

ValueAnimator valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), randomColorStart, randomColorEnd);

然后尝试在onAnimationRepeat()方法上更新randomColorStartrandomColorEnd,但是更新的值不影响动画。 我怎么能做到这一点?

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    我不知道这个解决方案是否最好。但是,它有效!

    简而言之,我创建了ValueAnimator,并将其重复模式设置为INFINITE。然后,我实现了Animator.AnimatorListener 来覆盖onAnimationRepeat,它在持续时间结束时被调用。然后,在onAnimationRepeat 内部,我正在生成下一个随机颜色。

        int nextColor; // class-scope variable
        int startColor; // class-scope variable
    
        final Random rnd = new Random();
        final float[] hsv = new float[3];
        final float[] from = new float[3];
        final float[] to = new float[3];
    
        //generate random initial color and next color
        startColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        nextColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    
        // to get nicer smooth transition I'm using HSV 
        Color.colorToHSV(startColor, from);
        Color.colorToHSV(nextColor, to);
    
        final ValueAnimator anim = ValueAnimator.ofInt(startColor, nextColor);
        // animate from the current color to the next color;
    
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                hsv[0] = from[0] + (to[0] - from[0]) * animation.getAnimatedFraction();
                hsv[1] = from[1] + (to[1] - from[1]) * animation.getAnimatedFraction();
                hsv[2] = from[2] + (to[2] - from[2]) * animation.getAnimatedFraction();
    
                view.setBackgroundColor(Color.HSVToColor(hsv));
            }
        });
    
    
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
    
            }
    
            @Override
            public void onAnimationEnd(Animator animation) {
    
            }
    
            @Override
            public void onAnimationCancel(Animator animation) {
    
            }
    
            @Override
            public void onAnimationRepeat(Animator animation) {
                // generate next random color
                startColor = Color.HSVToColor(to);
                nextColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    
                Color.colorToHSV(startColor, from);
                Color.colorToHSV(nextColor, to);
            }
        });
    
        anim.setDuration(4000); // duration to change colors
        anim.setRepeatCount(ValueAnimator.INFINITE);
        anim.start();
    

    【讨论】:

    • 我会在周末回顾一下。我尝试了类似的方法但没有运气,可能是一些错误导致了这个。
    • 这个应该可以。我自己测试过。如果您遇到错误,请告诉我
    • 我一定是在某处留下了错误,因为我的动画在第一次正常迭代后开始闪烁颜色。顺便说一句,最好使用AnimatorListenerAdapter() 而不是AnimatorListener() 来缩短代码。你能告诉我为什么 HSV 的过渡比ValueAnimator.ofObject(new ArgbEvaluator(), randomColorStart, randomColorEnd); 更顺畅吗?
    • 参考herehere
    【解决方案2】:

    我认为接受的答案有点老套

    大多数 Animator 子类中都有 api,可以帮助在动画运行(或 onRepeat)时更改开始/结束值

    我们以 ValueAnimator 为例

        ValueAnimator.ofFloat(
            1f,
            3f
        ).apply {
            repeatCount = 2
            repeatMode = RESTART
            addUpdateListener {
                textView.scaleX = it.animatedValue
                textView.scaleY = it.animatedValue
            }
            doOnRepeat {
                // lets change start and end valeus
                (it as ValueAnimator).setFloatValues(1f, 5f)
            }
            start()
        }
    

    我们可以将 animator 转换为我们需要的 animator(ValueAnimator、ObjectAnimator 等)并设置开始和结束值(可以是 float、object 或任何其他值)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2022-08-17
      相关资源
      最近更新 更多