Paint 的 RadialGradient 是 环形渐变渲染 ;
RadialGradient 是 在给定中心和半径的情况下 绘制径向渐变 的着色器。
RadialGradient 文档地址 : https://developer.android.google.cn/reference/kotlin/android/graphics/RadialGradient
RadialGradient(
centerX: Float, // The x-coordinate of the center of the radius
centerY: Float, // The y-coordinate of the center of the radius
radius: Float, // Must be positive. The radius of the circle for this gradient.
colors: IntArray, // The sRGB colors to be distributed between the center and edge of the circle This value cannot be null.
stops: FloatArray?, // May be null. Valid values are between 0.0f and 1.0f. The relative position of each corresponding color in the colors array. If null, colors are distributed evenly between the center and edge of the circle.
tileMode: Shader.TileMode // The Shader tiling mode This value cannot be null.
)
参数说明 :
RadialGradient(
centerX: Float, // The x-coordinate of the center of the radius
centerY: Float, // The y-coordinate of the center of the radius
radius: Float, // Must be positive. The radius of the circle for this gradient.
colors: LongArray, // The colors to be distributed between the center and edge of the circle This value cannot be null.
stops: FloatArray?, // May be null. Valid values are between 0.0f and 1.0f. The relative position of each corresponding color in the colors array. If null, colors are distributed evenly between the center and edge of the circle.
tileMode: Shader.TileMode // The Shader tiling mode This value cannot be null.
)
参数说明 :
代码示例 :
mPaint.setShader(new RadialGradient(
this.getWidth() / 2,
this.getHeight() / 2,
200,
Color.GREEN, Color.BLUE,
Shader.TileMode.CLAMP)
);
RadialGradient(
centerX: Float, // The x-coordinate of the center of the radius
centerY: Float, // The y-coordinate of the center of the radius
radius: Float, // Must be positive. The radius of the circle for this gradient.
centerColor: Int, // The sRGB color at the center of the circle.
edgeColor: Int, // The sRGB color at the edge of the circle.
tileMode: Shader.TileMode // The Shader tiling mode This value cannot be null.
)
参数说明 :
RadialGradient(
centerX: Float, // The x-coordinate of the center of the radius
centerY: Float, // The y-coordinate of the center of the radius
radius: Float, // Must be positive. The radius of the circle for this gradient.
centerColor: Long, // The color at the center of the circle.
edgeColor: Long, // The color at the edge of the circle.
tileMode: Shader.TileMode // The Shader tiling mode This value cannot be null.
)
参数说明 :
代码示例 :
mPaint.setShader(new RadialGradient(
this.getWidth() / 2,
this.getHeight() / 2,
200,
new int[]{Color.RED, Color.GREEN, Color.BLUE},
new float[]{0F, 0.5F, 1.0F},
Shader.TileMode.CLAMP)
);
package kim.hsl.paintgradient.radial;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
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 RadialGradientView extends View {
/**
* 画笔工具
* 线性渐变渲染 需要设置给该 画笔工具
*/
private Paint mPaint;
/**
* 使用线性渐变绘制的区域
*/
private RectF mRectF;
public RadialGradientView(Context context) {
this(context, null);
}
public RadialGradientView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public RadialGradientView(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 RadialGradient(
this.getWidth() / 2,
this.getHeight() / 2,
200,
Color.GREEN, Color.BLUE,
Shader.TileMode.CLAMP)
);
canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, 200, mPaint);
}
}
package kim.hsl.paintgradient.radial;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
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 RadialGradientView2 extends View {
/**
* 画笔工具
* 线性渐变渲染 需要设置给该 画笔工具
*/
private Paint mPaint;
/**
* 使用线性渐变绘制的区域
*/
private RectF mRectF;
public RadialGradientView2(Context context) {
this(context, null);
}
public RadialGradientView2(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public RadialGradientView2(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 RadialGradient(
this.getWidth() / 2,
this.getHeight() / 2,
200,
new int[]{Color.RED, Color.GREEN, Color.BLUE},
new float[]{0F, 0.5F, 1.0F},
Shader.TileMode.CLAMP)
);
canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, 200, mPaint);
}
}