SweepGradient.java revision b581e6704fc1478bc1dda517502abd3eab2558d6
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19import android.annotation.ColorInt;
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22
23public class SweepGradient extends Shader {
24
25    private static final int TYPE_COLORS_AND_POSITIONS = 1;
26    private static final int TYPE_COLOR_START_AND_COLOR_END = 2;
27
28    /**
29     * Type of the LinearGradient: can be either TYPE_COLORS_AND_POSITIONS or
30     * TYPE_COLOR_START_AND_COLOR_END.
31     */
32    private int mType;
33
34    private float mCx;
35    private float mCy;
36    private int[] mColors;
37    private float[] mPositions;
38    private int mColor0;
39    private int mColor1;
40
41    /**
42     * A Shader that draws a sweep gradient around a center point.
43     *
44     * @param cx       The x-coordinate of the center
45     * @param cy       The y-coordinate of the center
46     * @param colors   The colors to be distributed between around the center.
47     *                 There must be at least 2 colors in the array.
48     * @param positions May be NULL. The relative position of
49     *                 each corresponding color in the colors array, beginning
50     *                 with 0 and ending with 1.0. If the values are not
51     *                 monotonic, the drawing may produce unexpected results.
52     *                 If positions is NULL, then the colors are automatically
53     *                 spaced evenly.
54     */
55    public SweepGradient(float cx, float cy,
56            @NonNull @ColorInt int colors[], @Nullable float positions[]) {
57        set(cx, cy, colors, positions);
58    }
59
60    /**
61     * A Shader that draws a sweep gradient around a center point.
62     *
63     * @param cx       The x-coordinate of the center
64     * @param cy       The y-coordinate of the center
65     * @param color0   The color to use at the start of the sweep
66     * @param color1   The color to use at the end of the sweep
67     */
68    public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {
69        set(cx, cy, color0, color1);
70    }
71
72    /**
73     * Reinitialize the shader.
74     *
75     * @param cx       The x-coordinate of the center
76     * @param cy       The y-coordinate of the center
77     * @param colors   The colors to be distributed between around the center.
78     *                 There must be at least 2 colors in the array.
79     * @param positions May be NULL. The relative position of
80     *                 each corresponding color in the colors array, beginning
81     *                 with 0 and ending with 1.0. If the values are not
82     *                 monotonic, the drawing may produce unexpected results.
83     *                 If positions is NULL, then the colors are automatically
84     *                 spaced evenly.
85     */
86    public void set(float cx, float cy,
87            @NonNull @ColorInt int colors[], @Nullable float positions[]) {
88        if (colors.length < 2) {
89            throw new IllegalArgumentException("needs >= 2 number of colors");
90        }
91        if (positions != null && colors.length != positions.length) {
92            throw new IllegalArgumentException(
93                    "color and position arrays must be of equal length");
94        }
95        discardNativeInstance();
96        mType = TYPE_COLORS_AND_POSITIONS;
97        mCx = cx;
98        mCy = cy;
99        mColors = colors.clone();
100        mPositions = positions != null ? positions.clone() : null;
101    }
102
103    /**
104     * Reinitialize the shader.
105     *
106     * @param cx       The x-coordinate of the center
107     * @param cy       The y-coordinate of the center
108     * @param color0   The color to use at the start of the sweep
109     * @param color1   The color to use at the end of the sweep
110     */
111    public void set(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {
112        discardNativeInstance();
113        mType = TYPE_COLOR_START_AND_COLOR_END;
114        mCx = cx;
115        mCy = cy;
116        mColor0 = color0;
117        mColor1 = color1;
118        mColors = null;
119        mPositions = null;
120    }
121
122    @Override
123    long createNativeInstance(long nativeMatrix) {
124        if (mType == TYPE_COLORS_AND_POSITIONS) {
125            return nativeCreate1(nativeMatrix, mCx, mCy, mColors, mPositions);
126        } else { // TYPE_COLOR_START_AND_COLOR_END
127            return nativeCreate2(nativeMatrix, mCx, mCy, mColor0, mColor1);
128        }
129    }
130
131    /**
132     * @hide
133     */
134    @Override
135    protected Shader copy() {
136        final SweepGradient copy;
137        if (mType == TYPE_COLORS_AND_POSITIONS) {
138            copy = new SweepGradient(mCx, mCy, mColors.clone(),
139                    mPositions != null ? mPositions.clone() : null);
140        } else { // TYPE_COLOR_START_AND_COLOR_END
141            copy = new SweepGradient(mCx, mCy, mColor0, mColor1);
142        }
143        copyLocalMatrix(copy);
144        return copy;
145    }
146
147    private static native long nativeCreate1(long matrix, float x, float y,
148            int colors[], float positions[]);
149    private static native long nativeCreate2(long matrix, float x, float y,
150            int color0, int color1);
151}
152
153