Package icyllis.modernui.graphics.drawable

# Class GradientDrawable

java.lang.Object
icyllis.modernui.graphics.drawable.Drawable
icyllis.modernui.graphics.drawable.ShapeDrawable
icyllis.modernui.graphics.drawable.GradientDrawable

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

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static enum`
  `GradientDrawable.Orientation`
  Controls how the gradient is oriented relative to the drawable's bounds

  ## Nested classes/interfaces inherited from class icyllis.modernui.graphics.drawable.Drawable

  `Drawable.Callback, Drawable.ConstantState`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `ANGULAR_GRADIENT`
  `static final int`
  `LINEAR_GRADIENT`
  Gradient is linear (default.)
  `static final int`
  `RADIAL_GRADIENT`
  `static final int`
  `RADIUS_TYPE_FRACTION`
  Radius is a fraction of the base size.
  `static final int`
  `RADIUS_TYPE_FRACTION_PARENT`
  Radius is a fraction of the bounds size.
  `static final int`
  `RADIUS_TYPE_PIXELS`
  Radius is in pixels.

  ### Fields inherited from class icyllis.modernui.graphics.drawable.ShapeDrawable

  `CIRCLE, HLINE, RECTANGLE, RING, VLINE`

  ### Fields inherited from class icyllis.modernui.graphics.drawable.Drawable

  `MAX_LEVEL`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GradientDrawable()`
  `GradientDrawable(GradientDrawable.Orientation orientation, int[] colors)`
  Create a new gradient drawable given an orientation and an array of colors for the gradient.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int[]`
  `getColors()`
  Returns the colors used to draw the gradient, or `null` if the gradient is drawn using a single color or no colors.
  `float`
  `getGradientCenterX()`
  Returns the X-position of the center of the gradient as a fraction of the width.
  `float`
  `getGradientCenterY()`
  Returns the Y-position of the center of this gradient as a fraction of the height.
  `float`
  `getGradientRadius()`
  Returns the radius of the gradient in pixels.
  `int`
  `getGradientType()`
  Returns the type of gradient used by this drawable, one of `LINEAR_GRADIENT`, `RADIAL_GRADIENT`, or `ANGULAR_GRADIENT`.
  `GradientDrawable.Orientation`
  `getOrientation()`
  Returns the orientation of the gradient defined in this drawable.
  `boolean`
  `getUseLevel()`
  Returns whether this drawable's `level` property will be used to scale the gradient.
  `boolean`
  `isDither()`
  Returns true if color error may be distributed to smooth color transition.
  `Drawable`
  `mutate()`
  Make this drawable mutable.
  `void`
  `setColors(int[] colors)`
  Sets the colors used to draw the gradient.
  `void`
  `setColors(int[] colors, float[] offsets)`
  Sets the colors and offsets used to draw the gradient.
  `void`
  `setDither(boolean dither)`
  Sets a hint that indicates if color error may be distributed to smooth color transition.
  `void`
  `setGradientCenter(float x, float y)`
  Sets the position of the center of the gradient as a fraction of the width and height.
  `void`
  `setGradientRadius(float gradientRadius, int type)`
  Sets the radius of the gradient.
  `void`
  `setGradientType(int gradient)`
  Sets the type of gradient used by this drawable.
  `void`
  `setOrientation(GradientDrawable.Orientation orientation)`
  Sets the orientation of the gradient defined in this drawable.
  `void`
  `setUseLevel(boolean useLevel)`
  Sets whether this drawable's `level` property will be used to scale the gradient.

  ### Methods inherited from class icyllis.modernui.graphics.drawable.ShapeDrawable

  `clearMutated, draw, getAlpha, getColor, getColorFilter, getConstantState, getCornerRadius, getInnerRadius, getInnerRadiusRatio, getIntrinsicHeight, getIntrinsicWidth, getPadding, getShape, getThickness, getThicknessRatio, getUseLevelForShape, hasFocusStateSpecified, isStateful, modulateAlpha, onBoundsChange, onLevelChange, onStateChange, setAlpha, setColor, setColor, setColorFilter, setCornerRadius, setInnerRadius, setInnerRadiusRatio, setPadding, setShape, setSize, setStroke, setStroke, setThickness, setThicknessRatio, setTintBlendMode, setTintList, setUseLevelForShape`

  ### Methods inherited from class icyllis.modernui.graphics.drawable.Drawable

  `applyTheme, canApplyTheme, copyBounds, copyBounds, getBounds, getCallback, getChangingConfigurations, getCurrent, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getState, invalidateSelf, isAutoMirrored, isVisible, jumpToCurrentState, onLayoutDirectionChanged, scaleFromDensity, scaleFromDensity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, setVisible, unscheduleSelf`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### LINEAR_GRADIENT

    public static final int LINEAR_GRADIENT
    Gradient is linear (default.)
    See Also:
      - `LinearGradient`
      - Constant Field Values

  - ### RADIAL_GRADIENT

    public static final int RADIAL_GRADIENT
    See Also:
      - `RadialGradient`
      - Constant Field Values

  - ### ANGULAR_GRADIENT

    public static final int ANGULAR_GRADIENT
    See Also:
      - `AngularGradient`
      - Constant Field Values

  - ### RADIUS_TYPE_PIXELS

    public static final int RADIUS_TYPE_PIXELS
    Radius is in pixels.
    See Also:
      - Constant Field Values

  - ### RADIUS_TYPE_FRACTION

    public static final int RADIUS_TYPE_FRACTION
    Radius is a fraction of the base size.
    See Also:
      - Constant Field Values

  - ### RADIUS_TYPE_FRACTION_PARENT

    public static final int RADIUS_TYPE_FRACTION_PARENT
    Radius is a fraction of the bounds size.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### GradientDrawable

    public GradientDrawable()

  - ### GradientDrawable

    public GradientDrawable(@NonNull GradientDrawable.Orientation orientation, @Nullable @ColorInt int[] colors)
    Create a new gradient drawable given an orientation and an array of colors for the gradient.

- ## 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:
      - `mutate()`
      - `getGradientType()`

  - ### 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:
      - `setGradientType(int)`

  - ### 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:
      - `mutate()`
      - `setGradientType(int)`
      - `getGradientCenterX()`
      - `getGradientCenterY()`

  - ### 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:
      - `setGradientCenter(float, float)`

  - ### 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:
      - `setGradientCenter(float, float)`

  - ### 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:
      - `mutate()`
      - `setGradientType(int)`
      - `getGradientRadius()`

  - ### 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:
      - `setGradientRadius(float, int)`

  - ### 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:

    - `LINEAR_GRADIENT` adjusts the ending position along the gradient's axis of orientation (see `getOrientation()`)
    - `RADIAL_GRADIENT` adjusts the outer radius
    - `ANGULAR_GRADIENT` adjusts the ending angle

    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:
      - `mutate()`
      - `Drawable.setLevel(int)`
      - `Drawable.getLevel()`
      - `getUseLevel()`

  - ### 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:
      - `setUseLevel(boolean)`

  - ### 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(Orientation)`

  - ### 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:
      - `mutate()`
      - `getOrientation()`

  - ### 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:
      - `mutate()`
      - `ShapeDrawable.setColor(int)`

  - ### 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:
      - `mutate()`
      - `setColors(int[])`

  - ### 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:
      - `setColors(int[] colors)`

  - ### 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:
      - `Drawable.ConstantState`
      - `Drawable.getConstantState()`