Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. Introduction

SweepGradient can achieve scan gradient rendering, similar to radar scan, gradient arc, gradient progress bar, etc., there are two constructors

/** * A Shader that draws a sweep gradient around a center point. * * @param cx The x-coordinate of the center * @param cy The y-coordinate of the center * @param colors The colors to be distributed between around the center. * There must be at least 2 colors in the array. * @param positions May be NULL. The relative position of * each corresponding color In the colors array, beginning * with 0 and ending with 1.0. the drawing may produce unexpected results. * If positions is NULL, then the colors are automatically * spaced evenly. */ public SweepGradient(float cx, float cy, @nonnull @colorint int colors[], @nullable float positions[]); /** * A Shader that draws a sweep gradient around a center point. * * @param cx The x-coordinate of the center * @param cy The y-coordinate of the center * @param color0 The color to use at the start of the sweep * @param color1 The color To use at the end of the sweep */ public SweepGradient(float Cx, float cy, @colorint int color0, @colorint int color1);Copy the code

Parameter description: cx,cy gradient center coordinates. Color0,color1: gradient start and end colors. Colors, Positions: Similar to LinearGradient, used for multi-color gradients, and positions that are null change linearly based on the color.

2. Two color gradients

Constructor: SweepGradient(float cx, float cy, @colorint int color0, @colorint int color1)

SweepGradient sweepGradient1 = new SweepGradient(400, 400, Color.RED, Color.GREEN);
mPaint.setShader(sweepGradient1);
canvas.drawCircle(400, 400, 200, mPaint);
Copy the code

3. Multi-color scan gradient

Constructor: SweepGradient(float Cx, float cy, @nonNULL @colorInt int colors[], @nullable float positions[])

int[] colors = {Color.BLUE, Color.RED, Color.YELLOW};
SweepGradient sweepGradient2 = new SweepGradient(400, 800, colors, null);
mPaint.setShader(sweepGradient2);
canvas.drawCircle(400, 800, 200, mPaint);
Copy the code

Set the position:

Position array setting is mainly used to correspond to the color array of a specific position. The value of position is [0-1], where 0 indicates the start position and 1 indicates the end position. The array and color array correspond one by one.

int[] colors = {Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN}; Float [] positions = {0f, 0.2f, 0.6f, 1.0f}; float[] positions = {0f, 0.2f, 0.6f, 1.0f}; SweepGradient sweepGradient2 = new SweepGradient(400, 400, colors, positions); mPaint.setShader(sweepGradient2); canvas.drawCircle(400, 400, 200, mPaint);Copy the code

\