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 public class SweepGradient extends Shader { 27 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 28 private float mCx; 29 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 30 private float mCy; 31 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 32 private float[] mPositions; 33 34 // @ColorInts are replaced by @ColorLongs, but these remain due to @UnsupportedAppUsage. 35 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 36 @ColorInt 37 private int[] mColors; 38 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 39 @ColorInt 40 private int mColor0; 41 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 42 @ColorInt 43 private int mColor1; 44 45 @ColorLong 46 private final long[] mColorLongs; 47 48 /** 49 * A Shader that draws a sweep gradient around a center point. 50 * 51 * @param cx The x-coordinate of the center 52 * @param cy The y-coordinate of the center 53 * @param colors The sRGB colors to be distributed between around the center. 54 * There must be at least 2 colors in the array. 55 * @param positions May be NULL. The relative position of 56 * each corresponding color in the colors array, beginning 57 * with 0 and ending with 1.0. If the values are not 58 * monotonic, the drawing may produce unexpected results. 59 * If positions is NULL, then the colors are automatically 60 * spaced evenly. 61 */ SweepGradient(float cx, float cy, @NonNull @ColorInt int[] colors, @Nullable float[] positions)62 public SweepGradient(float cx, float cy, @NonNull @ColorInt int[] colors, 63 @Nullable float[] positions) { 64 this(cx, cy, convertColors(colors), positions, ColorSpace.get(ColorSpace.Named.SRGB)); 65 } 66 67 /** 68 * A Shader that draws a sweep gradient around a center point. 69 * 70 * @param cx The x-coordinate of the center 71 * @param cy The y-coordinate of the center 72 * @param colors The colors to be distributed between around the center. 73 * There must be at least 2 colors in the array. 74 * @param positions May be NULL. The relative position of 75 * each corresponding color in the colors array, beginning 76 * with 0 and ending with 1.0. If the values are not 77 * monotonic, the drawing may produce unexpected results. 78 * If positions is NULL, then the colors are automatically 79 * spaced evenly. 80 * @throws IllegalArgumentException if there are less than two colors, the colors do 81 * not share the same {@link ColorSpace} or do not use a valid one, or {@code positions} 82 * is not {@code null} and has a different length from {@code colors}. 83 */ SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors, @Nullable float[] positions)84 public SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors, 85 @Nullable float[] positions) { 86 this(cx, cy, colors.clone(), positions, detectColorSpace(colors)); 87 } 88 89 /** 90 * Base constructor. Assumes @param colors is a copy that this object can hold onto, 91 * and all colors share @param colorSpace. 92 */ SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors, @Nullable float[] positions, ColorSpace colorSpace)93 private SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors, 94 @Nullable float[] positions, ColorSpace colorSpace) { 95 super(colorSpace); 96 97 if (positions != null && colors.length != positions.length) { 98 throw new IllegalArgumentException( 99 "color and position arrays must be of equal length"); 100 } 101 mCx = cx; 102 mCy = cy; 103 mColorLongs = colors; 104 mPositions = positions != null ? positions.clone() : null; 105 } 106 107 /** 108 * A Shader that draws a sweep gradient around a center point. 109 * 110 * @param cx The x-coordinate of the center 111 * @param cy The y-coordinate of the center 112 * @param color0 The sRGB color to use at the start of the sweep 113 * @param color1 The sRGB color to use at the end of the sweep 114 */ SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1)115 public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) { 116 this(cx, cy, Color.pack(color0), Color.pack(color1)); 117 } 118 119 /** 120 * A Shader that draws a sweep gradient around a center point. 121 * 122 * @param cx The x-coordinate of the center 123 * @param cy The y-coordinate of the center 124 * @param color0 The color to use at the start of the sweep 125 * @param color1 The color to use at the end of the sweep 126 * 127 * @throws IllegalArgumentException if the colors do 128 * not share the same {@link ColorSpace} or do not use a valid one. 129 */ SweepGradient(float cx, float cy, @ColorLong long color0, @ColorLong long color1)130 public SweepGradient(float cx, float cy, @ColorLong long color0, @ColorLong long color1) { 131 this(cx, cy, new long[] {color0, color1}, null); 132 } 133 134 /** @hide */ 135 @Override createNativeInstance(long nativeMatrix, boolean filterFromPaint)136 protected long createNativeInstance(long nativeMatrix, boolean filterFromPaint) { 137 return nativeCreate(nativeMatrix, mCx, mCy, mColorLongs, mPositions, 138 colorSpace().getNativeInstance()); 139 } 140 nativeCreate(long matrix, float x, float y, long[] colors, float[] positions, long colorSpaceHandle)141 private static native long nativeCreate(long matrix, float x, float y, 142 long[] colors, float[] positions, long colorSpaceHandle); 143 } 144 145