Class PositionableGradientDrawable


  • public class PositionableGradientDrawable
    extends android.graphics.drawable.Drawable
    A Drawable with a color gradient for buttons, backgrounds, etc.

    Tweaked for Batch: No XML support, added support for setting the color positions Tinting is also broken, due to private APIs

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  PositionableGradientDrawable.Orientation
      Controls how the gradient is oriented relative to the drawable's bounds
      • Nested classes/interfaces inherited from class android.graphics.drawable.Drawable

        android.graphics.drawable.Drawable.Callback, android.graphics.drawable.Drawable.ConstantState
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int LINE
      Shape is a line
      static int LINEAR_GRADIENT
      Gradient is linear (default.)
      static int OVAL
      Shape is an ellipse
      static int RADIAL_GRADIENT
      Gradient is circular.
      static int RECTANGLE
      Shape is a rectangle, possibly with rounded corners
      static int RING
      Shape is a ring.
      static int SWEEP_GRADIENT
      Gradient is a sweep.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearMutated()  
      void draw​(android.graphics.Canvas canvas)  
      int getAlpha()  
      int getChangingConfigurations()  
      android.graphics.ColorFilter getColorFilter()  
      android.graphics.drawable.Drawable.ConstantState getConstantState()  
      float getGradientRadius()
      Returns the radius of the gradient in pixels.
      int getIntrinsicHeight()  
      int getIntrinsicWidth()  
      int getOpacity()  
      PositionableGradientDrawable.Orientation getOrientation()
      Returns the orientation of the gradient defined in this drawable.
      void getOutline​(android.graphics.Outline outline)  
      boolean getPadding​(android.graphics.Rect padding)  
      boolean isStateful()  
      android.graphics.drawable.Drawable mutate()  
      protected void onBoundsChange​(android.graphics.Rect r)  
      protected boolean onLevelChange​(int level)  
      protected boolean onStateChange​(int[] stateSet)  
      void setAlpha​(int alpha)  
      void setColor​(int argb)
      Changes this drawable to use a single color instead of a gradient.
      void setColor​(android.content.res.ColorStateList colorStateList)
      Changes this drawable to use a single color state list instead of a gradient.
      void setColorFilter​(android.graphics.ColorFilter colorFilter)  
      void setColors​(int[] colors, float[] positions)
      Sets the colors used to draw the gradient.
      void setCornerRadii​(float[] radii)
      Specify radii for each of the 4 corners.
      void setCornerRadius​(float radius)
      Specify radius for the corners of the gradient.
      void setDither​(boolean dither)  
      void setGradientCenter​(float x, float y)
      Sets the center location of the gradient.
      void setGradientRadius​(float gradientRadius)
      Sets the radius of the gradient.
      void setGradientType​(int gradient)
      Sets the type of gradient used by this drawable..
      void setOrientation​(PositionableGradientDrawable.Orientation orientation)
      Changes the orientation of the gradient defined in this drawable.
      void setShape​(int shape)
      Sets the type of shape used to draw the gradient.
      void setSize​(int width, int height)
      Sets the size of the shape drawn by this drawable.
      void setStroke​(int width, int color)
      Set the stroke width and color for the drawable.
      void setStroke​(int width, int color, float dashWidth, float dashGap)
      Set the stroke width and color for the drawable.
      void setStroke​(int width, android.content.res.ColorStateList colorStateList)
      Set the stroke width and color state list for the drawable.
      void setStroke​(int width, android.content.res.ColorStateList colorStateList, float dashWidth, float dashGap)
      Set the stroke width and color state list for the drawable.
      void setTintList​(android.content.res.ColorStateList tint)  
      void setTintMode​(android.graphics.PorterDuff.Mode tintMode)  
      void setUseLevel​(boolean useLevel)
      Sets whether or not this drawable will honor its level property.
      • Methods inherited from class android.graphics.drawable.Drawable

        applyTheme, canApplyTheme, clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getCurrent, getDirtyBounds, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getOpticalInsets, getState, getTransparentRegion, hasFocusStateSpecified, inflate, inflate, invalidateSelf, isAutoMirrored, isFilterBitmap, isProjected, isVisible, jumpToCurrentState, onLayoutDirectionChanged, resolveOpacity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setFilterBitmap, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, setTintBlendMode, setVisible, unscheduleSelf
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PositionableGradientDrawable

        public PositionableGradientDrawable()
      • PositionableGradientDrawable

        public PositionableGradientDrawable​(PositionableGradientDrawable.Orientation orientation,
                                            int[] colors,
                                            float[] positions)
        Create a new gradient drawable given an orientation and an array of colors for the gradient.
    • Method Detail

      • getPadding

        public boolean getPadding​(android.graphics.Rect padding)
        Overrides:
        getPadding in class android.graphics.drawable.Drawable
      • setCornerRadii

        public void setCornerRadii​(float[] radii)

        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. This property is honored only when the shape is of type RECTANGLE.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        radii - 4 pairs of X and Y radius for each corner, specified in pixels. The length of this array must be >= 8
        See Also:
        mutate(), setCornerRadii(float[]), setShape(int)
      • setCornerRadius

        public void setCornerRadius​(float radius)

        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 RECTANGLE.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        radius - The radius in pixels of the corners of the rectangle shape
        See Also:
        mutate(), setCornerRadii(float[]), setShape(int)
      • setStroke

        public void setStroke​(int width,
                              int color)

        Set the stroke width and color for the drawable. If width is zero, then no stroke is drawn.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        width - The width in pixels of the stroke
        color - The color of the stroke
        See Also:
        mutate(), setStroke(int, int, float, float)
      • setStroke

        public void setStroke​(int width,
                              android.content.res.ColorStateList colorStateList)

        Set the stroke width and color state list for the drawable. If width is zero, then no stroke is drawn.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        width - The width in pixels of the stroke
        colorStateList - The color state list of the stroke
        See Also:
        mutate(), setStroke(int, ColorStateList, float, float)
      • setStroke

        public void setStroke​(int width,
                              int color,
                              float dashWidth,
                              float dashGap)

        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.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        width - The width in pixels of the stroke
        color - The color of the stroke
        dashWidth - The length in pixels of the dashes, set to 0 to disable dashes
        dashGap - The gap in pixels between dashes
        See Also:
        mutate(), setStroke(int, int)
      • setStroke

        public void setStroke​(int width,
                              android.content.res.ColorStateList colorStateList,
                              float dashWidth,
                              float dashGap)

        Set the stroke width and color state list for the drawable. If width is zero, then no stroke is drawn. This method can also be used to dash the stroke.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        width - The width in pixels of the stroke
        colorStateList - The color state list of the stroke
        dashWidth - The length in pixels of the dashes, set to 0 to disable dashes
        dashGap - The gap in pixels between dashes
        See Also:
        mutate(), setStroke(int, ColorStateList)
      • setSize

        public void setSize​(int width,
                            int height)

        Sets the size of the shape drawn by this drawable.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        width - The width of the shape used by this drawable
        height - The height of the shape used by this drawable
        See Also:
        mutate(), setGradientType(int)
      • setShape

        public void setShape​(int shape)

        Sets the type of shape used to draw the gradient.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        shape - The desired shape for this drawable: LINE, OVAL, RECTANGLE or RING
        See Also:
        mutate()
      • setGradientType

        public void setGradientType​(int gradient)

        Sets the type of gradient used by this drawable..

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        gradient - The type of the gradient: LINEAR_GRADIENT, RADIAL_GRADIENT or SWEEP_GRADIENT
        See Also:
        mutate()
      • setGradientCenter

        public void setGradientCenter​(float x,
                                      float y)

        Sets the center location of the gradient. The radius is honored only when the gradient type is set to RADIAL_GRADIENT or SWEEP_GRADIENT.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        x - The x coordinate of the gradient's center
        y - The y coordinate of the gradient's center
        See Also:
        mutate(), setGradientType(int)
      • setGradientRadius

        public void setGradientRadius​(float gradientRadius)

        Sets the radius of the gradient. The radius is honored only when the gradient type is set to RADIAL_GRADIENT.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        gradientRadius - The radius of the gradient in pixels
        See Also:
        mutate(), setGradientType(int)
      • getGradientRadius

        public float getGradientRadius()
        Returns the radius of the gradient in pixels. The radius is valid only when the gradient type is set to RADIAL_GRADIENT.
        Returns:
        Radius in pixels.
      • setUseLevel

        public void setUseLevel​(boolean useLevel)

        Sets whether or not this drawable will honor its level property.

        Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

        Parameters:
        useLevel - True if this drawable should honor its level, false otherwise
        See Also:
        mutate(), Drawable.setLevel(int), Drawable.getLevel()
      • setOrientation

        public void setOrientation​(PositionableGradientDrawable.Orientation orientation)

        Changes the orientation of the gradient defined in this drawable.

        Note: changing orientation will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the orientation.

        Parameters:
        orientation - The desired orientation (angle) of the gradient
        See Also:
        mutate()
      • setColors

        public void setColors​(int[] colors,
                              float[] positions)
        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.

        Note: changing colors will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the colors.

        Parameters:
        colors - an array containing 2 or more ARGB colors
        See Also:
        mutate(), setColor(int)
      • draw

        public void draw​(android.graphics.Canvas canvas)
        Specified by:
        draw in class android.graphics.drawable.Drawable
      • setColor

        public void setColor​(int argb)

        Changes this drawable to use a single color instead of a gradient.

        Note: changing color will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the color.

        Parameters:
        argb - The color used to fill the shape
        See Also:
        mutate(), setColors(int[], float[])
      • setColor

        public void setColor​(android.content.res.ColorStateList colorStateList)
        Changes this drawable to use a single color state list instead of a gradient. Calling this method with a null argument will clear the color and is equivalent to calling setColor(int) with the argument Color.TRANSPARENT.

        Note: changing color will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the color.

        Parameters:
        colorStateList - The color state list used to fill the shape
        See Also:
        mutate()
      • onStateChange

        protected boolean onStateChange​(int[] stateSet)
        Overrides:
        onStateChange in class android.graphics.drawable.Drawable
      • isStateful

        public boolean isStateful()
        Overrides:
        isStateful in class android.graphics.drawable.Drawable
      • getChangingConfigurations

        public int getChangingConfigurations()
        Overrides:
        getChangingConfigurations in class android.graphics.drawable.Drawable
      • setAlpha

        public void setAlpha​(int alpha)
        Specified by:
        setAlpha in class android.graphics.drawable.Drawable
      • getAlpha

        public int getAlpha()
        Overrides:
        getAlpha in class android.graphics.drawable.Drawable
      • setDither

        public void setDither​(boolean dither)
        Overrides:
        setDither in class android.graphics.drawable.Drawable
      • getColorFilter

        public android.graphics.ColorFilter getColorFilter()
        Overrides:
        getColorFilter in class android.graphics.drawable.Drawable
      • setColorFilter

        public void setColorFilter​(android.graphics.ColorFilter colorFilter)
        Specified by:
        setColorFilter in class android.graphics.drawable.Drawable
      • setTintList

        public void setTintList​(android.content.res.ColorStateList tint)
        Overrides:
        setTintList in class android.graphics.drawable.Drawable
      • setTintMode

        public void setTintMode​(android.graphics.PorterDuff.Mode tintMode)
        Overrides:
        setTintMode in class android.graphics.drawable.Drawable
      • getOpacity

        public int getOpacity()
        Specified by:
        getOpacity in class android.graphics.drawable.Drawable
      • onBoundsChange

        protected void onBoundsChange​(android.graphics.Rect r)
        Overrides:
        onBoundsChange in class android.graphics.drawable.Drawable
      • onLevelChange

        protected boolean onLevelChange​(int level)
        Overrides:
        onLevelChange in class android.graphics.drawable.Drawable
      • getIntrinsicWidth

        public int getIntrinsicWidth()
        Overrides:
        getIntrinsicWidth in class android.graphics.drawable.Drawable
      • getIntrinsicHeight

        public int getIntrinsicHeight()
        Overrides:
        getIntrinsicHeight in class android.graphics.drawable.Drawable
      • getConstantState

        public android.graphics.drawable.Drawable.ConstantState getConstantState()
        Overrides:
        getConstantState in class android.graphics.drawable.Drawable
      • getOutline

        public void getOutline​(android.graphics.Outline outline)
        Overrides:
        getOutline in class android.graphics.drawable.Drawable
      • mutate

        public android.graphics.drawable.Drawable mutate()
        Overrides:
        mutate in class android.graphics.drawable.Drawable
      • clearMutated

        public void clearMutated()