MaterialShapeDrawable

public class MaterialShapeDrawable implements Shapeable

Known direct subclasses
ChipDrawable

ChipDrawable contains all the layout and draw logic for Chip.


Base drawable class for Material Shapes that handles shadows, elevation, scale and color for a generated path.

Summary

Nested types

@Retention(value = RetentionPolicy.SOURCE)
public annotation MaterialShapeDrawable.CompatibilityShadowMode

Determines when compatibility shadow is drawn vs. native elevation shadows.

Constants

static final int

Always draw fake shadows, never draw native elevation shadows.

static final int

Try to draw native elevation shadows if possible, otherwise use fake shadows.

static final int

Never draw fake shadows.

Public fields

int
final Region

Public constructors

MaterialShapeDrawable(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

static MaterialShapeDrawable
createWithElevationOverlay(Context context)

Returns a MaterialShapeDrawable with the elevation overlay functionality initialized, a fill color of colorSurface, and an elevation of 0.

static MaterialShapeDrawable
createWithElevationOverlay(Context context, float elevation)

Returns a MaterialShapeDrawable with the elevation overlay functionality initialized, a fill color of colorSurface, and an elevation of elevation.

static MaterialShapeDrawable
createWithElevationOverlay(
    Context context,
    float elevation,
    ColorStateList backgroundTint
)

Returns a MaterialShapeDrawable with the elevation overlay functionality initialized, a fill color of backgroundTint, and an elevation of elevation.

void
draw(Canvas canvas)
int
float

Returns the actual size of the bottom left corner for the current bounds.

float

Returns the actual size of the bottom right corner for the current bounds.

ConstantState
float

Returns the elevation used to render both fake shadows when requiresCompatShadow is true and elevation overlays.

ColorStateList

Get the color used for the fill.

float

Get the interpolation of the path, between 0 and 1.

int
void
getOutline(Outline outline)
boolean
getPadding(Rect padding)
Style

Get the current style used by the shape's paint.

float

Returns the parent absolute elevation.

void
getPathForSize(int width, int height, Path path)

This method is deprecated.

see ShapeAppearancePathProvider

int

Get the tint color factoring in any other runtime modifications such as elevation overlays.

float

Get the scale of the rendered path.

int

Returns the rotation offset applied to the fake shadow which is drawn when requiresCompatShadow is true.

int
int

This method is deprecated.

use getElevation instead.

int

Returns the X offset of the shadow from the bounds of the shape.

int

Returns the Y offset of the shadow from the bounds of the shape.

int

Get the shadow radius rendered by the path in pixels.

ShapeAppearanceModel

Get the ShapeAppearanceModel containing the path that will be rendered in this drawable.

ShapePathModel

This method is deprecated.

Use getShapeAppearanceModel instead.

ColorStateList

Get the color used for the stroke.

ColorStateList

Get the stroke's current ColorStateList.

float

Get the stroke width used by the shape's paint.

ColorStateList

Get the tint list used by the shape's paint.

float

Returns the actual size of the top left corner for the current bounds.

float

Returns the actual size of the top right corner for the current bounds.

float

Returns the translationZ used to render both fake shadows when requiresCompatShadow is true and elevation overlays.

Region
float

Returns the visual z position of this drawable, in pixels.

void
initializeElevationOverlay(Context context)

Initializes the elevation overlay functionality for this drawable.

void
boolean

Returns whether the elevation overlay functionality is initialized and enabled in this drawable's theme context.

boolean

Returns whether the elevation overlay functionality has been initialized for this drawable.

boolean

Determines whether a point is contained within the transparent region of the Drawable.

boolean

This method is deprecated.

use getShadowCompatibilityMode instead

boolean
Drawable
boolean

Returns true if compat shadows should be drawn.

void
setAlpha(int alpha)
void
setColorFilter(ColorFilter colorFilter)
void

Updates the corners for the given CornerSize.

void
setCornerSize(float cornerSize)

Updates the corners for the given CornerSize.

void
void
setElevation(float elevation)

Sets the elevation used to render both fake shadows when requiresCompatShadow is true and elevation overlays.

void
setFillColor(ColorStateList fillColor)

Set the color used for the fill.

void
setInterpolation(float interpolation)

Set the interpolation of the path, between 0 and 1.

void
setPadding(int left, int top, int right, int bottom)

Configure the padding of the shape

void
setPaintStyle(Style paintStyle)

Set the style used by the shape's paint.

void
setParentAbsoluteElevation(float parentAbsoluteElevation)

Sets the parent absolute elevation, which is used to render elevation overlays.

void
setScale(float scale)

Set the scale of the rendered path.

void
void
setShadowColor(int shadowColor)

Set the color of fake shadow rendered behind the shape.

void
setShadowCompatRotation(int shadowRotation)

Set the rotation offset applied to the fake shadow which is drawn when requiresCompatShadow is true.

void

Set the shadow compatibility mode.

void
setShadowElevation(int shadowElevation)

This method is deprecated.

use setElevation instead.

void
setShadowEnabled(boolean shadowEnabled)

This method is deprecated.

use setShadowCompatibilityMode instead.

void
setShadowRadius(int shadowRadius)

This method is deprecated.

use setElevation instead.

void

Set the ShapeAppearanceModel containing the path that will be rendered in this drawable.

void

This method is deprecated.

Use setShapeAppearanceModel instead.

void
setStroke(float strokeWidth, ColorStateList strokeColor)

Set the shape's stroke width and stroke color using a ColorStateList.

void
setStroke(float strokeWidth, int strokeColor)

Set the shape's stroke width and stroke color.

void
setStrokeColor(ColorStateList strokeColor)

Set the color used for the stroke.

void
setStrokeTint(int tintColor)

Set the shape's stroke color.

void
setStrokeTint(ColorStateList tintList)

Set the shape's stroke ColorStateList

void
setStrokeWidth(float strokeWidth)

Set the stroke width used by the shape's paint.

void
setTint(int tintColor)
void
setTintList(ColorStateList tintList)
void
setTintMode(Mode tintMode)
void
setTranslationZ(float translationZ)

Sets the translationZ used to render both fake shadows when requiresCompatShadow is true and elevation overlays.

void
setUseTintColorForShadow(boolean useTintColorForShadow)

Set whether fake shadow color should match next set tint color.

void
setZ(float z)

Sets the visual z position of this view, in pixels.

Protected methods

int
compositeElevationOverlayIfNeeded(int backgroundColor)
RectF
void
onBoundsChange(Rect bounds)
boolean
onStateChange(int[] state)

Constants

SHADOW_COMPAT_MODE_ALWAYS

public static final int SHADOW_COMPAT_MODE_ALWAYS = 2

Always draw fake shadows, never draw native elevation shadows. If a path could be concave, this will prevent the shadow from suddenly being rendered natively.

SHADOW_COMPAT_MODE_DEFAULT

public static final int SHADOW_COMPAT_MODE_DEFAULT = 0

Try to draw native elevation shadows if possible, otherwise use fake shadows. This is best for paths which will always be convex. If the path might change to be concave, you should consider using SHADOW_COMPAT_MODE_ALWAYS otherwise the shadows could suddenly switch from native to fake in the middle of an animation.

SHADOW_COMPAT_MODE_NEVER

public static final int SHADOW_COMPAT_MODE_NEVER = 1

Never draw fake shadows. You may want to enable this if backwards compatibility for shadows isn't as important as performance. Native shadow elevation shadows will still be drawn if possible.

Public fields

resolvedTintColor

public int resolvedTintColor

transparentRegion

public final Region transparentRegion

Public constructors

MaterialShapeDrawable

public MaterialShapeDrawable()

MaterialShapeDrawable

public MaterialShapeDrawable(ShapeAppearanceModel shapeAppearanceModel)

MaterialShapeDrawable

public MaterialShapeDrawable(ShapePathModel shapePathModel)

MaterialShapeDrawable

public MaterialShapeDrawable(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

createWithElevationOverlay

public static MaterialShapeDrawable createWithElevationOverlay(Context context)

Returns a MaterialShapeDrawable with the elevation overlay functionality initialized, a fill color of colorSurface, and an elevation of 0.

See compositeOverlayIfNeeded for information on when the overlay will be active.

createWithElevationOverlay

public static MaterialShapeDrawable createWithElevationOverlay(Context context, float elevation)

Returns a MaterialShapeDrawable with the elevation overlay functionality initialized, a fill color of colorSurface, and an elevation of elevation.

See compositeOverlayIfNeeded for information on when the overlay will be active.

createWithElevationOverlay

public static MaterialShapeDrawable createWithElevationOverlay(
    Context context,
    float elevation,
    ColorStateList backgroundTint
)

Returns a MaterialShapeDrawable with the elevation overlay functionality initialized, a fill color of backgroundTint, and an elevation of elevation. When backgroundTint is null, colorSurface will be used as default.

See compositeOverlayIfNeeded for information on when the overlay will be active.

draw

public void draw(Canvas canvas)

getAlpha

public int getAlpha()

getBottomLeftCornerResolvedSize

public float getBottomLeftCornerResolvedSize()

Returns the actual size of the bottom left corner for the current bounds.

getBottomRightCornerResolvedSize

public float getBottomRightCornerResolvedSize()

Returns the actual size of the bottom right corner for the current bounds.

getConstantState

public ConstantState getConstantState()

getElevation

public float getElevation()

Returns the elevation used to render both fake shadows when requiresCompatShadow is true and elevation overlays. This value is the same as the native elevation that would be used to render shadows on API 21 and up.

getFillColor

public ColorStateList getFillColor()

Get the color used for the fill.

Returns
ColorStateList

the color set on the Paint object responsible for the fill.

getInterpolation

public float getInterpolation()

Get the interpolation of the path, between 0 and 1. Ranges between 0 (none) and 1 (fully) interpolated.

Returns
float

the interpolation of the path.

getOpacity

public int getOpacity()

getOutline

public void getOutline(Outline outline)

getPadding

public boolean getPadding(Rect padding)

getPaintStyle

public Style getPaintStyle()

Get the current style used by the shape's paint.

Returns
Style

current used paint style.

getParentAbsoluteElevation

public float getParentAbsoluteElevation()

Returns the parent absolute elevation.

getPathForSize

public void getPathForSize(int width, int height, Path path)

getResolvedTintColor

public int getResolvedTintColor()

Get the tint color factoring in any other runtime modifications such as elevation overlays.

getScale

public float getScale()

Get the scale of the rendered path. A value of 1 renders it at 100% size.

Returns
float

the scale of the path.

getShadowCompatRotation

public int getShadowCompatRotation()

Returns the rotation offset applied to the fake shadow which is drawn when requiresCompatShadow is true.

getShadowElevation

public int getShadowElevation()

Get the shadow elevation rendered by the path.

getShadowOffsetX

public int getShadowOffsetX()

Returns the X offset of the shadow from the bounds of the shape.

getShadowOffsetY

public int getShadowOffsetY()

Returns the Y offset of the shadow from the bounds of the shape.

getShadowRadius

public int getShadowRadius()

Get the shadow radius rendered by the path in pixels. This method should be used only when the actual size of the shadow is required. Usually getElevation should be used instead to get the actual elevation of this view as it might be different.

getShapeAppearanceModel

public ShapeAppearanceModel getShapeAppearanceModel()

Get the ShapeAppearanceModel containing the path that will be rendered in this drawable.

Returns
ShapeAppearanceModel

the current model.

getShapedViewModel

public ShapePathModel getShapedViewModel()

Get the ShapePathModel containing the path that will be rendered in this drawable.

Returns
ShapePathModel

the current model.

getStrokeColor

public ColorStateList getStrokeColor()

Get the color used for the stroke.

Returns
ColorStateList

the color set on the Paint object responsible for the stroke.

getStrokeTintList

public ColorStateList getStrokeTintList()

Get the stroke's current ColorStateList.

Returns
ColorStateList

the stroke's current ColorStateList.

getStrokeWidth

public float getStrokeWidth()

Get the stroke width used by the shape's paint.

Returns
float

the stroke's current width.

getTintList

public ColorStateList getTintList()

Get the tint list used by the shape's paint.

getTopLeftCornerResolvedSize

public float getTopLeftCornerResolvedSize()

Returns the actual size of the top left corner for the current bounds.

getTopRightCornerResolvedSize

public float getTopRightCornerResolvedSize()

Returns the actual size of the top right corner for the current bounds.

getTranslationZ

public float getTranslationZ()

Returns the translationZ used to render both fake shadows when requiresCompatShadow is true and elevation overlays. This value is the same as the native translationZ that would be used to render shadows on API 21 and up.

getTransparentRegion

public Region getTransparentRegion()

getZ

public float getZ()

Returns the visual z position of this drawable, in pixels. This is equivalent to the translationZ property plus the current elevation property.

initializeElevationOverlay

public void initializeElevationOverlay(Context context)

Initializes the elevation overlay functionality for this drawable.

See compositeOverlayIfNeeded for information on when the overlay will be active.

invalidateSelf

public void invalidateSelf()

isElevationOverlayEnabled

public boolean isElevationOverlayEnabled()

Returns whether the elevation overlay functionality is initialized and enabled in this drawable's theme context.

isElevationOverlayInitialized

public boolean isElevationOverlayInitialized()

Returns whether the elevation overlay functionality has been initialized for this drawable.

isPointInTransparentRegion

public boolean isPointInTransparentRegion(int x, int y)

Determines whether a point is contained within the transparent region of the Drawable. A return value of true generally suggests that the touched view should not process a touch event at that point.

Parameters
int x

The X coordinate of the point.

int y

The Y coordinate of the point.

Returns
boolean

true iff the point is contained in the transparent region of the Drawable.

isShadowEnabled

public boolean isShadowEnabled()

Get shadow rendering status for shadows when requiresCompatShadow is true.

Returns
boolean

true if fake shadows should be drawn, false otherwise.

isStateful

public boolean isStateful()

mutate

public Drawable mutate()

requiresCompatShadow

public boolean requiresCompatShadow()

Returns true if compat shadows should be drawn. Native elevation shadows can't be drawn for concave paths on API <29.

setAlpha

public void setAlpha(int alpha)

setColorFilter

public void setColorFilter(ColorFilter colorFilter)

setCornerSize

public void setCornerSize(CornerSize cornerSize)

Updates the corners for the given CornerSize.

setCornerSize

public void setCornerSize(float cornerSize)

Updates the corners for the given CornerSize.

setEdgeIntersectionCheckEnable

public void setEdgeIntersectionCheckEnable(boolean enable)

setElevation

public void setElevation(float elevation)

Sets the elevation used to render both fake shadows when requiresCompatShadow is true and elevation overlays. This value is the same as the native elevation that would be used to render shadows on API 21 and up.

setFillColor

public void setFillColor(ColorStateList fillColor)

Set the color used for the fill.

Parameters
ColorStateList fillColor

the color set on the Paint object responsible for the fill.

setInterpolation

public void setInterpolation(float interpolation)

Set the interpolation of the path, between 0 and 1. Ranges between 0 (none) and 1 (fully) interpolated. An interpolation of 1 generally indicates a fully rendered path, while an interpolation of 0 generally indicates a fully healed path, which is usually a rectangle.

Parameters
float interpolation

the desired interpolation.

setPadding

public void setPadding(int left, int top, int right, int bottom)

Configure the padding of the shape

Parameters
int left

Left padding of the shape

int top

Top padding of the shape

int right

Right padding of the shape

int bottom

Bottom padding of the shape

setPaintStyle

public void setPaintStyle(Style paintStyle)

Set the style used by the shape's paint.

Parameters
Style paintStyle

the desired style.

setParentAbsoluteElevation

public void setParentAbsoluteElevation(float parentAbsoluteElevation)

Sets the parent absolute elevation, which is used to render elevation overlays.

setScale

public void setScale(float scale)

Set the scale of the rendered path. A value of 1 renders it at 100% size.

Parameters
float scale

the desired scale.

setShadowBitmapDrawingEnable

public void setShadowBitmapDrawingEnable(boolean enable)

setShadowColor

public void setShadowColor(int shadowColor)

Set the color of fake shadow rendered behind the shape. This will only be drawn when requiresCompatShadow is true, otherwise native elevation shadows will be drawn which don't support colored shadows.

Setting a shadow color will prevent the tint color from being used.

Parameters
int shadowColor

desired color.

setShadowCompatRotation

public void setShadowCompatRotation(int shadowRotation)

Set the rotation offset applied to the fake shadow which is drawn when requiresCompatShadow is true. 0 degrees will draw the shadow below the shape.

This allows for the Drawable to be wrapped in a , or rotated in a view while still having the fake shadow to appear to be drawn from the bottom.

setShadowCompatibilityMode

public void setShadowCompatibilityMode(
    @MaterialShapeDrawable.CompatibilityShadowMode int mode
)

Set the shadow compatibility mode. This allows control over when fake shadows should drawn instead of native elevation shadows.

Note: To prevent clipping of fake shadow for views on API levels above lollipop, the parent view must disable clipping of children by calling setClipChildren, or by setting `android:clipChildren="false"` in xml. `clipToPadding` may also need to be false if there is any padding on the parent that could intersect the shadow.

setShadowElevation

public void setShadowElevation(int shadowElevation)

Set the shadow elevation rendered by the path.

Parameters
int shadowElevation

the desired elevation.

setShadowEnabled

public void setShadowEnabled(boolean shadowEnabled)

Set shadow rendering to be enabled or disabled when requiresCompatShadow is true. Setting this to false could provide some performance benefits on older devices if you don't mind no shadows being drawn.

Note: native elevation shadows will still be drawn on API 21 and up if the shape is convex and the view with this background has elevation.

Parameters
boolean shadowEnabled

true if fake shadows should be drawn; false if not.

setShadowRadius

public void setShadowRadius(int shadowRadius)

Set the shadow radius rendered by the path.

Parameters
int shadowRadius

the desired shadow radius.

setShapeAppearanceModel

public void setShapeAppearanceModel(ShapeAppearanceModel shapeAppearanceModel)

Set the ShapeAppearanceModel containing the path that will be rendered in this drawable.

Parameters
ShapeAppearanceModel shapeAppearanceModel

the desired model.

setShapedViewModel

public void setShapedViewModel(ShapePathModel shapedViewModel)

Set the ShapePathModel containing the path that will be rendered in this drawable.

Parameters
ShapePathModel shapedViewModel

the desired model.

setStroke

public void setStroke(float strokeWidth, ColorStateList strokeColor)

Set the shape's stroke width and stroke color using a ColorStateList.

Parameters
float strokeWidth

a float for the width of the stroke.

ColorStateList strokeColor

the ColorStateList for the shape's stroke.

setStroke

public void setStroke(float strokeWidth, int strokeColor)

Set the shape's stroke width and stroke color.

Parameters
float strokeWidth

a float for the width of the stroke.

int strokeColor

an int representing the Color to use for the shape's stroke.

setStrokeColor

public void setStrokeColor(ColorStateList strokeColor)

Set the color used for the stroke.

Parameters
ColorStateList strokeColor

the color set on the Paint object responsible for the stroke.

setStrokeTint

public void setStrokeTint(int tintColor)

Set the shape's stroke color.

Parameters
int tintColor

an int representing the Color to use for the shape's stroke.

setStrokeTint

public void setStrokeTint(ColorStateList tintList)

Set the shape's stroke ColorStateList

Parameters
ColorStateList tintList

the ColorStateList for the shape's stroke.

setStrokeWidth

public void setStrokeWidth(float strokeWidth)

Set the stroke width used by the shape's paint.

Parameters
float strokeWidth

desired stroke width.

setTint

public void setTint(int tintColor)

setTintList

public void setTintList(ColorStateList tintList)

setTintMode

public void setTintMode(Mode tintMode)

setTranslationZ

public void setTranslationZ(float translationZ)

Sets the translationZ used to render both fake shadows when requiresCompatShadow is true and elevation overlays. This value is the same as the native translationZ that would be used to render shadows on API 21 and up.

setUseTintColorForShadow

public void setUseTintColorForShadow(boolean useTintColorForShadow)

Set whether fake shadow color should match next set tint color. This will only be drawn when requiresCompatShadow is true, otherwise native elevation shadows will be drawn which don't support colored shadows.

Parameters
boolean useTintColorForShadow

true if color should match; false otherwise.

setZ

public void setZ(float z)

Sets the visual z position of this view, in pixels. This is equivalent to setting the translationZ property to be the difference between the z value passed in and the current elevation property.

Protected methods

compositeElevationOverlayIfNeeded

protected int compositeElevationOverlayIfNeeded(int backgroundColor)

getBoundsAsRectF

protected RectF getBoundsAsRectF()

onBoundsChange

protected void onBoundsChange(Rect bounds)

onStateChange

protected boolean onStateChange(int[] state)