Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8bd9698b authored by Romain Guy's avatar Romain Guy
Browse files

Add GradientDrawable.setColors(int[])

This change also updates the documentation to warn the user about which properties
require a call to mutate().

Change-Id: I84717068bf026669e3eef1ae92db665a964fe08a
parent 74d7ca13
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -9083,14 +9083,17 @@ package android.graphics.drawable {
    ctor public GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation, int[]);
    method public void draw(android.graphics.Canvas);
    method public int getOpacity();
    method public android.graphics.drawable.GradientDrawable.Orientation getOrientation();
    method public void setAlpha(int);
    method public void setColor(int);
    method public void setColorFilter(android.graphics.ColorFilter);
    method public void setColors(int[]);
    method public void setCornerRadii(float[]);
    method public void setCornerRadius(float);
    method public void setGradientCenter(float, float);
    method public void setGradientRadius(float);
    method public void setGradientType(int);
    method public void setOrientation(android.graphics.drawable.GradientDrawable.Orientation);
    method public void setShape(int);
    method public void setSize(int, int);
    method public void setStroke(int, int);
+4 −4
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ public class LinearGradient extends Shader {
                            the the colors are distributed evenly along the gradient line.
        @param  tile        The Shader tiling mode
	*/
	public LinearGradient(float x0, float y0, float x1, float y1,
                          int colors[], float positions[], TileMode tile) {
	public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],
            TileMode tile) {
        if (colors.length < 2) {
            throw new IllegalArgumentException("needs >= 2 number of colors");
        }
@@ -50,8 +50,8 @@ public class LinearGradient extends Shader {
        @param  color1  The color at the end of the gradient line.
        @param  tile    The Shader tiling mode
	*/
	public LinearGradient(float x0, float y0, float x1, float y1,
                          int color0, int color1, TileMode tile) {
	public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,
            TileMode tile) {
        native_instance = nativeCreate2(x0, y0, x1, y1, color0, color1, tile.nativeInt);
        native_shader = nativePostCreate2(native_instance, x0, y0, x1, y1, color0, color1,
                tile.nativeInt);
+188 −21
Original line number Diff line number Diff line
@@ -173,9 +173,20 @@ public class GradientDrawable extends Drawable {
    }

    /**
     * Specify radii for each of the 4 corners. For each corner, the array
     * contains 2 values, [X_radius, Y_radius]. The corners are ordered
     * top-left, top-right, bottom-right, bottom-left
     * <p>Specify radii for each of the 4 corners. For each corner, the array
     * contains 2 values, <code>[X_radius, Y_radius]</code>. The corners are ordered
     * top-left, top-right, bottom-right, bottom-left. This property
     * is honored only when the shape is of type {@link #RECTANGLE}.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param radii 4 pairs of X and Y radius for each corner, specified in pixels.
     *              The length of this array must be >= 8
     *
     * @see #mutate()
     * @see #setCornerRadii(float[])
     * @see #setShape(int)
     */
    public void setCornerRadii(float[] radii) {
        mGradientState.setCornerRadii(radii);
@@ -184,8 +195,18 @@ public class GradientDrawable extends Drawable {
    }
    
    /**
     * Specify radius for the corners of the gradient. If this is > 0, then the
     * drawable is drawn in a round-rectangle, rather than a rectangle.
     * <p>Specify radius for the corners of the gradient. If this is > 0, then the
     * drawable is drawn in a round-rectangle, rather than a rectangle. This property
     * is honored only when the shape is of type {@link #RECTANGLE}.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param radius The radius in pixels of the corners of the rectangle shape
     *
     * @see #mutate()
     * @see #setCornerRadii(float[])
     * @see #setShape(int) 
     */
    public void setCornerRadius(float radius) {
        mGradientState.setCornerRadius(radius);
@@ -194,13 +215,37 @@ public class GradientDrawable extends Drawable {
    }

    /**
     * Set the stroke width and color for the drawable. If width is zero,
     * then no stroke is drawn.
     * <p>Set the stroke width and color for the drawable. If width is zero,
     * then no stroke is drawn.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param width The width in pixels of the stroke
     * @param color The color of the stroke
     *
     * @see #mutate()
     * @see #setStroke(int, int, float, float) 
     */
    public void setStroke(int width, int color) {
        setStroke(width, color, 0, 0);
    }

    /**
     * <p>Set the stroke width and color for the drawable. If width is zero,
     * then no stroke is drawn. This method can also be used to dash the stroke.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param width The width in pixels of the stroke
     * @param color The color of the stroke
     * @param dashWidth The length in pixels of the dashes, set to 0 to disable dashes 
     * @param dashGap The gap in pixels between dashes
     *
     * @see #mutate()
     * @see #setStroke(int, int) 
     */
    public void setStroke(int width, int color, float dashWidth, float dashGap) {
        mGradientState.setStroke(width, color, dashWidth, dashGap);

@@ -219,12 +264,36 @@ public class GradientDrawable extends Drawable {
        invalidateSelf();
    }


    /**
     * <p>Sets the size of the shape drawn by this drawable.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param width The width of the shape used by this drawable
     * @param height The height of the shape used by this drawable
     *
     * @see #mutate()
     * @see #setGradientType(int)
     */
    public void setSize(int width, int height) {
        mGradientState.setSize(width, height);
        mPathIsDirty = true;
        invalidateSelf();
    }

    /**
     * <p>Sets the type of shape used to draw the gradient.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param shape The desired shape for this drawable: {@link #LINE},
     *              {@link #OVAL}, {@link #RECTANGLE} or {@link #RING}
     *
     * @see #mutate()
     */
    public void setShape(int shape) {
        mRingPath = null;
        mPathIsDirty = true;
@@ -232,24 +301,73 @@ public class GradientDrawable extends Drawable {
        invalidateSelf();
    }

    /**
     * <p>Sets the type of gradient used by this drawable..</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param gradient The type of the gradient: {@link #LINEAR_GRADIENT},
     *                 {@link #RADIAL_GRADIENT} or {@link #SWEEP_GRADIENT}
     *
     * @see #mutate()
     */
    public void setGradientType(int gradient) {
        mGradientState.setGradientType(gradient);
        mRectIsDirty = true;
        invalidateSelf();
    }

    /**
     * <p>Sets the center location of the gradient. The radius is honored only when 
     * the gradient type is set to {@link #RADIAL_GRADIENT} or {@link #SWEEP_GRADIENT}.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param x The x coordinate of the gradient's center
     * @param y The y coordinate of the gradient's center
     *
     * @see #mutate()
     * @see #setGradientType(int)
     */
    public void setGradientCenter(float x, float y) {
        mGradientState.setGradientCenter(x, y);
        mRectIsDirty = true;
        invalidateSelf();
    }

    /**
     * <p>Sets the radius of the gradient. The radius is honored only when the
     * gradient type is set to {@link #RADIAL_GRADIENT}.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param gradientRadius The radius of the gradient in pixels
     *
     * @see #mutate()
     * @see #setGradientType(int) 
     */
    public void setGradientRadius(float gradientRadius) {
        mGradientState.setGradientRadius(gradientRadius);
        mRectIsDirty = true;
        invalidateSelf();
    }

    /**
     * <p>Sets whether or not this drawable will honor its <code>level</code>
     * property.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param useLevel True if this drawable should honor its level, false otherwise
     *
     * @see #mutate()
     * @see #setLevel(int) 
     * @see #getLevel() 
     */
    public void setUseLevel(boolean useLevel) {
        mGradientState.mUseLevel = useLevel;
        mRectIsDirty = true;
@@ -261,6 +379,47 @@ public class GradientDrawable extends Drawable {
        return alpha * scale >> 8;
    }

    /**
     * Returns the orientation of the gradient defined in this drawable.
     */
    public Orientation getOrientation() {
        return mGradientState.mOrientation;
    }

    /**
     * <p>Changes the orientation of the gradient defined in this drawable.</p>
     * <p><strong>Note</strong>: changing orientation will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing the orientation.</p>
     * 
     * @param orientation The desired orientation (angle) of the gradient
     *                    
     * @see #mutate() 
     */
    public void setOrientation(Orientation orientation) {
        mGradientState.mOrientation = orientation;
        mRectIsDirty = true;
        invalidateSelf();
    }

    /**
     * <p>Sets the colors used to draw the gradient. Each color is specified as an
     * ARGB integer and the array must contain at least 2 colors.</p>
     * <p><strong>Note</strong>: changing orientation will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing the orientation.</p>
     *
     * @param colors 2 or more ARGB colors
     *
     * @see #mutate()
     * @see #setColor(int) 
     */
    public void setColors(int[] colors) {
        mGradientState.setColors(colors);
        mRectIsDirty = true;
        invalidateSelf();
    }

    @Override
    public void draw(Canvas canvas) {
        if (!ensureValidRect()) {
@@ -442,6 +601,17 @@ public class GradientDrawable extends Drawable {
        return ringPath;
    }

    /**
     * <p>Changes this drawbale to use a single color instead of a gradient.</p>
     * <p><strong>Note</strong>: changing orientation will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing the orientation.</p>
     *
     * @param argb The color used to fill the shape
     *
     * @see #mutate()
     * @see #setColors(int[]) 
     */
    public void setColor(int argb) {
        mGradientState.setSolidColor(argb);
        mFillPaint.setColor(argb);
@@ -450,8 +620,7 @@ public class GradientDrawable extends Drawable {

    @Override
    public int getChangingConfigurations() {
        return super.getChangingConfigurations()
                | mGradientState.mChangingConfigurations;
        return super.getChangingConfigurations() | mGradientState.mChangingConfigurations;
    }

    @Override
@@ -480,7 +649,6 @@ public class GradientDrawable extends Drawable {

    @Override
    public int getOpacity() {
        // XXX need to figure out the actual opacity...
        return PixelFormat.TRANSLUCENT;
    }

@@ -912,11 +1080,6 @@ public class GradientDrawable extends Drawable {
        private boolean mUseLevel;
        private boolean mUseLevelForShape;

        
        GradientState() {
            mOrientation = Orientation.TOP_BOTTOM;
        }

        GradientState(Orientation orientation, int[] colors) {
            mOrientation = orientation;
            mColors = colors;
@@ -987,6 +1150,11 @@ public class GradientDrawable extends Drawable {
            mCenterY = y;
        }

        public void setColors(int[] colors) {
            mHasSolidColor = false;
            mColors = colors;
        }
        
        public void setSolidColor(int argb) {
            mHasSolidColor = true;
            mSolidColor = argb;
@@ -1055,4 +1223,3 @@ public class GradientDrawable extends Drawable {
        }
    }
}