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 
19 import android.annotation.ColorInt;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 
23 public class LinearGradient extends Shader {
24 
25     private static final int TYPE_COLORS_AND_POSITIONS = 1;
26     private static final int TYPE_COLOR_START_AND_COLOR_END = 2;
27 
28     /**
29      * Type of the LinearGradient: can be either TYPE_COLORS_AND_POSITIONS or
30      * TYPE_COLOR_START_AND_COLOR_END.
31      */
32     private int mType;
33 
34     private float mX0;
35     private float mY0;
36     private float mX1;
37     private float mY1;
38     private int[] mColors;
39     private float[] mPositions;
40     private int mColor0;
41     private int mColor1;
42 
43     private TileMode mTileMode;
44 
45     /**
46      * Create a shader that draws a linear gradient along a line.
47      *
48      * @param x0           The x-coordinate for the start of the gradient line
49      * @param y0           The y-coordinate for the start of the gradient line
50      * @param x1           The x-coordinate for the end of the gradient line
51      * @param y1           The y-coordinate for the end of the gradient line
52      * @param colors       The colors to be distributed along the gradient line
53      * @param positions    May be null. The relative positions [0..1] of
54      *                     each corresponding color in the colors array. If this is null,
55      *                     the the colors are distributed evenly along the gradient line.
56      * @param tile         The Shader tiling mode
57     */
LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[], @Nullable float positions[], @NonNull TileMode tile)58     public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[],
59             @Nullable float positions[], @NonNull TileMode tile) {
60         if (colors.length < 2) {
61             throw new IllegalArgumentException("needs >= 2 number of colors");
62         }
63         if (positions != null && colors.length != positions.length) {
64             throw new IllegalArgumentException("color and position arrays must be of equal length");
65         }
66         mType = TYPE_COLORS_AND_POSITIONS;
67         mX0 = x0;
68         mY0 = y0;
69         mX1 = x1;
70         mY1 = y1;
71         mColors = colors.clone();
72         mPositions = positions != null ? positions.clone() : null;
73         mTileMode = tile;
74     }
75 
76     /**
77      * Create a shader that draws a linear gradient along a line.
78      *
79      * @param x0       The x-coordinate for the start of the gradient line
80      * @param y0       The y-coordinate for the start of the gradient line
81      * @param x1       The x-coordinate for the end of the gradient line
82      * @param y1       The y-coordinate for the end of the gradient line
83      * @param color0   The color at the start of the gradient line.
84      * @param color1   The color at the end of the gradient line.
85      * @param tile     The Shader tiling mode
86     */
LinearGradient(float x0, float y0, float x1, float y1, @ColorInt int color0, @ColorInt int color1, @NonNull TileMode tile)87     public LinearGradient(float x0, float y0, float x1, float y1,
88             @ColorInt int color0, @ColorInt int color1,
89             @NonNull TileMode tile) {
90         mType = TYPE_COLOR_START_AND_COLOR_END;
91         mX0 = x0;
92         mY0 = y0;
93         mX1 = x1;
94         mY1 = y1;
95         mColor0 = color0;
96         mColor1 = color1;
97         mColors = null;
98         mPositions = null;
99         mTileMode = tile;
100     }
101 
102     @Override
createNativeInstance(long nativeMatrix)103     long createNativeInstance(long nativeMatrix) {
104         if (mType == TYPE_COLORS_AND_POSITIONS) {
105             return nativeCreate1(nativeMatrix, mX0, mY0, mX1, mY1,
106                     mColors, mPositions, mTileMode.nativeInt);
107         } else { // TYPE_COLOR_START_AND_COLOR_END
108             return nativeCreate2(nativeMatrix, mX0, mY0, mX1, mY1,
109                     mColor0, mColor1, mTileMode.nativeInt);
110         }
111     }
112 
113     /**
114      * @hide
115      */
116     @Override
copy()117     protected Shader copy() {
118         final LinearGradient copy;
119         if (mType == TYPE_COLORS_AND_POSITIONS) {
120             copy = new LinearGradient(mX0, mY0, mX1, mY1, mColors.clone(),
121                     mPositions != null ? mPositions.clone() : null, mTileMode);
122         } else { // TYPE_COLOR_START_AND_COLOR_END
123             copy = new LinearGradient(mX0, mY0, mX1, mY1, mColor0, mColor1, mTileMode);
124         }
125         copyLocalMatrix(copy);
126         return copy;
127     }
128 
nativeCreate1(long matrix, float x0, float y0, float x1, float y1, int colors[], float positions[], int tileMode)129     private native long nativeCreate1(long matrix, float x0, float y0, float x1, float y1,
130             int colors[], float positions[], int tileMode);
nativeCreate2(long matrix, float x0, float y0, float x1, float y1, int color0, int color1, int tileMode)131     private native long nativeCreate2(long matrix, float x0, float y0, float x1, float y1,
132             int color0, int color1, int tileMode);
133 }
134