【问题标题】:Gradient color animation渐变色动画
【发布时间】:2015-07-13 12:32:26
【问题描述】:

我想要达到的目标:

径向渐变,起始颜色和结束颜色随着时间的推移从一种定义的颜色平滑地变化到另一种定义的颜色。

到目前为止我所做的尝试:

像这样使用 ObjectAnimator:

        searchAnimator = ObjectAnimator.ofFloat(drawThread, new Property<DrawThread, Float>(Float.TYPE, "fraction") {
            @Override
            public Float get(DrawThread object) {
                return object.fraction;
            }

            @Override
            public void set(DrawThread object, Float value) {
                object.setFraction(value);
            }
        }, 0, 1);
        searchAnimator.setDuration(maxSearchDuration);
        searchAnimator.setInterpolator(new LinearInterpolator());

随着时间的推移,这将调用DrawThread.setFraction(value);。在线程内部,我使用SurfaceView 执行Canvas 绘图,如下所示:

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
radius = (int) Math.sqrt(canvas.getWidth() / 2 * canvas.getWidth() / 2 + canvas.getHeight() / 2 * canvas.getHeight() / 2);
//calculating colors for current fraction using ARGBEvaluator
int start = (int) argbEvaluator.evaluate(fraction, colors[0].start, colors[1].start);
int end = (int) argbEvaluator.evaluate(fraction, colors[0].end, colors[1].end);
//end
mPaint.setShader(new RadialGradient(canvas.getWidth() / 2, canvas.getHeight() / 2,
      radius, start, end, Shader.TileMode.CLAMP));
canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, radius, mPaint);

问题:

  1. 渐变不平滑。图片看起来颜色很浅
  2. 性能非常低。我在全高清 Snapdragon 801 设备上获得了大约 16 FPS。

所以我想问的是在提高性能(甚至完全不同的方式)和提高图像质量方面的任何帮助。

【问题讨论】:

  • 我可能会尝试使用GradientDrawable 作为普通视图(不是 SurfaceView)的背景。并使用 ValueAnimator 计算和回调每个动画帧,您可以在其中计算梯度值并调用 gradientDrawable.setColors(int[])
  • @Budius,谢谢,但我已经尝试过了。性能相同,setColors 方法不适用于旧 API(如 7)
  • 这将是我的第一个方法,因为它比 SurfaceView 简单得多。您的后台线程不是在做其他减慢速度的工作吗?关于 API 级别,不确定您的要求,但 API 7 太旧了,甚至 Google 都在很长一段时间内放弃了对它们的支持。它甚至没有显示在仪表板上:developer.android.com/about/dashboards/…
  • @Budius 这里是 API 文档,上面写着 setColors() 是在 API 16 中添加的 developer.android.com/reference/android/graphics/drawable/… 我完全不能选择放弃对旧 API 的支持

标签: android performance animation gradient


【解决方案1】:

您可以像这样在渐变中为颜色变化设置动画:

int colorFrom = ContextCompat.getColor(this, R.color.red);
int colorTo = ContextCompat.getColor(this, R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(2000);

int color1 = ContextCompat.getColor(this, R.color.red);
int color2 = ContextCompat.getColor(this, R.color.green);
GradientDrawable gradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM, new int[]{color1, color2});
gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);

colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animator) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                gradientDrawable.setColors(new int[]{(int) animator.getAnimatedValue(), color2});
                viewWithGradientBg.setBackground(gradientDrawable);
            }
        }

    });

colorAnimation.start();

Kotlin 变体:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val colorFrom = ContextCompat.getColor(requireContext(), android.R.color.holo_red_dark)
    val colorTo = ContextCompat.getColor(requireContext(), android.R.color.holo_blue_dark)
    val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
    colorAnimation.duration = 2000
    val color1 = ContextCompat.getColor(requireContext(), android.R.color.holo_red_dark)
    val color2 = ContextCompat.getColor(requireContext(), android.R.color.holo_blue_dark)
    val gradientDrawable = GradientDrawable(GradientDrawable.Orientation.TL_BR, intArrayOf(color1, color2))
    gradientDrawable.gradientType = GradientDrawable.RADIAL_GRADIENT
    colorAnimation.addUpdateListener { animator ->
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            gradientDrawable.colors = intArrayOf(animator.animatedValue as Int, color2)
            viewWithGradientBg.background = gradientDrawable
        }
    }
    gradientDrawable.gradientRadius = 140f;
    gradientDrawable.setGradientCenter(0.5f, 0.5f);
    colorAnimation.repeatCount = INFINITE
    colorAnimation.start()
}

【讨论】:

  • 您可以在gradientDrawable.setColors(...)方法中更改所有颜色。
  • 这应该可行。我现在无法检查。我使用了一种解决方法 - 在视图上放置一个静态透明渐变,并更改视图的纯色背景颜色
  • 抱歉,将“RADIAL_GRADIENT”更改为“LINEAR_GRADIENT”
  • gradientDrawable.gradientRadius = 140f; gradientDrawable.setGradientCenter(0.5f, 0.5f) 无论我输入什么数字,这些变量似乎都对渐变没有任何影响
【解决方案2】:

也许这段伪代码有帮助:

private ValueAnimator colorAnimation;

  colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), startColor, endColor); 
                                                          // you can add more colors here 

    colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animator) {
            yourView.setBackgroundColor((Integer) animator
                    .getAnimatedValue());
        }

    });

    colorAnimation.setDuration(maxSearchDuration);
    colorAnimation.start();
}

【讨论】:

  • 在背景上设置单色动画是没有问题的。但是,如果您再次阅读我的问题,您会发现问题在于为 gradient 设置动画
猜你喜欢
  • 1970-01-01
  • 2015-07-13
  • 2016-03-19
  • 1970-01-01
  • 2012-06-13
  • 2017-06-04
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多