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 native_instance = nativeCreate1(cx, cy, colors, positions); 66 native_shader = nativePostCreate1(native_instance, cx, cy, colors, positions); 67 } 68 69 /** 70 * A subclass of Shader that draws a sweep gradient around a center point. 71 * 72 * @param cx The x-coordinate of the center 73 * @param cy The y-coordinate of the center 74 * @param color0 The color to use at the start of the sweep 75 * @param color1 The color to use at the end of the sweep 76 */ 77 public SweepGradient(float cx, float cy, int color0, int color1) { 78 mType = TYPE_COLOR_START_AND_COLOR_END; 79 mCx = cx; 80 mCy = cy; 81 mColor0 = color0; 82 mColor1 = color1; 83 native_instance = nativeCreate2(cx, cy, color0, color1); 84 native_shader = nativePostCreate2(native_instance, cx, cy, color0, color1); 85 } 86 87 /** 88 * @hide 89 */ 90 @Override 91 protected Shader copy() { 92 final SweepGradient copy; 93 switch (mType) { 94 case TYPE_COLORS_AND_POSITIONS: 95 copy = new SweepGradient(mCx, mCy, mColors.clone(), 96 mPositions != null ? mPositions.clone() : null); 97 break; 98 case TYPE_COLOR_START_AND_COLOR_END: 99 copy = new SweepGradient(mCx, mCy, mColor0, mColor1); 100 break; 101 default: 102 throw new IllegalArgumentException("SweepGradient should be created with either " + 103 "colors and positions or start color and end color"); 104 } 105 copyLocalMatrix(copy); 106 return copy; 107 } 108 109 private static native int nativeCreate1(float x, float y, int colors[], float positions[]); 110 private static native int nativeCreate2(float x, float y, int color0, int color1); 111 112 private static native int nativePostCreate1(int native_shader, float cx, float cy, 113 int[] colors, float[] positions); 114 private static native int nativePostCreate2(int native_shader, float cx, float cy, 115 int color0, int color1); 116} 117 118