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 SweepGradient extends Shader { 20 21 private static final int TYPE_COLORS_AND_POSITIONS = 1; 22 private static final int TYPE_COLOR_START_AND_COLOR_END = 2; 23 24 /** 25 * Type of the LinearGradient: can be either TYPE_COLORS_AND_POSITIONS or 26 * TYPE_COLOR_START_AND_COLOR_END. 27 */ 28 private int mType; 29 30 private float mCx; 31 private float mCy; 32 private int[] mColors; 33 private float[] mPositions; 34 private int mColor0; 35 private int mColor1; 36 37 /** 38 * A subclass of Shader that draws a sweep gradient around a center point. 39 * 40 * @param cx The x-coordinate of the center 41 * @param cy The y-coordinate of the center 42 * @param colors The colors to be distributed between around the center. 43 * There must be at least 2 colors in the array. 44 * @param positions May be NULL. The relative position of 45 * each corresponding color in the colors array, beginning 46 * with 0 and ending with 1.0. If the values are not 47 * monotonic, the drawing may produce unexpected results. 48 * If positions is NULL, then the colors are automatically 49 * spaced evenly. 50 */ 51 public SweepGradient(float cx, float cy, 52 int colors[], float positions[]) { 53 if (colors.length < 2) { 54 throw new IllegalArgumentException("needs >= 2 number of colors"); 55 } 56 if (positions != null && colors.length != positions.length) { 57 throw new IllegalArgumentException( 58 "color and position arrays must be of equal length"); 59 } 60 mType = TYPE_COLORS_AND_POSITIONS; 61 mCx = cx; 62 mCy = cy; 63 mColors = colors; 64 mPositions = positions; 65 init(nativeCreate1(cx, cy, colors, positions)); 66 } 67 68 /** 69 * A subclass of Shader that draws a sweep gradient around a center point. 70 * 71 * @param cx The x-coordinate of the center 72 * @param cy The y-coordinate of the center 73 * @param color0 The color to use at the start of the sweep 74 * @param color1 The color to use at the end of the sweep 75 */ 76 public SweepGradient(float cx, float cy, int color0, int color1) { 77 mType = TYPE_COLOR_START_AND_COLOR_END; 78 mCx = cx; 79 mCy = cy; 80 mColor0 = color0; 81 mColor1 = color1; 82 init(nativeCreate2(cx, cy, color0, color1)); 83 } 84 85 /** 86 * @hide 87 */ 88 @Override 89 protected Shader copy() { 90 final SweepGradient copy; 91 switch (mType) { 92 case TYPE_COLORS_AND_POSITIONS: 93 copy = new SweepGradient(mCx, mCy, mColors.clone(), 94 mPositions != null ? mPositions.clone() : null); 95 break; 96 case TYPE_COLOR_START_AND_COLOR_END: 97 copy = new SweepGradient(mCx, mCy, mColor0, mColor1); 98 break; 99 default: 100 throw new IllegalArgumentException("SweepGradient should be created with either " + 101 "colors and positions or start color and end color"); 102 } 103 copyLocalMatrix(copy); 104 return copy; 105 } 106 107 private static native long nativeCreate1(float x, float y, int colors[], float positions[]); 108 private static native long nativeCreate2(float x, float y, int color0, int color1); 109} 110 111