Class GradientDrawable


public class GradientDrawable extends ShapeDrawable
A Drawable with a color gradient for buttons, backgrounds, etc.
  • Field Details

  • Constructor Details

  • Method Details

    • 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 ANGULAR_GRADIENT
      See Also:
    • getGradientType

      public int getGradientType()
      Returns the type of gradient used by this drawable, one of LINEAR_GRADIENT, RADIAL_GRADIENT, or ANGULAR_GRADIENT.
      Returns:
      the type of gradient used by this drawable
      See Also:
    • setGradientCenter

      public void setGradientCenter(float x, float y)
      Sets the position of the center of the gradient as a fraction of the width and height.

      The default value is (0.5, 0.5).

      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-position of the center of the gradient
      y - the Y-position of the center of the gradient
      See Also:
    • getGradientCenterX

      public float getGradientCenterX()
      Returns the X-position of the center of the gradient as a fraction of the width.
      Returns:
      the X-position of the center of the gradient
      See Also:
    • getGradientCenterY

      public float getGradientCenterY()
      Returns the Y-position of the center of this gradient as a fraction of the height.
      Returns:
      the Y-position of the center of the gradient
      See Also:
    • setGradientRadius

      public void setGradientRadius(float gradientRadius, int type)
      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
      type - the unit of the radius
      See Also:
    • getGradientRadius

      @Px 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:
      the radius of the gradient in pixels
      See Also:
    • setUseLevel

      public void setUseLevel(boolean useLevel)
      Sets whether this drawable's level property will be used to scale the gradient. If a gradient is not used, this property has no effect.

      Scaling behavior varies based on gradient type:

      The default value for this property is false.

      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 the gradient should be scaled based on level, false otherwise
      See Also:
    • getUseLevel

      public boolean getUseLevel()
      Returns whether this drawable's level property will be used to scale the gradient.
      Returns:
      true if the gradient should be scaled based on level, false otherwise
      See Also:
    • getOrientation

      @NonNull public GradientDrawable.Orientation getOrientation()
      Returns the orientation of the gradient defined in this drawable.
      Returns:
      the orientation of the gradient defined in this drawable
      See Also:
    • setOrientation

      public void setOrientation(@NonNull GradientDrawable.Orientation orientation)
      Sets 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:
    • setColors

      public void setColors(@Nullable @ColorInt int[] colors)
      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:
    • setColors

      public void setColors(@Nullable @ColorInt int[] colors, @Nullable float[] offsets)
      Sets the colors and offsets 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
      offsets - optional array of floating point parameters representing the positions of the colors. Null evenly disperses the colors
      See Also:
    • getColors

      @Nullable public int[] getColors()
      Returns the colors used to draw the gradient, or null if the gradient is drawn using a single color or no colors.
      Returns:
      the colors used to draw the gradient, or null
      See Also:
    • setDither

      public void setDither(boolean dither)
      Sets a hint that indicates if color error may be distributed to smooth color transition. The default value is false.
    • isDither

      public boolean isDither()
      Returns true if color error may be distributed to smooth color transition. The default value is false.
    • mutate

      @NonNull public Drawable mutate()
      Description copied from class: Drawable
      Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

      Calling this method on a mutable Drawable will have no effect.

      Overrides:
      mutate in class ShapeDrawable
      Returns:
      This drawable.
      See Also: