| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.graphics; |
| 18 | |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | public class RadialGradient extends Shader { |
| 23 | |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 24 | 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 Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 38 | private int mCenterColor; |
| 39 | private int mEdgeColor; |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 40 | |
| 41 | private TileMode mTileMode; |
| 42 | |
| John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 43 | /** Create a shader that draws a radial gradient given the center and radius. |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 44 | @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | @param colors The colors to be distributed between the center and edge of the circle |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 48 | @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 Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 53 | */ |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 54 | public RadialGradient(float centerX, float centerY, float radius, |
| 55 | @NonNull int colors[], @Nullable float stops[], @NonNull TileMode tileMode) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | 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 Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 62 | if (stops != null && colors.length != stops.length) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | throw new IllegalArgumentException("color and position arrays must be of equal length"); |
| 64 | } |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 65 | mType = TYPE_COLORS_AND_POSITIONS; |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 66 | mX = centerX; |
| 67 | mY = centerY; |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 68 | mRadius = radius; |
| 69 | mColors = colors; |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 70 | mPositions = stops; |
| 71 | mTileMode = tileMode; |
| 72 | init(nativeCreate1(centerX, centerY, radius, colors, stops, tileMode.nativeInt)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 75 | /** Create a shader that draws a radial gradient given the center and radius. |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 76 | @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 Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 82 | */ |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 83 | public RadialGradient(float centerX, float centerY, float radius, |
| 84 | int centerColor, int edgeColor, @NonNull TileMode tileMode) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | if (radius <= 0) { |
| 86 | throw new IllegalArgumentException("radius must be > 0"); |
| 87 | } |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 88 | mType = TYPE_COLOR_CENTER_AND_COLOR_EDGE; |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 89 | mX = centerX; |
| 90 | mY = centerY; |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 91 | mRadius = radius; |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 92 | mCenterColor = centerColor; |
| 93 | mEdgeColor = edgeColor; |
| 94 | mTileMode = tileMode; |
| 95 | init(nativeCreate2(centerX, centerY, radius, centerColor, edgeColor, tileMode.nativeInt)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 98 | /** |
| 99 | * @hide |
| 100 | */ |
| 101 | @Override |
| 102 | protected Shader copy() { |
| 103 | final RadialGradient copy; |
| 104 | switch (mType) { |
| 105 | case TYPE_COLORS_AND_POSITIONS: |
| Romain Guy | 9622adf | 2013-09-03 16:08:00 -0700 | [diff] [blame] | 106 | copy = new RadialGradient(mX, mY, mRadius, mColors.clone(), |
| 107 | mPositions != null ? mPositions.clone() : null, mTileMode); |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 108 | break; |
| 109 | case TYPE_COLOR_CENTER_AND_COLOR_EDGE: |
| Chris Craik | af046ab | 2014-08-06 11:11:41 -0700 | [diff] [blame] | 110 | copy = new RadialGradient(mX, mY, mRadius, mCenterColor, mEdgeColor, mTileMode); |
| Fabrice Di Meglio | e3c526f | 2013-07-30 18:58:19 -0700 | [diff] [blame] | 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 | |
| Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 120 | private static native long nativeCreate1(float x, float y, float radius, |
| Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 121 | int colors[], float positions[], int tileMode); |
| John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 122 | private static native long nativeCreate2(float x, float y, float radius, |
| Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 123 | int color0, int color1, int tileMode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | } |
| 125 | |