RadialGradient.java revision af046ab637715e420f714ab48ca4788056311609
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.NonNull;
20import android.annotation.Nullable;
21
22public class RadialGradient extends Shader {
23
24    private static final int TYPE_COLORS_AND_POSITIONS = 1;
25    private static final int TYPE_COLOR_CENTER_AND_COLOR_EDGE = 2;
26
27    /**
28     * Type of the RadialGradient: can be either TYPE_COLORS_AND_POSITIONS or
29     * TYPE_COLOR_CENTER_AND_COLOR_EDGE.
30     */
31    private int mType;
32
33    private float mX;
34    private float mY;
35    private float mRadius;
36    private int[] mColors;
37    private float[] mPositions;
38    private int mCenterColor;
39    private int mEdgeColor;
40
41    private TileMode mTileMode;
42
43    /** Create a shader that draws a radial gradient given the center and radius.
44        @param centerX  The x-coordinate of the center of the radius
45        @param centerY  The y-coordinate of the center of the radius
46        @param radius   Must be positive. The radius of the circle for this gradient.
47        @param colors   The colors to be distributed between the center and edge of the circle
48        @param stops    May be <code>null</code>. Valid values are between <code>0.0f</code> and
49                        <code>1.0f</code>. The relative position of each corresponding color in
50                        the colors array. If <code>null</code>, colors are distributed evenly
51                        between the center and edge of the circle.
52        @param tileMode The Shader tiling mode
53    */
54    public RadialGradient(float centerX, float centerY, float radius,
55               @NonNull int colors[], @Nullable float stops[], @NonNull TileMode tileMode) {
56        if (radius <= 0) {
57            throw new IllegalArgumentException("radius must be > 0");
58        }
59        if (colors.length < 2) {
60            throw new IllegalArgumentException("needs >= 2 number of colors");
61        }
62        if (stops != null && colors.length != stops.length) {
63            throw new IllegalArgumentException("color and position arrays must be of equal length");
64        }
65        mType = TYPE_COLORS_AND_POSITIONS;
66        mX = centerX;
67        mY = centerY;
68        mRadius = radius;
69        mColors = colors;
70        mPositions = stops;
71        mTileMode = tileMode;
72        init(nativeCreate1(centerX, centerY, radius, colors, stops, tileMode.nativeInt));
73    }
74
75    /** Create a shader that draws a radial gradient given the center and radius.
76        @param centerX     The x-coordinate of the center of the radius
77        @param centerY     The y-coordinate of the center of the radius
78        @param radius      Must be positive. The radius of the circle for this gradient
79        @param centerColor The color at the center of the circle.
80        @param edgeColor   The color at the edge of the circle.
81        @param tileMode    The Shader tiling mode
82    */
83    public RadialGradient(float centerX, float centerY, float radius,
84            int centerColor, int edgeColor, @NonNull TileMode tileMode) {
85        if (radius <= 0) {
86            throw new IllegalArgumentException("radius must be > 0");
87        }
88        mType = TYPE_COLOR_CENTER_AND_COLOR_EDGE;
89        mX = centerX;
90        mY = centerY;
91        mRadius = radius;
92        mCenterColor = centerColor;
93        mEdgeColor = edgeColor;
94        mTileMode = tileMode;
95        init(nativeCreate2(centerX, centerY, radius, centerColor, edgeColor, tileMode.nativeInt));
96    }
97
98    /**
99     * @hide
100     */
101    @Override
102    protected Shader copy() {
103        final RadialGradient copy;
104        switch (mType) {
105            case TYPE_COLORS_AND_POSITIONS:
106                copy = new RadialGradient(mX, mY, mRadius, mColors.clone(),
107                        mPositions != null ? mPositions.clone() : null, mTileMode);
108                break;
109            case TYPE_COLOR_CENTER_AND_COLOR_EDGE:
110                copy = new RadialGradient(mX, mY, mRadius, mCenterColor, mEdgeColor, mTileMode);
111                break;
112            default:
113                throw new IllegalArgumentException("RadialGradient should be created with either " +
114                        "colors and positions or center color and edge color");
115        }
116        copyLocalMatrix(copy);
117        return copy;
118    }
119
120    private static native long nativeCreate1(float x, float y, float radius,
121            int colors[], float positions[], int tileMode);
122    private static native long nativeCreate2(float x, float y, float radius,
123            int color0, int color1, int tileMode);
124}
125
126