Paint 的 SweepGradient 是 梯度渐变渲染 ;
SweepGradient 是围绕中心点绘制扫描渐变的着色器。
SweepGradient 文档地址 : https://developer.android.google.cn/reference/android/graphics/SweepGradient
public SweepGradient (
float cx, // The x-coordinate of the center
float cy, // The y-coordinate of the center
int[] colors, // The sRGB colors to be distributed between around the center. There must be at least 2 colors in the array. This value cannot be null.
float[] positions // May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly. This value may be null.
)
参数说明 :
public SweepGradient (
float cx, // The x-coordinate of the center
float cy, // The y-coordinate of the center
long[] colors, // The colors to be distributed between around the center. There must be at least 2 colors in the array. This value cannot be null.
float[] positions // May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly. This value may be null.
)
参数说明 :
代码示例 :
mPaint.setShader(new SweepGradient(
this.getWidth() / 2,
this.getHeight() / 2,
new int[]{Color.RED, Color.GREEN, Color.BLUE},
new float[]{0F, 0.5F, 1.0F})
);
public SweepGradient (
float cx, // The x-coordinate of the center
float cy, // The y-coordinate of the center
int color0, // The sRGB color to use at the start of the sweep
int color1 // The sRGB color to use at the end of the sweep
)
参数说明 :
public SweepGradient (
float cx, // The x-coordinate of the center
float cy, // The y-coordinate of the center
long color0, // The color to use at the start of the sweep
long color1 // The color to use at the end of the sweep
)
参数说明 :
代码示例 :
mPaint.setShader(new SweepGradient(
this.getWidth() / 2,
this.getHeight() / 2,
Color.GREEN, Color.BLUE)
);
package kim.hsl.paintgradient.sweep;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class SweepGradientView2 extends View {
/**
* 画笔工具
* 线性渐变渲染 需要设置给该 画笔工具
*/
private Paint mPaint;
/**
* 使用线性渐变绘制的区域
*/
private RectF mRectF;
public SweepGradientView2(Context context) {
this(context, null);
}
public SweepGradientView2(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public SweepGradientView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(width, height, oldWidth, oldHeight);
}
/**
* 初始化 画笔工具, 主要是设置该画笔的渲染
*/
private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setShader(new SweepGradient(
this.getWidth() / 2,
this.getHeight() / 2,
new int[]{Color.RED, Color.GREEN, Color.BLUE},
new float[]{0F, 0.5F, 1.0F})
);
canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, 200, mPaint);
}
}
package kim.hsl.paintgradient.sweep;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class SweepGradientView extends View {
/**
* 画笔工具
* 线性渐变渲染 需要设置给该 画笔工具
*/
private Paint mPaint;
/**
* 使用线性渐变绘制的区域
*/
private RectF mRectF;
public SweepGradientView(Context context) {
this(context, null);
}
public SweepGradientView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public SweepGradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(width, height, oldWidth, oldHeight);
}
/**
* 初始化 画笔工具, 主要是设置该画笔的渲染
*/
private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setShader(new SweepGradient(
this.getWidth() / 2,
this.getHeight() / 2,
Color.GREEN, Color.BLUE)
);
canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, 200, mPaint);
}
}