RadialGradient

环形渲染:

public class RadialGradient extends Shader {

    private static final int TYPE_COLORS_AND_POSITIONS = 1;
    private static final int TYPE_COLOR_CENTER_AND_COLOR_EDGE = 2;

    /**
     * RadialGradient类型: 可以是TYPE_COLORS_AND_POSITIONS,
     *                   也可以是TYPE_COLOR_CENTER_AND_COLOR_EDGE。
     */
    private int mType;

    private float mX;
    private float mY;
    private float mRadius;
    private int[] mColors;
    private float[] mPositions;
    private int mCenterColor;
    private int mEdgeColor;

    private TileMode mTileMode;

    /**
     * 创建一个绘制中心和半径的环形渐变渲染器。
     *
     * @param centerX  辐射中心的x坐标
     * @param centerY  辐射中心的y坐标
     * @param radius   必须是正值,辐射半径
     * @param colors   分布在辐射中心和边缘之间的颜色
     * @param stops    可能为空。有效值介于0.0f和1.0f之间。颜色数组中每个相应颜色的相对位置。
     * 				   如果为空,则颜色在辐射中心和边缘之间均匀分布。
     * @param tileMode 渲染器的拼接模式,辐射范围之外的渲染规则
     */
    public RadialGradient(float centerX, float centerY, float radius,
            @NonNull @ColorInt int colors[], @Nullable float stops[],
            @NonNull TileMode tileMode) {
        if (radius <= 0) {
            throw new IllegalArgumentException("radius must be > 0");
        }
        if (colors.length < 2) {
            throw new IllegalArgumentException("needs >= 2 number of colors");
        }
        if (stops != null && colors.length != stops.length) {
            throw new IllegalArgumentException("color and position arrays must be of equal length");
        }
        mType = TYPE_COLORS_AND_POSITIONS;
        mX = centerX;
        mY = centerY;
        mRadius = radius;
        mColors = colors.clone();
        mPositions = stops != null ? stops.clone() : null;
        mTileMode = tileMode;
    }

	/**
     * 创建一个绘制中心和半径的环形渐变渲染器。
     *
     * @param centerX  辐射中心的x坐标
     * @param centerY  辐射中心的y坐标
     * @param radius   必须是正值,辐射半径
     * @param centerColor 辐射中心的颜色。
     * @param edgeColor   辐射边缘的颜色。
     * @param tileMode 渲染器的拼接模式,辐射范围之外的渲染规则
     */
    public RadialGradient(float centerX, float centerY, float radius,
            @ColorInt int centerColor, @ColorInt int edgeColor, @NonNull TileMode tileMode) {
        if (radius <= 0) {
            throw new IllegalArgumentException("radius must be > 0");
        }
        mType = TYPE_COLOR_CENTER_AND_COLOR_EDGE;
        mX = centerX;
        mY = centerY;
        mRadius = radius;
        mCenterColor = centerColor;
        mEdgeColor = edgeColor;
        mTileMode = tileMode;
    }

    @Override
    long createNativeInstance(long nativeMatrix) {
        if (mType == TYPE_COLORS_AND_POSITIONS) {
            return nativeCreate1(nativeMatrix, mX, mY, mRadius,
                    mColors, mPositions, mTileMode.nativeInt);
        } else { // TYPE_COLOR_CENTER_AND_COLOR_EDGE
            return nativeCreate2(nativeMatrix, mX, mY, mRadius,
                    mCenterColor, mEdgeColor, mTileMode.nativeInt);
        }
    }

    /** @hide */
    @Override
    protected Shader copy() {
        final RadialGradient copy;
        if (mType == TYPE_COLORS_AND_POSITIONS) {
            copy = new RadialGradient(mX, mY, mRadius, mColors.clone(),
                    mPositions != null ? mPositions.clone() : null, mTileMode);
        } else { // TYPE_COLOR_CENTER_AND_COLOR_EDGE
            copy = new RadialGradient(mX, mY, mRadius, mCenterColor, mEdgeColor, mTileMode);
        }
        copyLocalMatrix(copy);
        return copy;
    }

    private static native long nativeCreate1(long matrix, float x, float y, float radius,
            int colors[], float positions[], int tileMode);
    private static native long nativeCreate2(long matrix, float x, float y, float radius,
            int color0, int color1, int tileMode);
}
发布了19 篇原创文章 · 获赞 0 · 访问量 874

猜你喜欢

转载自blog.csdn.net/qq_32036981/article/details/103803699