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 LinearGradient extends Shader { 20e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 21e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private static final int TYPE_COLORS_AND_POSITIONS = 1; 22e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private static final int TYPE_COLOR_START_AND_COLOR_END = 2; 23e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 24e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio /** 25e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio * Type of the LinearGradient: can be either TYPE_COLORS_AND_POSITIONS or 26e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio * TYPE_COLOR_START_AND_COLOR_END. 27e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio */ 28e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private int mType; 29e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 30e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float mX0; 31e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float mY0; 32e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float mX1; 33e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float mY1; 34e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private int[] mColors; 35e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private float[] mPositions; 36e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private int mColor0; 37e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private int mColor1; 38e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 39e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio private TileMode mTileMode; 40e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 418a985d24ce9a38f40ed88fecbdcd0e75e3a68f44John Spurlock /** Create a shader that draws a linear gradient along a line. 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param x0 The x-coordinate for the start of the gradient line 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param y0 The y-coordinate for the start of the gradient line 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param x1 The x-coordinate for the end of the gradient line 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param y1 The y-coordinate for the end of the gradient line 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param colors The colors to be distributed along the gradient line 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param positions May be null. The relative positions [0..1] of 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project each corresponding color in the colors array. If this is null, 499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project the the colors are distributed evenly along the gradient line. 509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param tile The Shader tiling mode 518a985d24ce9a38f40ed88fecbdcd0e75e3a68f44John Spurlock */ 528a985d24ce9a38f40ed88fecbdcd0e75e3a68f44John Spurlock public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[], 538bd9698b66d2e1219931ca1be5f81efaba3ed95aRomain Guy TileMode tile) { 549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (colors.length < 2) { 559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("needs >= 2 number of colors"); 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (positions != null && colors.length != positions.length) { 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("color and position arrays must be of equal length"); 599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 60e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mType = TYPE_COLORS_AND_POSITIONS; 61e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mX0 = x0; 62e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mY0 = y0; 63e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mX1 = x1; 64e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mY1 = y1; 65e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mColors = colors; 66e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mPositions = positions; 67e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mTileMode = tile; 68866cf65cc3c53f67836c9157d5c661adfdbd25e1Leon Scroggins III init(nativeCreate1(x0, y0, x1, y1, colors, positions, tile.nativeInt)); 699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 718a985d24ce9a38f40ed88fecbdcd0e75e3a68f44John Spurlock /** Create a shader that draws a linear gradient along a line. 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param x0 The x-coordinate for the start of the gradient line 739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param y0 The y-coordinate for the start of the gradient line 749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param x1 The x-coordinate for the end of the gradient line 759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param y1 The y-coordinate for the end of the gradient line 769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param color0 The color at the start of the gradient line. 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param color1 The color at the end of the gradient line. 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @param tile The Shader tiling mode 798a985d24ce9a38f40ed88fecbdcd0e75e3a68f44John Spurlock */ 808a985d24ce9a38f40ed88fecbdcd0e75e3a68f44John Spurlock public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, 818bd9698b66d2e1219931ca1be5f81efaba3ed95aRomain Guy TileMode tile) { 82e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mType = TYPE_COLOR_START_AND_COLOR_END; 83e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mX0 = x0; 84e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mY0 = y0; 85e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mX1 = x1; 86e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mY1 = y1; 87e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mColor0 = color0; 88e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mColor1 = color1; 89e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio mTileMode = tile; 90866cf65cc3c53f67836c9157d5c661adfdbd25e1Leon Scroggins III init(nativeCreate2(x0, y0, x1, y1, color0, color1, tile.nativeInt)); 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 93e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio /** 94e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio * @hide 95e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio */ 96e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio @Override 97e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio protected Shader copy() { 98e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio final LinearGradient copy; 99e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio switch (mType) { 100e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio case TYPE_COLORS_AND_POSITIONS: 1019622adf6e8028aebd57213371c4f2c6c26fc63e5Romain Guy copy = new LinearGradient(mX0, mY0, mX1, mY1, mColors.clone(), 1029622adf6e8028aebd57213371c4f2c6c26fc63e5Romain Guy mPositions != null ? mPositions.clone() : null, mTileMode); 103e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio break; 104e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio case TYPE_COLOR_START_AND_COLOR_END: 105e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio copy = new LinearGradient(mX0, mY0, mX1, mY1, mColor0, mColor1, mTileMode); 106e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio break; 107e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio default: 108e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio throw new IllegalArgumentException("LinearGradient should be created with either " + 109e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio "colors and positions or start color and end color"); 110e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio } 111e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio copyLocalMatrix(copy); 112e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio return copy; 113e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio } 114e3c526f4f603e83c5fa8b9e399506b085f5648b7Fabrice Di Meglio 11536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private native long nativeCreate1(float x0, float y0, float x1, float y1, 1167fac2e18339f765320d759e8d4c090f92431959eRomain Guy int colors[], float positions[], int tileMode); 1178a985d24ce9a38f40ed88fecbdcd0e75e3a68f44John Spurlock private native long nativeCreate2(float x0, float y0, float x1, float y1, 1187fac2e18339f765320d759e8d4c090f92431959eRomain Guy int color0, int color1, int tileMode); 1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 120