I need to draw a circle in an Android canvas, based on a gradient list of colors. I managed to draw it without the gradient, as a set of arcs each having one of the colors in the list, as presented by the following image.
How can I draw it with an actual gradient? I tried with the following code to apply a shader to the paint:
Shader shader = new LinearGradient(0, 0, circleWidth, circleHeight, colorList, null, Shader.TileMode.MIRROR);
paint.setShader(shader);
canvas.drawCircle(circleWidth / 2, circleHeight / 2, radius, paint);
but the result is as follows.
Gradient positions can be anywhere between 0 to 1. To use the gradient, set the fillStyle or strokeStyle property to the gradient, then draw the shape (rectangle, text, or a line).
To create a radial gradient with HTML5 Canvas, we can use the createRadialGradient() method. Radial gradients are defined with two imaginary circles - a starting circle and an ending circle, in which the gradient starts with the start circle and moves towards the end circle.
There is no special property for this; you simply use the background or background-image property and define your gradient in its value. You can create both linear and radial gradients this way.
I managed to make it using a SweepGradient.
Shader shader = new SweepGradient(circleWidth / 2, circleHeight / 2, colorList, null);
paint.setShader(shader);
canvas.drawCircle(circleWidth / 2, circleHeight / 2, radius, paint);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With