• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ColorLong;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.compat.annotation.UnsupportedAppUsage;
24 import android.os.Build;
25 
26 
27 @android.ravenwood.annotation.RavenwoodKeepWholeClass
28 public class LinearGradient extends Shader {
29     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
30     private float mX0;
31     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
32     private float mY0;
33     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
34     private float mX1;
35     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
36     private float mY1;
37     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
38     private float[] mPositions;
39     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
40     private TileMode mTileMode;
41 
42     // @ColorInts are replaced by @ColorLongs, but these remain due to @UnsupportedAppUsage.
43     @UnsupportedAppUsage
44     @ColorInt
45     private int[] mColors;
46     @UnsupportedAppUsage
47     @ColorInt
48     private int mColor0;
49     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
50     @ColorInt
51     private int mColor1;
52 
53     @ColorLong
54     private final long[] mColorLongs;
55 
56 
57     /**
58      * Create a shader that draws a linear gradient along a line.
59      *
60      * @param x0           The x-coordinate for the start of the gradient line
61      * @param y0           The y-coordinate for the start of the gradient line
62      * @param x1           The x-coordinate for the end of the gradient line
63      * @param y1           The y-coordinate for the end of the gradient line
64      * @param colors       The sRGB colors to be distributed along the gradient line
65      * @param positions    May be null. The relative positions [0..1] of
66      *                     each corresponding color in the colors array. If this is null,
67      *                     the colors are distributed evenly along the gradient line.
68      * @param tile         The Shader tiling mode
69      */
LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors, @Nullable float[] positions, @NonNull TileMode tile)70     public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors,
71             @Nullable float[] positions, @NonNull TileMode tile) {
72         this(x0, y0, x1, y1, convertColors(colors), positions, tile,
73                 ColorSpace.get(ColorSpace.Named.SRGB));
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 colors       The colors to be distributed along the gradient line
84      * @param positions    May be null. The relative positions [0..1] of
85      *                     each corresponding color in the colors array. If this is null,
86      *                     the colors are distributed evenly along the gradient line.
87      * @param tile         The Shader tiling mode
88      *
89      * @throws IllegalArgumentException if there are less than two colors, the colors do
90      *      not share the same {@link ColorSpace} or do not use a valid one, or {@code positions}
91      *      is not {@code null} and has a different length from {@code colors}.
92      */
LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorLong long[] colors, @Nullable float[] positions, @NonNull TileMode tile)93     public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorLong long[] colors,
94             @Nullable float[] positions, @NonNull TileMode tile) {
95         this(x0, y0, x1, y1, colors.clone(), positions, tile, detectColorSpace(colors));
96     }
97 
98     /**
99      * Base constructor. Assumes @param colors is a copy that this object can hold onto,
100      * and all colors share @param colorSpace.
101      */
LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorLong long[] colors, @Nullable float[] positions, @NonNull TileMode tile, @NonNull ColorSpace colorSpace)102     private LinearGradient(float x0, float y0, float x1, float y1,
103             @NonNull @ColorLong long[] colors, @Nullable float[] positions, @NonNull TileMode tile,
104             @NonNull ColorSpace colorSpace) {
105         super(colorSpace);
106 
107         if (positions != null && colors.length != positions.length) {
108             throw new IllegalArgumentException("color and position arrays must be of equal length");
109         }
110         mX0 = x0;
111         mY0 = y0;
112         mX1 = x1;
113         mY1 = y1;
114         mColorLongs = colors;
115         mPositions = positions != null ? positions.clone() : null;
116         mTileMode = tile;
117     }
118 
119     /**
120      * Create a shader that draws a linear gradient along a line.
121      *
122      * @param x0       The x-coordinate for the start of the gradient line
123      * @param y0       The y-coordinate for the start of the gradient line
124      * @param x1       The x-coordinate for the end of the gradient line
125      * @param y1       The y-coordinate for the end of the gradient line
126      * @param color0   The sRGB color at the start of the gradient line.
127      * @param color1   The sRGB color at the end of the gradient line.
128      * @param tile     The Shader tiling mode
129      */
LinearGradient(float x0, float y0, float x1, float y1, @ColorInt int color0, @ColorInt int color1, @NonNull TileMode tile)130     public LinearGradient(float x0, float y0, float x1, float y1,
131             @ColorInt int color0, @ColorInt int color1,
132             @NonNull TileMode tile) {
133         this(x0, y0, x1, y1, Color.pack(color0), Color.pack(color1), tile);
134     }
135 
136     /**
137      * Create a shader that draws a linear gradient along a line.
138      *
139      * @param x0       The x-coordinate for the start of the gradient line
140      * @param y0       The y-coordinate for the start of the gradient line
141      * @param x1       The x-coordinate for the end of the gradient line
142      * @param y1       The y-coordinate for the end of the gradient line
143      * @param color0   The color at the start of the gradient line.
144      * @param color1   The color at the end of the gradient line.
145      * @param tile     The Shader tiling mode
146      *
147      * @throws IllegalArgumentException if the colors do
148      *      not share the same {@link ColorSpace} or do not use a valid one.
149      */
LinearGradient(float x0, float y0, float x1, float y1, @ColorLong long color0, @ColorLong long color1, @NonNull TileMode tile)150     public LinearGradient(float x0, float y0, float x1, float y1,
151             @ColorLong long color0, @ColorLong long color1,
152             @NonNull TileMode tile) {
153         this(x0, y0, x1, y1, new long[] {color0, color1}, null, tile);
154     }
155 
156     /** @hide */
157     @Override
createNativeInstance(long nativeMatrix, boolean filterFromPaint)158     protected long createNativeInstance(long nativeMatrix, boolean filterFromPaint) {
159         return nativeCreate(nativeMatrix, mX0, mY0, mX1, mY1,
160                 mColorLongs, mPositions, mTileMode.nativeInt,
161                 colorSpace().getNativeInstance());
162     }
163 
nativeCreate(long matrix, float x0, float y0, float x1, float y1, long[] colors, float[] positions, int tileMode, long colorSpaceHandle)164     private native long nativeCreate(long matrix, float x0, float y0, float x1, float y1,
165             long[] colors, float[] positions, int tileMode, long colorSpaceHandle);
166 }
167