简体   繁体   中英

Draw a circle in a canvas with several gradient colors

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.

在此输入图像描述

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM