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