19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License. 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License. 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.graphics; 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class RadialGradient extends Shader { 209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 21e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private static final int TYPE_COLORS_AND_POSITIONS = 1; 22e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private static final int TYPE_COLOR_CENTER_AND_COLOR_EDGE = 2; 23e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 24e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio /** 25e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio * Type of the RadialGradient: can be either TYPE_COLORS_AND_POSITIONS or 26e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio * TYPE_COLOR_CENTER_AND_COLOR_EDGE. 27e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio */ 28e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private int mType; 29e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 30e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float mX; 31e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float mY; 32e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float mRadius; 33e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private int[] mColors; 34e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float[] mPositions; 35e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private int mColor0; 36e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private int mColor1; 37e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 38e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private TileMode mTileMode; 39e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** Create a shader that draws a radial gradient given the center and radius. 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param x The x-coordinate of the center of the radius 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param y The y-coordinate of the center of the radius 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param radius Must be positive. The radius of the circle for this gradient 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param colors The colors to be distributed between the center and edge of the circle 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param positions May be NULL. The relative position of 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project each corresponding color in the colors array. If this is NULL, 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project the the colors are distributed evenly between the center and edge of the circle. 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param tile The Shader tiling mode 499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public RadialGradient(float x, float y, float radius, 519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int colors[], float positions[], TileMode tile) { 529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (radius <= 0) { 539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("radius must be > 0"); 549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (colors.length < 2) { 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("needs >= 2 number of colors"); 579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (positions != null && colors.length != positions.length) { 599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("color and position arrays must be of equal length"); 609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 61e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mType = TYPE_COLORS_AND_POSITIONS; 62e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mX = x; 63e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mY = y; 64e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mRadius = radius; 65e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mColors = colors; 66e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mPositions = positions; 67e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mTileMode = tile; 689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project native_instance = nativeCreate1(x, y, radius, colors, positions, tile.nativeInt); 69ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy native_shader = nativePostCreate1(native_instance, x, y, radius, colors, positions, 70ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy tile.nativeInt); 719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** Create a shader that draws a radial gradient given the center and radius. 749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param x The x-coordinate of the center of the radius 759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param y The y-coordinate of the center of the radius 769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param radius Must be positive. The radius of the circle for this gradient 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param color0 The color at the center of the circle. 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param color1 The color at the edge of the circle. 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param tile The Shader tiling mode 809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public RadialGradient(float x, float y, float radius, 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int color0, int color1, TileMode tile) { 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (radius <= 0) { 849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("radius must be > 0"); 859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 86e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mType = TYPE_COLOR_CENTER_AND_COLOR_EDGE; 87e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mX = x; 88e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mY = y; 89e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mRadius = radius; 90e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mColor0 = color0; 91e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mColor1 = color1; 92e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mTileMode = tile; 939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project native_instance = nativeCreate2(x, y, radius, color0, color1, tile.nativeInt); 94ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy native_shader = nativePostCreate2(native_instance, x, y, radius, color0, color1, 95ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy tile.nativeInt); 969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 98e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio /** 99e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio * @hide 100e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio */ 101e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio @Override 102e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio protected Shader copy() { 103e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio final RadialGradient copy; 104e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio switch (mType) { 105e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio case TYPE_COLORS_AND_POSITIONS: 1069622adf6e8028aebd57213371c4f2c6c26fc63e5Romain Guy copy = new RadialGradient(mX, mY, mRadius, mColors.clone(), 1079622adf6e8028aebd57213371c4f2c6c26fc63e5Romain Guy mPositions != null ? mPositions.clone() : null, mTileMode); 108e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio break; 109e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio case TYPE_COLOR_CENTER_AND_COLOR_EDGE: 110e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio copy = new RadialGradient(mX, mY, mRadius, mColor0, mColor1, mTileMode); 111e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio break; 112e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio default: 113e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio throw new IllegalArgumentException("RadialGradient should be created with either " + 114e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio "colors and positions or center color and edge color"); 115e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio } 116e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio copyLocalMatrix(copy); 117e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio return copy; 118e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio } 119e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 120e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private static native int nativeCreate1(float x, float y, float radius, 121ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy int colors[], float positions[], int tileMode); 1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static native int nativeCreate2(float x, float y, float radius, 123ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy int color0, int color1, int tileMode); 124ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy 125ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy private static native int nativePostCreate1(int native_shader, float x, float y, float radius, 126ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy int colors[], float positions[], int tileMode); 127ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy private static native int nativePostCreate2(int native_shader, float x, float y, float radius, 128ddb80bebb0776e6d852aab6e8bba5d5591847a55Romain Guy int color0, int color1, int tileMode); 1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 131