Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradient color animation

What I want to achieve:

Radial gradient, who's start and end colors are changing smoothly over time from one defined color to another.

What I have tried so far:

Using ObjectAnimator like this:

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

This will call DrawThread.setFraction(value); over time. Inside the thread I perform Canvas drawing using SurfaceView like this:

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

The problems:

  1. The gradient is not smooth. Image looks like low-color
  2. Performance is very low. I get aprox 16 FPS on FullHD Snapdragon 801 device.

So what I want to ask is any help in improving performance (even completely different way), and improving resulting image quality.

like image 934
Vladyslav Matviienko Avatar asked Nov 10 '22 09:11

Vladyslav Matviienko


1 Answers

You can animate color changes in gradient like this:

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 variant:

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()
}
like image 113
Rytis Guntulis Avatar answered Nov 15 '22 06:11

Rytis Guntulis