RadialGradient.java revision 8a985d24ce9a38f40ed88fecbdcd0e75e3a68f44
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 19public class RadialGradient extends Shader { 20 21 private static final int TYPE_COLORS_AND_POSITIONS = 1; 22 private static final int TYPE_COLOR_CENTER_AND_COLOR_EDGE = 2; 23 24 /** 25 * Type of the RadialGradient: can be either TYPE_COLORS_AND_POSITIONS or 26 * TYPE_COLOR_CENTER_AND_COLOR_EDGE. 27 */ 28 private int mType; 29 30 private float mX; 31 private float mY; 32 private float mRadius; 33 private int[] mColors; 34 private float[] mPositions; 35 private int mColor0; 36 private int mColor1; 37 38 private TileMode mTileMode; 39 40 /** Create a shader that draws a radial gradient given the center and radius. 41 @param x The x-coordinate of the center of the radius 42 @param y The y-coordinate of the center of the radius 43 @param radius Must be positive. The radius of the circle for this gradient 44 @param colors The colors to be distributed between the center and edge of the circle 45 @param positions May be NULL. The relative position of 46 each corresponding color in the colors array. If this is NULL, 47 the the colors are distributed evenly between the center and edge of the circle. 48 @param tile The Shader tiling mode 49 */ 50 public RadialGradient(float x, float y, float radius, 51 int colors[], float positions[], TileMode tile) { 52 if (radius <= 0) { 53 throw new IllegalArgumentException("radius must be > 0"); 54 } 55 if (colors.length < 2) { 56 throw new IllegalArgumentException("needs >= 2 number of colors"); 57 } 58 if (positions != null && colors.length != positions.length) { 59 throw new IllegalArgumentException("color and position arrays must be of equal length"); 60 } 61 mType = TYPE_COLORS_AND_POSITIONS; 62 mX = x; 63 mY = y; 64 mRadius = radius; 65 mColors = colors; 66 mPositions = positions; 67 mTileMode = tile; 68 native_instance = nativeCreate1(x, y, radius, colors, positions, tile.nativeInt); 69 native_shader = nativePostCreate1(native_instance, x, y, radius, colors, positions, 70 tile.nativeInt); 71 } 72 73 /** Create a shader that draws a radial gradient given the center and radius. 74 @param x The x-coordinate of the center of the radius 75 @param y The y-coordinate of the center of the radius 76 @param radius Must be positive. The radius of the circle for this gradient 77 @param color0 The color at the center of the circle. 78 @param color1 The color at the edge of the circle. 79 @param tile The Shader tiling mode 80 */ 81 public RadialGradient(float x, float y, float radius, 82 int color0, int color1, TileMode tile) { 83 if (radius <= 0) { 84 throw new IllegalArgumentException("radius must be > 0"); 85 } 86 mType = TYPE_COLOR_CENTER_AND_COLOR_EDGE; 87 mX = x; 88 mY = y; 89 mRadius = radius; 90 mColor0 = color0; 91 mColor1 = color1; 92 mTileMode = tile; 93 native_instance = nativeCreate2(x, y, radius, color0, color1, tile.nativeInt); 94 native_shader = nativePostCreate2(native_instance, x, y, radius, color0, color1, 95 tile.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, mColor0, mColor1, 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 private static native long nativePostCreate1(long native_shader, float x, float y, float radius, 126 int colors[], float positions[], int tileMode); 127 private static native long nativePostCreate2(long native_shader, float x, float y, float radius, 128 int color0, int color1, int tileMode); 129} 130 131