blob: ade14c64f491255ca85b75d631520bd3d0a96e6e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Chris Craikaf046ab2014-08-06 11:11:41 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022public class RadialGradient extends Shader {
23
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -070024 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;
Chris Craikaf046ab2014-08-06 11:11:41 -070038 private int mCenterColor;
39 private int mEdgeColor;
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -070040
41 private TileMode mTileMode;
42
John Spurlock8a985d22014-02-25 09:40:05 -050043 /** Create a shader that draws a radial gradient given the center and radius.
Chris Craikaf046ab2014-08-06 11:11:41 -070044 @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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 @param colors The colors to be distributed between the center and edge of the circle
Chris Craikaf046ab2014-08-06 11:11:41 -070048 @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
John Spurlock8a985d22014-02-25 09:40:05 -050053 */
Chris Craikaf046ab2014-08-06 11:11:41 -070054 public RadialGradient(float centerX, float centerY, float radius,
55 @NonNull int colors[], @Nullable float stops[], @NonNull TileMode tileMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 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 }
Chris Craikaf046ab2014-08-06 11:11:41 -070062 if (stops != null && colors.length != stops.length) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 throw new IllegalArgumentException("color and position arrays must be of equal length");
64 }
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -070065 mType = TYPE_COLORS_AND_POSITIONS;
Chris Craikaf046ab2014-08-06 11:11:41 -070066 mX = centerX;
67 mY = centerY;
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -070068 mRadius = radius;
69 mColors = colors;
Chris Craikaf046ab2014-08-06 11:11:41 -070070 mPositions = stops;
71 mTileMode = tileMode;
72 init(nativeCreate1(centerX, centerY, radius, colors, stops, tileMode.nativeInt));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 }
74
John Spurlock8a985d22014-02-25 09:40:05 -050075 /** Create a shader that draws a radial gradient given the center and radius.
Chris Craikaf046ab2014-08-06 11:11:41 -070076 @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
John Spurlock8a985d22014-02-25 09:40:05 -050082 */
Chris Craikaf046ab2014-08-06 11:11:41 -070083 public RadialGradient(float centerX, float centerY, float radius,
84 int centerColor, int edgeColor, @NonNull TileMode tileMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 if (radius <= 0) {
86 throw new IllegalArgumentException("radius must be > 0");
87 }
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -070088 mType = TYPE_COLOR_CENTER_AND_COLOR_EDGE;
Chris Craikaf046ab2014-08-06 11:11:41 -070089 mX = centerX;
90 mY = centerY;
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -070091 mRadius = radius;
Chris Craikaf046ab2014-08-06 11:11:41 -070092 mCenterColor = centerColor;
93 mEdgeColor = edgeColor;
94 mTileMode = tileMode;
95 init(nativeCreate2(centerX, centerY, radius, centerColor, edgeColor, tileMode.nativeInt));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 }
97
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -070098 /**
99 * @hide
100 */
101 @Override
102 protected Shader copy() {
103 final RadialGradient copy;
104 switch (mType) {
105 case TYPE_COLORS_AND_POSITIONS:
Romain Guy9622adf2013-09-03 16:08:00 -0700106 copy = new RadialGradient(mX, mY, mRadius, mColors.clone(),
107 mPositions != null ? mPositions.clone() : null, mTileMode);
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -0700108 break;
109 case TYPE_COLOR_CENTER_AND_COLOR_EDGE:
Chris Craikaf046ab2014-08-06 11:11:41 -0700110 copy = new RadialGradient(mX, mY, mRadius, mCenterColor, mEdgeColor, mTileMode);
Fabrice Di Meglioe3c526f2013-07-30 18:58:19 -0700111 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000120 private static native long nativeCreate1(float x, float y, float radius,
Romain Guyddb80be2010-09-20 19:04:33 -0700121 int colors[], float positions[], int tileMode);
John Spurlock8a985d22014-02-25 09:40:05 -0500122 private static native long nativeCreate2(float x, float y, float radius,
Romain Guyddb80be2010-09-20 19:04:33 -0700123 int color0, int color1, int tileMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124}
125