扫描渲染:
public class SweepGradient extends Shader {
private static final int TYPE_COLORS_AND_POSITIONS = 1;
private static final int TYPE_COLOR_START_AND_COLOR_END = 2;
/**
* SweepGradient的类型: 可以是TYPE_COLORS_AND_POSITIONS,
* 也可以是TYPE_COLOR_START_AND_COLOR_END.
*/
private int mType;
private float mCx;
private float mCy;
private int[] mColors;
private float[] mPositions;
private int mColor0;
private int mColor1;
/**
* 围绕中心点绘制扫描渐变的渲染器。
*
* @param cx 中心的x坐标
* @param cy 中心的y坐标
* @param colors 颜色分布在中心周围。数组中必须至少有两种颜色。
* @param positions 可能为空。颜色数组中每个相应颜色的相对位置,从0开始,到1.0结束。
* 如果值不是单调的,则绘图可能会产生意外的结果。
* 如果位置为空,则颜色将自动均匀分布。
*/
public SweepGradient(float cx, float cy,
@NonNull @ColorInt int colors[], @Nullable float positions[]) {
if (colors.length < 2) {
throw new IllegalArgumentException("needs >= 2 number of colors");
}
if (positions != null && colors.length != positions.length) {
throw new IllegalArgumentException(
"color and position arrays must be of equal length");
}
mType = TYPE_COLORS_AND_POSITIONS;
mCx = cx;
mCy = cy;
mColors = colors.clone();
mPositions = positions != null ? positions.clone() : null;
}
/**
* 围绕中心点绘制扫描渐变的渲染器。
*
* @param cx 中心的x坐标
* @param cy 中心的y坐标
* @param color0 扫描开始处使用的颜色
* @param color1 扫描结束处使用的颜色
*/
public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {
mType = TYPE_COLOR_START_AND_COLOR_END;
mCx = cx;
mCy = cy;
mColor0 = color0;
mColor1 = color1;
mColors = null;
mPositions = null;
}
@Override
long createNativeInstance(long nativeMatrix) {
if (mType == TYPE_COLORS_AND_POSITIONS) {
return nativeCreate1(nativeMatrix, mCx, mCy, mColors, mPositions);
} else { // TYPE_COLOR_START_AND_COLOR_END
return nativeCreate2(nativeMatrix, mCx, mCy, mColor0, mColor1);
}
}
/** @hide */
@Override
protected Shader copy() {
final SweepGradient copy;
if (mType == TYPE_COLORS_AND_POSITIONS) {
copy = new SweepGradient(mCx, mCy, mColors.clone(),
mPositions != null ? mPositions.clone() : null);
} else { // TYPE_COLOR_START_AND_COLOR_END
copy = new SweepGradient(mCx, mCy, mColor0, mColor1);
}
copyLocalMatrix(copy);
return copy;
}
private static native long nativeCreate1(long matrix, float x, float y,
int colors[], float positions[]);
private static native long nativeCreate2(long matrix, float x, float y,
int color0, int color1);
}