Class ShapeDrawable
- Direct Known Subclasses:
GradientDrawable
Drawable
for drawing shapes.
We have five shapes that are:
- Rectangle
- Circle
- Ring
- HLine
- VLine
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
Modifier and TypeFieldDescriptionstatic final int
Shape is a circle or pie.static final int
Shape is a horizontal line.static final int
Shape is a rectangle, possibly with rounded corners.static final int
Shape is a ring or arc.static final int
Shape is a vertical line. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the mutated state, allowing this drawable to be cached and mutated again.void
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.getColor()
Returns the color state list used to fill the shape, ornull
if the shape is filled with a gradient or has no fill color.Returns the current color filter, ornull
if none set.Return aDrawable.ConstantState
instance that holds the shared state of this Drawable.float
Returns the radius for the corners of the gradient, that was previously set withsetCornerRadius(float)
.int
Return the inner radius of the ring, or -1 to usegetInnerRadiusRatio()
.float
Return the inner radius of the ring expressed as a ratio of the ring's width.int
Returns the drawable's intrinsic height.int
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()
int
Return the thickness of the ring, or -1 to usegetThicknessRatio()
.float
Return the thickness ratio of the ring expressed as a ratio of the ring's width.boolean
boolean
Indicates whether this drawable has at least one state spec explicitly specifying state_focused.boolean
Indicates whether this drawable will change its appearance based on state.static int
modulateAlpha
(int srcAlpha, int dstAlpha) Blends two alpha using modulate.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 usegetInnerRadiusRatio()
.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 usegetThicknessRatio()
.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
-
Field Details
-
RECTANGLE
public static final int RECTANGLEShape is a rectangle, possibly with rounded corners.- See Also:
-
CIRCLE
public static final int CIRCLEShape is a circle or pie.- See Also:
-
RING
public static final int RINGShape is a ring or arc.- See Also:
-
HLINE
public static final int HLINEShape is a horizontal line.- See Also:
-
VLINE
public static final int VLINEShape is a vertical line.- See Also:
-
-
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 validationdstAlpha
- 0..255 no validation- Returns:
- result alpha
-
draw
Description copied from class:Drawable
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha). -
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. -
getShape
public int getShape()- Returns:
- the type of shape used by this drawable
- See Also:
-
setUseLevelForShape
public void setUseLevelForShape(boolean useLevelForShape) Sets whether to draw circles and rings based on level. If
Drawable.getLevel()
is less thanDrawable.MAX_LEVEL
, a circle shape becomes a pie, and a ring shape becomes an open arc.CIRCLE
andRING
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
public boolean getUseLevelForShape()- See Also:
-
setPadding
Configure the padding of the shape, there is no padding by default.- Parameters:
left
- Left padding of the shapetop
- Top padding of the shaperight
- Right padding of the shapebottom
- Bottom padding of the shape
-
getPadding
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 (setRect.inset(int, int)
).- Overrides:
getPadding
in classDrawable
- 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 drawableheight
- The height of the shape used by this drawable- See Also:
-
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 typeRECTANGLE
. Specifically, if this is > 0, the line ends are rounded when the shape is of typeHLINE
,VLINE
orRING
.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:
-
getCornerRadius
public float getCornerRadius()Returns the radius for the corners of the gradient, that was previously set withsetCornerRadius(float)
.- Returns:
- the radius in pixels of the corners of the rectangle shape, or 0
- See Also:
-
setColor
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:
-
setColor
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 callingsetColor(int)
with the argumentColor.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:
-
getColor
Returns the color state list used to fill the shape, ornull
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:
-
setStroke
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 strokecolor
- The color of the stroke- See Also:
-
setStroke
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 strokecolorStateList
- The color state list of the stroke- See Also:
-
setInnerRadiusRatio
Inner radius of the ring expressed as a ratio of the ring's width. This value is used only whengetInnerRadius()
is -1.- See Also:
-
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 whengetInnerRadius()
is -1.- See Also:
-
setInnerRadius
Configure the inner radius of the ring, or -1 to usegetInnerRadiusRatio()
. The default value is -1.- See Also:
-
getInnerRadius
Return the inner radius of the ring, or -1 to usegetInnerRadiusRatio()
. The default value is -1.- See Also:
-
setThicknessRatio
Configure the thickness of the ring expressed as a ratio of the ring's width. This value is used only whengetThickness()
is -1.- See Also:
-
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 whengetThickness()
is -1.- See Also:
-
setThickness
Configure the thickness of the ring, or -1 to usegetThicknessRatio()
. The default value is -1. -
getThickness
Return the thickness of the ring, or -1 to usegetThicknessRatio()
. The default value is -1.- See Also:
-
onStateChange
Description copied from class:Drawable
Override this in your subclass to change appearance if you recognize the specified state.- Overrides:
onStateChange
in classDrawable
- 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 classDrawable
- Returns:
- True if this drawable changes its appearance based on state, false otherwise.
- See Also:
-
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 classDrawable
- 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. -
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. -
setColorFilter
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 disablestint
.- Overrides:
setColorFilter
in classDrawable
- Parameters:
colorFilter
- The color filter to apply, ornull
to remove the existing color filter
-
getColorFilter
Description copied from class:Drawable
Returns the current color filter, ornull
if none set.- Overrides:
getColorFilter
in classDrawable
- Returns:
- the current color filter, or
null
if none set
-
setTintList
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 classDrawable
- Parameters:
tint
- Color state list to use for tinting this drawable, ornull
to clear the tint- See Also:
-
setTintBlendMode
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 classDrawable
- Parameters:
blendMode
- BlendMode to apply to the drawable, a value of null sets the default blend mode value ofBlendMode.SRC_IN
- See Also:
-
onBoundsChange
Description copied from class:Drawable
Override this in your subclass to change appearance if you vary based on the bounds.- Overrides:
onBoundsChange
in classDrawable
-
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 classDrawable
- 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 classDrawable
- 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 classDrawable
- Returns:
- the intrinsic height, or -1 if no intrinsic height
-
getConstantState
Description copied from class:Drawable
Return aDrawable.ConstantState
instance that holds the shared state of this Drawable.- Overrides:
getConstantState
in classDrawable
- Returns:
- The ConstantState associated to that Drawable.
- See Also:
-
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.
-
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 classDrawable
-