Package icyllis.modernui.graphics.drawable

# Class ShapeDrawable

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

Direct Known Subclasses:
  `GradientDrawable`

public class ShapeDrawable extends Drawable
A `Drawable` for drawing shapes.

We have five shapes that are:

- Rectangle
- Circle
- Ring
- HLine
- VLine

In details: rectangle, circle and ring can be filled, stroked, or both, with different colors.
Rectangle can be rounded; circle and ring can use `level`, that is, they become pie and arc, which can be stroked as well.

- ## Nested Class Summary

  ## 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`
  `CIRCLE`
  Shape is a circle or pie.
  `static final int`
  `HLINE`
  Shape is a horizontal line.
  `static final int`
  `RECTANGLE`
  Shape is a rectangle, possibly with rounded corners.
  `static final int`
  `RING`
  Shape is a ring or arc.
  `static final int`
  `VLINE`
  Shape is a vertical line.

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

  `MAX_LEVEL`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ShapeDrawable()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `clearMutated()`
  Clears the mutated state, allowing this drawable to be cached and mutated again.
  `void`
  `draw(Canvas canvas)`
  Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha).
  `int`
  `getAlpha()`
  Gets the current alpha value for the drawable.
  `ColorStateList`
  `getColor()`
  Returns the color state list used to fill the shape, or `null` if the shape is filled with a gradient or has no fill color.
  `ColorFilter`
  `getColorFilter()`
  Returns the current color filter, or `null` if none set.
  `Drawable.ConstantState`
  `getConstantState()`
  Return a `Drawable.ConstantState` instance that holds the shared state of this Drawable.
  `float`
  `getCornerRadius()`
  Returns the radius for the corners of the gradient, that was previously set with `setCornerRadius(float)`.
  `int`
  `getInnerRadius()`
  Return the inner radius of the ring, or -1 to use `getInnerRadiusRatio()`.
  `float`
  `getInnerRadiusRatio()`
  Return the inner radius of the ring expressed as a ratio of the ring's width.
  `int`
  `getIntrinsicHeight()`
  Returns the drawable's intrinsic height.
  `int`
  `getIntrinsicWidth()`
  Returns the drawable's intrinsic width.
  `boolean`
  `getPadding(Rect padding)`
  Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.
  `int`
  `getShape()`
  Returns the type of shape used by this drawable, one of `RECTANGLE`, `CIRCLE`, `RING`, `HLINE` or `VLINE`.
  `int`
  `getThickness()`
  Return the thickness of the ring, or -1 to use `getThicknessRatio()`.
  `float`
  `getThicknessRatio()`
  Return the thickness ratio of the ring expressed as a ratio of the ring's width.
  `boolean`
  `getUseLevelForShape()`
  `boolean`
  `hasFocusStateSpecified()`
  Indicates whether this drawable has at least one state spec explicitly specifying state_focused.
  `boolean`
  `isStateful()`
  Indicates whether this drawable will change its appearance based on state.
  `static int`
  `modulateAlpha(int srcAlpha, int dstAlpha)`
  Blends two alpha using modulate.
  `Drawable`
  `mutate()`
  Make this drawable mutable.
  `protected void`
  `onBoundsChange(Rect bounds)`
  Override this in your subclass to change appearance if you vary based on the bounds.
  `protected boolean`
  `onLevelChange(int level)`
  Override this in your subclass to change appearance if you vary based on level.
  `protected boolean`
  `onStateChange(int[] stateSet)`
  Override this in your subclass to change appearance if you recognize the specified state.
  `void`
  `setAlpha(int alpha)`
  Specify an alpha value for the drawable.
  `void`
  `setColor(int color)`
  Changes this drawable to use a single color instead of a gradient.
  `void`
  `setColor(ColorStateList colorStateList)`
  Changes this drawable to use a single color state list instead of a gradient.
  `void`
  `setColorFilter(ColorFilter colorFilter)`
  Specify an optional color filter for the drawable.
  `void`
  `setCornerRadius(float radius)`
  Specifies the radius for the corners of the gradient.
  `void`
  `setInnerRadius(int innerRadius)`
  Configure the inner radius of the ring, or -1 to use `getInnerRadiusRatio()`.
  `void`
  `setInnerRadiusRatio(float innerRadiusRatio)`
  Inner radius of the ring expressed as a ratio of the ring's width.
  `void`
  `setPadding(int left, int top, int right, int bottom)`
  Configure the padding of the shape, there is no padding by default.
  `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, ColorStateList colorStateList)`
  Set the stroke width and color state list for the drawable.
  `void`
  `setThickness(int thickness)`
  Configure the thickness of the ring, or -1 to use `getThicknessRatio()`.
  `void`
  `setThicknessRatio(float thicknessRatio)`
  Configure the thickness of the ring expressed as a ratio of the ring's width.
  `void`
  `setTintBlendMode(BlendMode blendMode)`
  Specifies a tint blending mode for this drawable.
  `void`
  `setTintList(ColorStateList tint)`
  Specifies tint color for this drawable as a color state list.
  `void`
  `setUseLevelForShape(boolean useLevelForShape)`
  Sets whether to draw circles and rings based on level.

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

  - ### RECTANGLE

    public static final int RECTANGLE
    Shape is a rectangle, possibly with rounded corners.
    See Also:
      - Constant Field Values

  - ### CIRCLE

    public static final int CIRCLE
    Shape is a circle or pie.
    See Also:
      - Constant Field Values

  - ### RING

    public static final int RING
    Shape is a ring or arc.
    See Also:
      - Constant Field Values

  - ### HLINE

    public static final int HLINE
    Shape is a horizontal line.
    See Also:
      - Constant Field Values

  - ### VLINE

    public static final int VLINE
    Shape is a vertical line.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### ShapeDrawable

    public ShapeDrawable()

- ## Method Details

  - ### modulateAlpha

    public static int modulateAlpha(int srcAlpha, int dstAlpha)
    Blends two alpha using modulate. This method has errors but is fast.
    Parameters:
      `srcAlpha` - 0..255 no validation
      `dstAlpha` - 0..255 no validation
    Returns:
      result alpha

  - ### draw

    public void draw(@NonNull Canvas canvas)
    Description copied from class: `Drawable`
    Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha).
    Specified by:
      `draw` in class `Drawable`
    Parameters:
      `canvas` - The canvas to draw into

  - ### 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: `RECTANGLE`, `CIRCLE`, `RING`, `HLINE` or `VLINE`
    See Also:
      - `mutate()`

  - ### getShape

    public int getShape()
    Returns the type of shape used by this drawable, one of `RECTANGLE`, `CIRCLE`, `RING`, `HLINE` or `VLINE`.
    Returns:
      the type of shape used by this drawable
    See Also:
      - `setShape(int)`

  - ### setUseLevelForShape

    public void setUseLevelForShape(boolean useLevelForShape)

    Sets whether to draw circles and rings based on level. If `Drawable.getLevel()` is less than `Drawable.MAX_LEVEL`, a circle shape becomes a pie, and a ring shape becomes an open arc. `CIRCLE` and `RING` will start at 0 o'clock direction and sweep clockwise. The default is true.

    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:
      `useLevelForShape` - Whether to use level for shape
    See Also:
      - `getUseLevelForShape()`

  - ### getUseLevelForShape

    public boolean getUseLevelForShape()
    See Also:
      - `setUseLevelForShape(boolean)`

  - ### setPadding

    public void setPadding(@Px int left, @Px int top, @Px int right, @Px int bottom)
    Configure the padding of the shape, there is no padding by default.
    Parameters:
      `left` - Left padding of the shape
      `top` - Top padding of the shape
      `right` - Right padding of the shape
      `bottom` - Bottom padding of the shape

  - ### getPadding

    public boolean getPadding(@NonNull Rect padding)
    Description copied from class: `Drawable`
    Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds. Positive values move toward the center of the Drawable (set `Rect.inset(int, int)`).
    Overrides:
      `getPadding` in class `Drawable`
    Returns:
      true if this drawable actually has a padding, else false. When false is returned, the padding is always set to 0.

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

  - ### setCornerRadius

    public void setCornerRadius(float radius)
    Specifies the 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`. Specifically, if this is > 0, the line ends are rounded when the shape is of type `HLINE`, `VLINE` or `RING`.

    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()`
      - `setShape(int)`

  - ### getCornerRadius

    public float getCornerRadius()
    Returns the radius for the corners of the gradient, that was previously set with `setCornerRadius(float)`.
    Returns:
      the radius in pixels of the corners of the rectangle shape, or 0
    See Also:
      - `setCornerRadius(float)`

  - ### setColor

    public void setColor(@ColorInt int color)
    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:
      `color` - The color used to fill the shape
    See Also:
      - `mutate()`
      - `getColor()`

  - ### setColor

    public void setColor(@Nullable 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()`
      - `getColor()`

  - ### getColor

    @Nullable public ColorStateList getColor()
    Returns the color state list used to fill the shape, or `null` if the shape is filled with a gradient or has no fill color.
    Returns:
      the color state list used to fill this gradient, or `null`
    See Also:
      - `setColor(int)`
      - `setColor(ColorStateList)`

  - ### setStroke

    public void setStroke(int width, @ColorInt 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

    public void setStroke(int width, @Nullable 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()`

  - ### setInnerRadiusRatio

    public void setInnerRadiusRatio(@FloatRange(from=0.0,fromInclusive=false) float innerRadiusRatio)
    Inner radius of the ring expressed as a ratio of the ring's width. This value is used only when `getInnerRadius()` is -1.
    See Also:
      - `getInnerRadiusRatio()`

  - ### getInnerRadiusRatio

    public float getInnerRadiusRatio()
    Return the inner radius of the ring expressed as a ratio of the ring's width. This value is used only when `getInnerRadius()` is -1.
    See Also:
      - `setInnerRadiusRatio(float)`

  - ### setInnerRadius

    public void setInnerRadius(@Px int innerRadius)
    Configure the inner radius of the ring, or -1 to use `getInnerRadiusRatio()`. The default value is -1.
    See Also:
      - `getInnerRadius()`

  - ### getInnerRadius

    @Px public int getInnerRadius()
    Return the inner radius of the ring, or -1 to use `getInnerRadiusRatio()`. The default value is -1.
    See Also:
      - `setInnerRadius(int)`

  - ### setThicknessRatio

    public void setThicknessRatio(@FloatRange(from=0.0,fromInclusive=false) float thicknessRatio)
    Configure the thickness of the ring expressed as a ratio of the ring's width. This value is used only when `getThickness()` is -1.
    See Also:
      - `getThicknessRatio()`

  - ### getThicknessRatio

    public float getThicknessRatio()
    Return the thickness ratio of the ring expressed as a ratio of the ring's width. This value is used only when `getThickness()` is -1.
    See Also:
      - `setThicknessRatio(float)`

  - ### setThickness

    public void setThickness(@Px int thickness)
    Configure the thickness of the ring, or -1 to use `getThicknessRatio()`. The default value is -1.

  - ### getThickness

    @Px public int getThickness()
    Return the thickness of the ring, or -1 to use `getThicknessRatio()`. The default value is -1.
    See Also:
      - `setThickness(int)`

  - ### onStateChange

    protected boolean onStateChange(@NonNull int[] stateSet)
    Description copied from class: `Drawable`
    Override this in your subclass to change appearance if you recognize the specified state.
    Overrides:
      `onStateChange` in class `Drawable`
    Returns:
      Returns true if the state change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last state.

  - ### isStateful

    public boolean isStateful()
    Description copied from class: `Drawable`
    Indicates whether this drawable will change its appearance based on state. Clients can use this to determine whether it is necessary to calculate their state and call setState.
    Overrides:
      `isStateful` in class `Drawable`
    Returns:
      True if this drawable changes its appearance based on state, false otherwise.
    See Also:
      - `Drawable.setState(int[])`

  - ### hasFocusStateSpecified

    public boolean hasFocusStateSpecified()
    Description copied from class: `Drawable`
    Indicates whether this drawable has at least one state spec explicitly specifying state_focused.

    Note: A View uses a `Drawable` instance as its background and it changes its appearance based on a state. On keyboard devices, it should specify its state_focused to make sure the user knows which view is holding the focus.

    Overrides:
      `hasFocusStateSpecified` in class `Drawable`
    Returns:
      `true` if state_focused is specified for this drawable.

  - ### setAlpha

    public void setAlpha(int alpha)
    Description copied from class: `Drawable`
    Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.
    Overrides:
      `setAlpha` in class `Drawable`

  - ### getAlpha

    public int getAlpha()
    Description copied from class: `Drawable`
    Gets the current alpha value for the drawable. 0 means fully transparent, 255 means fully opaque. This method is implemented by Drawable subclasses and the value returned is specific to how that class treats alpha. The default return value is 255 if the class does not override this method to return a value specific to its use of alpha.
    Overrides:
      `getAlpha` in class `Drawable`

  - ### setColorFilter

    public void setColorFilter(@Nullable ColorFilter colorFilter)
    Description copied from class: `Drawable`
    Specify an optional color filter for the drawable.

    If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing contents will be modified by the color filter before it is blended onto the render target of a Canvas.

    Pass `null` to remove any existing color filter.

    Note: Setting a non-`null` color filter disables `tint`.

    Overrides:
      `setColorFilter` in class `Drawable`
    Parameters:
      `colorFilter` - The color filter to apply, or `null` to remove the existing color filter

  - ### getColorFilter

    @Nullable public ColorFilter getColorFilter()
    Description copied from class: `Drawable`
    Returns the current color filter, or `null` if none set.
    Overrides:
      `getColorFilter` in class `Drawable`
    Returns:
      the current color filter, or `null` if none set

  - ### setTintList

    public void setTintList(@Nullable ColorStateList tint)
    Description copied from class: `Drawable`
    Specifies tint color for this drawable as a color state list.

    A Drawable's drawing content will be blended together with its tint before it is drawn to the screen.

    Note: Setting a color filter via `Drawable.setColorFilter(ColorFilter)` overrides tint.

    Overrides:
      `setTintList` in class `Drawable`
    Parameters:
      `tint` - Color state list to use for tinting this drawable, or `null` to clear the tint
    See Also:
      - `Drawable.setTint(int)`
      - `Drawable.setTintBlendMode(BlendMode)`

  - ### setTintBlendMode

    public void setTintBlendMode(@NonNull BlendMode blendMode)
    Description copied from class: `Drawable`
    Specifies a tint blending mode for this drawable.

    Defines how this drawable's tint color should be blended into the drawable before it is drawn to screen. Default tint mode is `BlendMode.SRC_IN`.

    Note: Setting a color filter via `Drawable.setColorFilter(ColorFilter)`

    Overrides:
      `setTintBlendMode` in class `Drawable`
    Parameters:
      `blendMode` - BlendMode to apply to the drawable, a value of null sets the default blend mode value of `BlendMode.SRC_IN`
    See Also:
      - `Drawable.setTint(int)`
      - `Drawable.setTintList(ColorStateList)`

  - ### onBoundsChange

    protected void onBoundsChange(@NonNull Rect bounds)
    Description copied from class: `Drawable`
    Override this in your subclass to change appearance if you vary based on the bounds.
    Overrides:
      `onBoundsChange` in class `Drawable`

  - ### onLevelChange

    protected boolean onLevelChange(int level)
    Description copied from class: `Drawable`
    Override this in your subclass to change appearance if you vary based on level.
    Overrides:
      `onLevelChange` in class `Drawable`
    Returns:
      Returns true if the level change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last level.

  - ### getIntrinsicWidth

    public int getIntrinsicWidth()
    Description copied from class: `Drawable`
    Returns the drawable's intrinsic width.

    Intrinsic width is the width at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic width, such as a solid color, this method returns -1.

    Overrides:
      `getIntrinsicWidth` in class `Drawable`
    Returns:
      the intrinsic width, or -1 if no intrinsic width

  - ### getIntrinsicHeight

    public int getIntrinsicHeight()
    Description copied from class: `Drawable`
    Returns the drawable's intrinsic height.

    Intrinsic height is the height at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic height, such as a solid color, this method returns -1.

    Overrides:
      `getIntrinsicHeight` in class `Drawable`
    Returns:
      the intrinsic height, or -1 if no intrinsic height

  - ### getConstantState

    public Drawable.ConstantState getConstantState()
    Description copied from class: `Drawable`
    Return a `Drawable.ConstantState` instance that holds the shared state of this Drawable.
    Overrides:
      `getConstantState` in class `Drawable`
    Returns:
      The ConstantState associated to that Drawable.
    See Also:
      - `Drawable.ConstantState`
      - `Drawable.mutate()`

  - ### 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 `Drawable`
    Returns:
      This drawable.
    See Also:
      - `Drawable.ConstantState`
      - `Drawable.getConstantState()`

  - ### clearMutated

    public void clearMutated()
    Description copied from class: `Drawable`
    Clears the mutated state, allowing this drawable to be cached and mutated again.
    Overrides:
      `clearMutated` in class `Drawable`