RadialGradient.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.NonNull; 20import android.annotation.Nullable; 21import android.annotation.ColorInt; 22 23public class RadialGradient extends Shader { 24 25 private static final int TYPE_COLORS_AND_POSITIONS = 1; 26 private static final int TYPE_COLOR_CENTER_AND_COLOR_EDGE = 2; 27 28 /** 29 * Type of the RadialGradient: can be either TYPE_COLORS_AND_POSITIONS or 30 * TYPE_COLOR_CENTER_AND_COLOR_EDGE. 31 */ 32 private int mType; 33 34 private float mX; 35 private float mY; 36 private float mRadius; 37 private int[] mColors; 38 private float[] mPositions; 39 private int mCenterColor; 40 private int mEdgeColor; 41 42 private TileMode mTileMode; 43 44 /** 45 * Create a shader that draws a radial gradient given the center and radius. 46 * 47 * @param centerX The x-coordinate of the center of the radius 48 * @param centerY The y-coordinate of the center of the radius 49 * @param radius Must be positive. The radius of the circle for this gradient. 50 * @param colors The colors to be distributed between the center and edge of the circle 51 * @param stops May be <code>null</code>. Valid values are between <code>0.0f</code> and 52 * <code>1.0f</code>. The relative position of each corresponding color in 53 * the colors array. If <code>null</code>, colors are distributed evenly 54 * between the center and edge of the circle. 55 * @param tileMode The Shader tiling mode 56 */ 57 public RadialGradient(float centerX, float centerY, float radius, 58 @NonNull @ColorInt int colors[], @Nullable float stops[], 59 @NonNull TileMode tileMode) { 60 set(centerX, centerY, radius, colors, stops, tileMode); 61 } 62 63 /** 64 * Create a shader that draws a radial gradient given the center and radius. 65 * 66 * @param centerX The x-coordinate of the center of the radius 67 * @param centerY The y-coordinate of the center of the radius 68 * @param radius Must be positive. The radius of the circle for this gradient 69 * @param centerColor The color at the center of the circle. 70 * @param edgeColor The color at the edge of the circle. 71 * @param tileMode The Shader tiling mode 72 */ 73 public RadialGradient(float centerX, float centerY, float radius, 74 @ColorInt int centerColor, @ColorInt int edgeColor, @NonNull TileMode tileMode) { 75 set(centerX, centerY, radius, centerColor, edgeColor, tileMode); 76 } 77 78 /** 79 * Reinitialize the shader. 80 * 81 * @param centerX The x-coordinate of the center of the radius 82 * @param centerY The y-coordinate of the center of the radius 83 * @param radius Must be positive. The radius of the circle for this gradient. 84 * @param colors The colors to be distributed between the center and edge of the circle 85 * @param stops May be <code>null</code>. Valid values are between <code>0.0f</code> and 86 * <code>1.0f</code>. The relative position of each corresponding color in 87 * the colors array. If <code>null</code>, colors are distributed evenly 88 * between the center and edge of the circle. 89 * @param tileMode The Shader tiling mode 90 */ 91 public void set(float centerX, float centerY, float radius, 92 @NonNull @ColorInt int colors[], @Nullable float stops[], @NonNull TileMode tileMode) { 93 if (radius <= 0) { 94 throw new IllegalArgumentException("radius must be > 0"); 95 } 96 if (colors.length < 2) { 97 throw new IllegalArgumentException("needs >= 2 number of colors"); 98 } 99 if (stops != null && colors.length != stops.length) { 100 throw new IllegalArgumentException("color and position arrays must be of equal length"); 101 } 102 discardNativeInstance(); 103 mType = TYPE_COLORS_AND_POSITIONS; 104 mX = centerX; 105 mY = centerY; 106 mRadius = radius; 107 mColors = colors.clone(); 108 mPositions = stops != null ? stops.clone() : null; 109 mTileMode = tileMode; 110 } 111 112 /** 113 * Reinitialize the shader. 114 * 115 * @param centerX The x-coordinate of the center of the radius 116 * @param centerY The y-coordinate of the center of the radius 117 * @param radius Must be positive. The radius of the circle for this gradient 118 * @param centerColor The color at the center of the circle. 119 * @param edgeColor The color at the edge of the circle. 120 * @param tileMode The Shader tiling mode 121 */ 122 public void set(float centerX, float centerY, float radius, 123 @ColorInt int centerColor, @ColorInt int edgeColor, @NonNull TileMode tileMode) { 124 if (radius <= 0) { 125 throw new IllegalArgumentException("radius must be > 0"); 126 } 127 discardNativeInstance(); 128 mType = TYPE_COLOR_CENTER_AND_COLOR_EDGE; 129 mX = centerX; 130 mY = centerY; 131 mRadius = radius; 132 mCenterColor = centerColor; 133 mEdgeColor = edgeColor; 134 mTileMode = tileMode; 135 } 136 137 @Override 138 long createNativeInstance(long nativeMatrix) { 139 if (mType == TYPE_COLORS_AND_POSITIONS) { 140 return nativeCreate1(nativeMatrix, mX, mY, mRadius, 141 mColors, mPositions, mTileMode.nativeInt); 142 } else { // TYPE_COLOR_CENTER_AND_COLOR_EDGE 143 return nativeCreate2(nativeMatrix, mX, mY, mRadius, 144 mCenterColor, mEdgeColor, mTileMode.nativeInt); 145 } 146 } 147 148 /** 149 * @hide 150 */ 151 @Override 152 protected Shader copy() { 153 final RadialGradient copy; 154 if (mType == TYPE_COLORS_AND_POSITIONS) { 155 copy = new RadialGradient(mX, mY, mRadius, mColors.clone(), 156 mPositions != null ? mPositions.clone() : null, mTileMode); 157 } else { // TYPE_COLOR_CENTER_AND_COLOR_EDGE 158 copy = new RadialGradient(mX, mY, mRadius, mCenterColor, mEdgeColor, mTileMode); 159 } 160 copyLocalMatrix(copy); 161 return copy; 162 } 163 164 private static native long nativeCreate1(long matrix, float x, float y, float radius, 165 int colors[], float positions[], int tileMode); 166 private static native long nativeCreate2(long matrix, float x, float y, float radius, 167 int color0, int color1, int tileMode); 168} 169 170