Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

MaterialShapeDrawable

public class MaterialShapeDrawable
extends Drawable implements TintAwareDrawable, Shapeable

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ com.google.android.material.shape.MaterialShapeDrawable
Known Direct Subclasses


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

Summary

Nested classes

@interface MaterialShapeDrawable.CompatibilityShadowMode

Determines when compatibility shadow is drawn vs. 

Constants

int SHADOW_COMPAT_MODE_ALWAYS

Always draw fake shadows, never draw native elevation shadows.

int SHADOW_COMPAT_MODE_DEFAULT

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

int SHADOW_COMPAT_MODE_NEVER

Never draw fake shadows.

Public constructors

MaterialShapeDrawable()
MaterialShapeDrawable(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
MaterialShapeDrawable(ShapePathModel shapePathModel)
MaterialShapeDrawable(ShapeAppearanceModel shapeAppearanceModel)

Public methods

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)

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

void draw(Canvas canvas)
float getBottomLeftCornerResolvedSize()

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

float getBottomRightCornerResolvedSize()

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

Drawable.ConstantState getConstantState()
float getElevation()

Returns the elevation used to render both fake shadows when ERROR(/#requiresCompatShadow()) is true and elevation overlays.

ColorStateList getFillColor()

Get the color used for the fill.

float getInterpolation()

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

int getOpacity()
void getOutline(Outline outline)
boolean getPadding(Rect padding)
Paint.Style getPaintStyle()

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

float getParentAbsoluteElevation()

Returns the parent absolute elevation.

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

This method is deprecated. see ShapeAppearancePathProvider

float getScale()

Get the scale of the rendered path.

int getShadowCompatRotation()

Returns the rotation offset applied to the fake shadow which is drawn when ERROR(/#requiresCompatShadow()) is true.

int getShadowCompatibilityMode()
int getShadowElevation()

This method is deprecated. use getElevation() instead.

int getShadowOffsetX()

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

int getShadowOffsetY()

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

int getShadowRadius()

Get the shadow radius rendered by the path in pixels.

ShapeAppearanceModel getShapeAppearanceModel()

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

ShapePathModel getShapedViewModel()

This method is deprecated. Use getShapeAppearanceModel() instead.

ColorStateList getStrokeColor()

Get the color used for the stroke.

ColorStateList getStrokeTintList()

Get the stroke's current ColorStateList.

float getStrokeWidth()

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

ColorStateList getTintList()

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

float getTopLeftCornerResolvedSize()

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

float getTopRightCornerResolvedSize()

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

float getTranslationZ()

Returns the translationZ used to render both fake shadows when ERROR(/#requiresCompatShadow()) is true and elevation overlays.

Region getTransparentRegion()
float getZ()

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

void initializeElevationOverlay(Context context)

Initializes the elevation overlay functionality for this drawable.

void invalidateSelf()
boolean isElevationOverlayEnabled()

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

boolean isElevationOverlayInitialized()

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

boolean isPointInTransparentRegion(int x, int y)

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

boolean isShadowEnabled()

This method is deprecated. use getShadowCompatibilityMode() instead

boolean isStateful()
Drawable mutate()
void setAlpha(int alpha)
void setColorFilter(ColorFilter colorFilter)
void setCornerSize(float cornerSize)

Updates the corners for the given ERROR(/cornerSize).

void setCornerSize(CornerSize cornerSize)

Updates the corners for the given ERROR(/cornerSize).

void setElevation(float elevation)

Sets the elevation used to render both fake shadows when ERROR(/#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(Paint.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 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 ERROR(/#requiresCompatShadow()) is true.

void setShadowCompatibilityMode(int mode)

Set the shadow compatibility mode.

void setShadowElevation(int shadowElevation)

This method is deprecated. use setElevation(float) instead.

void setShadowEnabled(boolean shadowEnabled)

This method is deprecated. use setShadowCompatibilityMode(int) instead.

void setShadowRadius(int shadowRadius)

This method is deprecated. use setElevation(float) instead.

void setShapeAppearanceModel(ShapeAppearanceModel shapeAppearanceModel)

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

void setShapedViewModel(ShapePathModel shapedViewModel)

This method is deprecated. Use setShapeAppearanceModel(ShapeAppearanceModel) 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(ColorStateList tintList)

Set the shape's stroke ColorStateList

void setStrokeTint(int tintColor)

Set the shape's stroke color.

void setStrokeWidth(float strokeWidth)

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

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

Sets the translationZ used to render both fake shadows when ERROR(/#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

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

Inherited methods

From class android.graphics.drawable.Drawable
From class java.lang.Object
From interface androidx.core.graphics.drawable.TintAwareDrawable
From interface com.google.android.material.shape.Shapeable

Constants

SHADOW_COMPAT_MODE_ALWAYS

int SHADOW_COMPAT_MODE_ALWAYS

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.

Constant Value: 2 (0x00000002)

SHADOW_COMPAT_MODE_DEFAULT

int SHADOW_COMPAT_MODE_DEFAULT

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.

Constant Value: 0 (0x00000000)

SHADOW_COMPAT_MODE_NEVER

int SHADOW_COMPAT_MODE_NEVER

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.

Constant Value: 1 (0x00000001)

Public constructors

MaterialShapeDrawable

MaterialShapeDrawable ()

MaterialShapeDrawable

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

MaterialShapeDrawable

MaterialShapeDrawable (ShapePathModel shapePathModel)

Parameters
shapePathModel ShapePathModel

MaterialShapeDrawable

MaterialShapeDrawable (ShapeAppearanceModel shapeAppearanceModel)

Parameters
shapeAppearanceModel ShapeAppearanceModel: the ShapeAppearanceModel containing the path that will be rendered in this drawable.

Public methods

createWithElevationOverlay

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(int, float) for information on when the overlay will be active.

Parameters
context Context

elevation float

Returns
MaterialShapeDrawable

createWithElevationOverlay

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(int, float) for information on when the overlay will be active.

Parameters
context Context

Returns
MaterialShapeDrawable

draw

void draw (Canvas canvas)

Parameters
canvas Canvas

getBottomLeftCornerResolvedSize

float getBottomLeftCornerResolvedSize ()

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

Returns
float

getBottomRightCornerResolvedSize

float getBottomRightCornerResolvedSize ()

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

Returns
float

getConstantState

Drawable.ConstantState getConstantState ()

Returns
Drawable.ConstantState

getElevation

float getElevation ()

Returns the elevation used to render both fake shadows when ERROR(/#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.

Returns
float

getFillColor

ColorStateList getFillColor ()

Get the color used for the fill.

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

getInterpolation

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

int getOpacity ()

Returns
int

getOutline

void getOutline (Outline outline)

Parameters
outline Outline

getPadding

boolean getPadding (Rect padding)

Parameters
padding Rect

Returns
boolean

getPaintStyle

Paint.Style getPaintStyle ()

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

Returns
Paint.Style current used paint style.

getParentAbsoluteElevation

float getParentAbsoluteElevation ()

Returns the parent absolute elevation.

Returns
float

getPathForSize

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

This method is deprecated.
see ShapeAppearancePathProvider

Parameters
width int

height int

path Path

getScale

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

int getShadowCompatRotation ()

Returns the rotation offset applied to the fake shadow which is drawn when ERROR(/#requiresCompatShadow()) is true.

Returns
int

getShadowCompatibilityMode

int getShadowCompatibilityMode ()

Returns
int

getShadowElevation

int getShadowElevation ()

This method is deprecated.
use getElevation() instead.

Get the shadow elevation rendered by the path.

Returns
int

getShadowOffsetX

int getShadowOffsetX ()

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

Returns
int

getShadowOffsetY

int getShadowOffsetY ()

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

Returns
int

getShadowRadius

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 ERROR(/getElevation()) should be used instead to get the actual elevation of this view as it might be different.

Returns
int

getShapeAppearanceModel

ShapeAppearanceModel getShapeAppearanceModel ()

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

Returns
ShapeAppearanceModel the current model.

getShapedViewModel

ShapePathModel getShapedViewModel ()

This method is deprecated.
Use getShapeAppearanceModel() instead.

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

Returns
ShapePathModel the current model.

getStrokeColor

ColorStateList getStrokeColor ()

Get the color used for the stroke.

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

getStrokeTintList

ColorStateList getStrokeTintList ()

Get the stroke's current ColorStateList.

Returns
ColorStateList the stroke's current ColorStateList.

getStrokeWidth

float getStrokeWidth ()

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

Returns
float the stroke's current width.

getTintList

ColorStateList getTintList ()

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

Returns
ColorStateList

getTopLeftCornerResolvedSize

float getTopLeftCornerResolvedSize ()

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

Returns
float

getTopRightCornerResolvedSize

float getTopRightCornerResolvedSize ()

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

Returns
float

getTranslationZ

float getTranslationZ ()

Returns the translationZ used to render both fake shadows when ERROR(/#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.

Returns
float

getTransparentRegion

Region getTransparentRegion ()

Returns
Region

getZ

float getZ ()

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

Returns
float

initializeElevationOverlay

void initializeElevationOverlay (Context context)

Initializes the elevation overlay functionality for this drawable.

See compositeOverlayIfNeeded(int, float) for information on when the overlay will be active.

Parameters
context Context

invalidateSelf

void invalidateSelf ()

isElevationOverlayEnabled

boolean isElevationOverlayEnabled ()

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

Returns
boolean

isElevationOverlayInitialized

boolean isElevationOverlayInitialized ()

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

Returns
boolean

isPointInTransparentRegion

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
x int: The X coordinate of the point.

y int: The Y coordinate of the point.

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

isShadowEnabled

boolean isShadowEnabled ()

This method is deprecated.
use getShadowCompatibilityMode() instead

Get shadow rendering status for shadows when ERROR(/#requiresCompatShadow()) is true.

Returns
boolean true if fake shadows should be drawn, false otherwise.

isStateful

boolean isStateful ()

Returns
boolean

mutate

Drawable mutate ()

Returns
Drawable

setAlpha

void setAlpha (int alpha)

Parameters
alpha int

setColorFilter

void setColorFilter (ColorFilter colorFilter)

Parameters
colorFilter ColorFilter

setCornerSize

void setCornerSize (float cornerSize)

Updates the corners for the given ERROR(/cornerSize).

Parameters
cornerSize float

setCornerSize

void setCornerSize (CornerSize cornerSize)

Updates the corners for the given ERROR(/cornerSize).

Parameters
cornerSize CornerSize

setElevation

void setElevation (float elevation)

Sets the elevation used to render both fake shadows when ERROR(/#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.

Parameters
elevation float

setFillColor

void setFillColor (ColorStateList fillColor)

Set the color used for the fill.

Parameters
fillColor ColorStateList: the color set on the Paint object responsible for the fill.

setInterpolation

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
interpolation float: the desired interpolation.

setPadding

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

Configure the padding of the shape

Parameters
left int: Left padding of the shape

top int: Top padding of the shape

right int: Right padding of the shape

bottom int: Bottom padding of the shape

setPaintStyle

void setPaintStyle (Paint.Style paintStyle)

Set the style used by the shape's paint.

Parameters
paintStyle Paint.Style: the desired style.

setParentAbsoluteElevation

void setParentAbsoluteElevation (float parentAbsoluteElevation)

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

Parameters
parentAbsoluteElevation float

setScale

void setScale (float scale)

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

Parameters
scale float: the desired scale.

setShadowColor

void setShadowColor (int shadowColor)

Set the color of fake shadow rendered behind the shape. This will only be drawn when ERROR(/#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
shadowColor int: desired color.

setShadowCompatRotation

void setShadowCompatRotation (int shadowRotation)

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

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

Parameters
shadowRotation int

setShadowCompatibilityMode

void setShadowCompatibilityMode (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(boolean), 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.

Parameters
mode int

setShadowElevation

void setShadowElevation (int shadowElevation)

This method is deprecated.
use setElevation(float) instead.

Set the shadow elevation rendered by the path.

Parameters
shadowElevation int: the desired elevation.

setShadowEnabled

void setShadowEnabled (boolean shadowEnabled)

This method is deprecated.
use setShadowCompatibilityMode(int) instead.

Set shadow rendering to be enabled or disabled when ERROR(/#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
shadowEnabled boolean: true if fake shadows should be drawn; false if not.

setShadowRadius

void setShadowRadius (int shadowRadius)

This method is deprecated.
use setElevation(float) instead.

Set the shadow radius rendered by the path.

Parameters
shadowRadius int: the desired shadow radius.

setShapeAppearanceModel

void setShapeAppearanceModel (ShapeAppearanceModel shapeAppearanceModel)

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

Parameters
shapeAppearanceModel ShapeAppearanceModel: the desired model.

setShapedViewModel

void setShapedViewModel (ShapePathModel shapedViewModel)

This method is deprecated.
Use setShapeAppearanceModel(ShapeAppearanceModel) instead.

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

Parameters
shapedViewModel ShapePathModel: the desired model.

setStroke

void setStroke (float strokeWidth, 
                ColorStateList strokeColor)

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

Parameters
strokeWidth float: a float for the width of the stroke.

strokeColor ColorStateList: the ColorStateList for the shape's stroke.

setStroke

void setStroke (float strokeWidth, 
                int strokeColor)

Set the shape's stroke width and stroke color.

Parameters
strokeWidth float: a float for the width of the stroke.

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

setStrokeColor

void setStrokeColor (ColorStateList strokeColor)

Set the color used for the stroke.

Parameters
strokeColor ColorStateList: the color set on the Paint object responsible for the stroke.

setStrokeTint

void setStrokeTint (ColorStateList tintList)

Set the shape's stroke ColorStateList

Parameters
tintList ColorStateList: the ColorStateList for the shape's stroke.

setStrokeTint

void setStrokeTint (int tintColor)

Set the shape's stroke color.

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

setStrokeWidth

void setStrokeWidth (float strokeWidth)

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

Parameters
strokeWidth float: desired stroke width.

setTint

void setTint (int tintColor)

Parameters
tintColor int

setTintList

void setTintList (ColorStateList tintList)

Parameters
tintList ColorStateList

setTintMode

void setTintMode (PorterDuff.Mode tintMode)

Parameters
tintMode PorterDuff.Mode

setTranslationZ

void setTranslationZ (float translationZ)

Sets the translationZ used to render both fake shadows when ERROR(/#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.

Parameters
translationZ float

setUseTintColorForShadow

void setUseTintColorForShadow (boolean useTintColorForShadow)

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

Parameters
useTintColorForShadow boolean: true if color should match; false otherwise.

setZ

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.

Parameters
z float

Protected methods

getBoundsAsRectF

RectF getBoundsAsRectF ()

Returns
RectF

onBoundsChange

void onBoundsChange (Rect bounds)

Parameters
bounds Rect

onStateChange

boolean onStateChange (int[] state)

Parameters
state int

Returns
boolean