1、多种颜色渐变
colors 沿渐变线分布的sRGB颜色数组
positions 可能为空。颜色阵列中每个对应颜色的相对位置[0..1]。如果为空,则颜色将沿着渐变线均匀分布。
Shader shader = new LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors,
@Nullable float[] positions, Shader.TileMode.MIRROR /*or REPEAT*/);
mPaint.setShader(shader);
2、两种颜色渐变
Shader shader = new LinearGradient(float x0, float y0, float x1, float y1,
@ColorInt int color0, @ColorInt int color1,
Shader.TileMode.MIRROR /*or REPEAT*/);
mPaint.setShader(shader);
接下来使用drawLine方法绘制即可
canvas.drawLine(startXLine1 ,stopXLine1, startYLine1, stopYLine1, mPaint);