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
FieldsModifier and TypeFieldDescriptionstatic final intShape is a circle or pie.static final intShape is a horizontal line.static final intShape is a rectangle, possibly with rounded corners.static final intShape is a ring or arc.static final intShape is a vertical line. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the mutated state, allowing this drawable to be cached and mutated again.voidDraw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha).intgetAlpha()Gets the current alpha value for the drawable.getColor()Returns the color state list used to fill the shape, ornullif the shape is filled with a gradient or has no fill color.Returns the current color filter, ornullif none set.Return aDrawable.ConstantStateinstance that holds the shared state of this Drawable.float[]Returns the radii for each of the 4 corners.floatReturns the radius for the corners of the gradient, that was previously set withsetCornerRadius(float).intReturn the inner radius of the ring, or -1 to usegetInnerRadiusRatio().floatReturn the inner radius of the ring expressed as a ratio of the ring's width.intReturns the drawable's intrinsic height.intReturns the drawable's intrinsic width.voidgetOutline(Outline outline) Called to get the drawable to populate the Outline that defines its drawing area.booleangetPadding(Rect padding) Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.intgetShape()intReturn the thickness of the ring, or -1 to usegetThicknessRatio().floatReturn the thickness ratio of the ring expressed as a ratio of the ring's width.booleanbooleanIndicates whether this drawable has at least one state spec explicitly specifying state_focused.booleanIndicates whether this drawable will change its appearance based on state.static intmodulateAlpha(int srcAlpha, int dstAlpha) Blends two alpha using modulate.mutate()Make this drawable mutable.protected voidonBoundsChange(Rect bounds) Override this in your subclass to change appearance if you vary based on the bounds.protected booleanonLevelChange(int level) Override this in your subclass to change appearance if you vary based on level.protected booleanonStateChange(int[] stateSet) Override this in your subclass to change appearance if you recognize the specified state.voidsetAlpha(int alpha) Specify an alpha value for the drawable.voidsetColor(int color) Changes this drawable to use a single color instead of a gradient.voidsetColor(ColorStateList colorStateList) Changes this drawable to use a single color state list instead of a gradient.voidsetColorFilter(ColorFilter colorFilter) Specify an optional color filter for the drawable.voidsetCornerRadii(float topLeftRadius, float topRightRadius, float bottomRightRadius, float bottomLeftRadius) Specifies the radius for each of the 4 corners.voidsetCornerRadius(float radius) Specifies the radius for the corners of the gradient.voidsetInnerRadius(int innerRadius) Configure the inner radius of the ring, or -1 to usegetInnerRadiusRatio().voidsetInnerRadiusRatio(float innerRadiusRatio) Inner radius of the ring expressed as a ratio of the ring's width.voidsetPadding(int left, int top, int right, int bottom) Configure the padding of the shape, there is no padding by default.voidsetShape(int shape) Sets the type of shape used to draw the gradient.voidsetSize(int width, int height) Sets the size of the shape drawn by this drawable.voidsetStroke(int width, int color) Set the stroke width and color for the drawable.voidsetStroke(int width, ColorStateList colorStateList) Set the stroke width and color state list for the drawable.voidsetThickness(int thickness) Configure the thickness of the ring, or -1 to usegetThicknessRatio().voidsetThicknessRatio(float thicknessRatio) Configure the thickness of the ring expressed as a ratio of the ring's width.voidsetTintBlendMode(BlendMode blendMode) Specifies a tint blending mode for this drawable.voidsetTintList(ColorStateList tint) Specifies tint color for this drawable as a color state list.voidsetUseLevelForShape(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, getDirtyBounds, 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:DrawableDraw 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.CIRCLEandRINGwill 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:DrawableReturn 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:
getPaddingin 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 also rounded when the shape is of typeHLINE,VLINEorRING.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:
-
setCornerRadii
public void setCornerRadii(float topLeftRadius, float topRightRadius, float bottomRightRadius, float bottomLeftRadius) Specifies the radius for each of the 4 corners. The corners are ordered top-left, top-right, bottom-right, bottom-left. This property is honored only when the shape is of typeRECTANGLE. Specifically, if top left radius is > 0, the line ends are also rounded when the shape is of typeHLINE,VLINEorRING.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:
topLeftRadius- the radius in pixels of the top-left cornertopRightRadius- the radius in pixels of the top-right cornerbottomRightRadius- the radius in pixels of the bottom-right cornerbottomLeftRadius- the radius in pixels of the bottom-left corner- See Also:
-
getCornerRadii
public float[] getCornerRadii()Returns the radii for each of the 4 corners. The corners are ordered top-left, top-right, bottom-right, bottom-left.- Returns:
- an array containing the radii for each of the 4 corners
- 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, ornullif 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:DrawableOverride this in your subclass to change appearance if you recognize the specified state.- Overrides:
onStateChangein 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:DrawableIndicates 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:
isStatefulin classDrawable- Returns:
- True if this drawable changes its appearance based on state, false otherwise.
- See Also:
-
hasFocusStateSpecified
public boolean hasFocusStateSpecified()Description copied from class:DrawableIndicates whether this drawable has at least one state spec explicitly specifying state_focused.Note: A View uses a
Drawableinstance 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:
hasFocusStateSpecifiedin classDrawable- Returns:
trueif state_focused is specified for this drawable.
-
setAlpha
public void setAlpha(int alpha) Description copied from class:DrawableSpecify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque. -
getAlpha
public int getAlpha()Description copied from class:DrawableGets 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:DrawableSpecify 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
nullto remove any existing color filter.Note: Setting a non-
nullcolor filter disablestint.- Overrides:
setColorFilterin classDrawable- Parameters:
colorFilter- The color filter to apply, ornullto remove the existing color filter
-
getColorFilter
Description copied from class:DrawableReturns the current color filter, ornullif none set.- Overrides:
getColorFilterin classDrawable- Returns:
- the current color filter, or
nullif none set
-
setTintList
Description copied from class:DrawableSpecifies 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:
setTintListin classDrawable- Parameters:
tint- Color state list to use for tinting this drawable, ornullto clear the tint- See Also:
-
setTintBlendMode
Description copied from class:DrawableSpecifies 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:
setTintBlendModein 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:DrawableOverride this in your subclass to change appearance if you vary based on the bounds.- Overrides:
onBoundsChangein classDrawable
-
onLevelChange
protected boolean onLevelChange(int level) Description copied from class:DrawableOverride this in your subclass to change appearance if you vary based on level.- Overrides:
onLevelChangein 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:DrawableReturns 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:
getIntrinsicWidthin classDrawable- Returns:
- the intrinsic width, or -1 if no intrinsic width
-
getIntrinsicHeight
public int getIntrinsicHeight()Description copied from class:DrawableReturns 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:
getIntrinsicHeightin classDrawable- Returns:
- the intrinsic height, or -1 if no intrinsic height
-
getConstantState
Description copied from class:DrawableReturn aDrawable.ConstantStateinstance that holds the shared state of this Drawable.- Overrides:
getConstantStatein classDrawable- Returns:
- The ConstantState associated to that Drawable.
- See Also:
-
getOutline
Description copied from class:DrawableCalled to get the drawable to populate the Outline that defines its drawing area.This method is called by the default
ViewOutlineProviderto define the outline of the View.The default behavior defines the outline to be the bounding rectangle of 0 alpha. Subclasses that wish to convey a different shape or alpha value must override this method.
- Overrides:
getOutlinein classDrawable- See Also:
-
mutate
Description copied from class:DrawableMake 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:DrawableClears the mutated state, allowing this drawable to be cached and mutated again.- Overrides:
clearMutatedin classDrawable
-