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        if (colors.length < 2) {
58            throw new IllegalArgumentException("needs >= 2 number of colors");
59        }
60        if (positions != null && colors.length != positions.length) {
61            throw new IllegalArgumentException(
62                    "color and position arrays must be of equal length");
63        }
64        mType = TYPE_COLORS_AND_POSITIONS;
65        mCx = cx;
66        mCy = cy;
67        mColors = colors.clone();
68        mPositions = positions != null ? positions.clone() : null;
69    }
70
71    /**
72     * A Shader that draws a sweep gradient around a center point.
73     *
74     * @param cx       The x-coordinate of the center
75     * @param cy       The y-coordinate of the center
76     * @param color0   The color to use at the start of the sweep
77     * @param color1   The color to use at the end of the sweep
78     */
79    public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {
80        mType = TYPE_COLOR_START_AND_COLOR_END;
81        mCx = cx;
82        mCy = cy;
83        mColor0 = color0;
84        mColor1 = color1;
85        mColors = null;
86        mPositions = null;
87    }
88
89    @Override
90    long createNativeInstance(long nativeMatrix) {
91        if (mType == TYPE_COLORS_AND_POSITIONS) {
92            return nativeCreate1(nativeMatrix, mCx, mCy, mColors, mPositions);
93        } else { // TYPE_COLOR_START_AND_COLOR_END
94            return nativeCreate2(nativeMatrix, mCx, mCy, mColor0, mColor1);
95        }
96    }
97
98    /**
99     * @hide
100     */
101    @Override
102    protected Shader copy() {
103        final SweepGradient copy;
104        if (mType == TYPE_COLORS_AND_POSITIONS) {
105            copy = new SweepGradient(mCx, mCy, mColors.clone(),
106                    mPositions != null ? mPositions.clone() : null);
107        } else { // TYPE_COLOR_START_AND_COLOR_END
108            copy = new SweepGradient(mCx, mCy, mColor0, mColor1);
109        }
110        copyLocalMatrix(copy);
111        return copy;
112    }
113
114    private static native long nativeCreate1(long matrix, float x, float y,
115            int colors[], float positions[]);
116    private static native long nativeCreate2(long matrix, float x, float y,
117            int color0, int color1);
118}
119
120