MaterialShapeDrawable
public
class
MaterialShapeDrawable
extends Drawable
implements
TintAwareDrawable,
ShapeAppearanceModel.OnChangedListener
| java.lang.Object | ||
| ↳ | android.graphics.drawable.Drawable | |
| ↳ | com.google.android.material.shape.MaterialShapeDrawable | |
|
|
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(ShapeAppearanceModel shapeAppearanceModel)
|
|
Public methods | |
|---|---|
static
MaterialShapeDrawable
|
createWithElevationOverlay(Context context, float elevation)
Returns a |
static
MaterialShapeDrawable
|
createWithElevationOverlay(Context context)
Returns a |
void
|
draw(Canvas canvas)
|
Drawable.ConstantState
|
getConstantState()
|
float
|
getElevation()
Returns the elevation used to render both fake shadows when |
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. |
void
|
getPathForSize(Rect bounds, Path path)
This method is deprecated. see ShapeAppearancePathProvider |
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 |
int
|
getShadowCompatibilityMode()
|
int
|
getShadowElevation()
This method is deprecated.
use |
int
|
getShadowRadius()
This method is deprecated.
use |
ShapeAppearanceModel
|
getShapeAppearanceModel()
Get the |
ShapeAppearanceModel
|
getShapedViewModel()
This method is deprecated.
Use |
ColorStateList
|
getStrokeColor()
Get the color used for the stroke. |
int
|
getStrokeTint()
This method is deprecated.
Use |
ColorStateList
|
getStrokeTintList()
Get the stroke's current |
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
|
getTranslationZ()
Returns the translationZ used to render both fake shadows when |
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
|
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 |
boolean
|
isStateful()
|
Drawable
|
mutate()
|
void
|
onShapeAppearanceModelChanged()
Callback invoked when a corner or edge of the |
void
|
setAlpha(int alpha)
|
void
|
setColorFilter(ColorFilter colorFilter)
|
void
|
setCornerRadius(float cornerRadius)
|
void
|
setElevation(float elevation)
Sets the elevation used to render both fake shadows when |
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
|
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 |
void
|
setShadowCompatibilityMode(int mode)
Set the shadow compatibility mode. |
void
|
setShadowElevation(int shadowElevation)
This method is deprecated.
use |
void
|
setShadowEnabled(boolean shadowEnabled)
This method is deprecated.
use |
void
|
setShadowRadius(int shadowRadius)
This method is deprecated.
use |
void
|
setShapeAppearanceModel(ShapeAppearanceModel shapeAppearanceModel)
Set the |
void
|
setShapedViewModel(ShapePathModel shapedViewModel)
This method is deprecated.
Use |
void
|
setStroke(float strokeWidth, ColorStateList strokeColor)
Set the shape's stroke width and stroke color using a |
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 |
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 |
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 | |
|---|---|
android.graphics.drawable.Drawable
| |
java.lang.Object
| |
androidx.core.graphics.drawable.TintAwareDrawable
| |
com.google.android.material.shape.ShapeAppearanceModel.OnChangedListener
| |
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 (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 layerOverlayIfNeeded(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 layerOverlayIfNeeded(int, float) for information on
when the overlay will be active.
| Parameters | |
|---|---|
context |
Context |
| Returns | |
|---|---|
MaterialShapeDrawable |
|
draw
void draw (Canvas canvas)
| Parameters | |
|---|---|
canvas |
Canvas |
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. |
getPathForSize
void getPathForSize (Rect bounds,
Path path)
This method is deprecated.
see ShapeAppearancePathProvider
| Parameters | |
|---|---|
bounds |
Rect |
path |
Path |
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 |
|
getShadowRadius
int getShadowRadius ()
This method is deprecated.
use getElevation() instead.
Get the shadow radius rendered by the path.
| Returns | |
|---|---|
int |
the shadow radius rendered by the path. |
getShapeAppearanceModel
ShapeAppearanceModel getShapeAppearanceModel ()
Get the ShapeAppearanceModel containing the path that will be rendered in this
drawable.
| Returns | |
|---|---|
ShapeAppearanceModel |
the current model. |
getShapedViewModel
ShapeAppearanceModel getShapedViewModel ()
This method is deprecated.
Use getShapeAppearanceModel() instead.
Get the ShapePathModel containing the path that will be rendered in this drawable.
| Returns | |
|---|---|
ShapeAppearanceModel |
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.
|
getStrokeTint
int getStrokeTint ()
This method is deprecated.
Use getStrokeTintList() instead.
Get the int representing the Color of the shape's stroke in the current state.
| Returns | |
|---|---|
int |
the stroke's current color. |
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 |
|
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 layerOverlayIfNeeded(int, float) for information on
when the overlay will be active.
| Parameters | |
|---|---|
context |
Context |
invalidateSelf
void invalidateSelf ()
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 |
|
onShapeAppearanceModelChanged
void onShapeAppearanceModelChanged ()
Callback invoked when a corner or edge of the ShapeAppearanceModel changes.
setAlpha
void setAlpha (int alpha)
| Parameters | |
|---|---|
alpha |
int |
setColorFilter
void setColorFilter (ColorFilter colorFilter)
| Parameters | |
|---|---|
colorFilter |
ColorFilter |
setCornerRadius
void setCornerRadius (float cornerRadius)
| Parameters | |
|---|---|
cornerRadius |
float |
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.
|
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 |
|
Annotations
Interfaces
Classes
- CornerTreatment
- CutCornerTreatment
- EdgeTreatment
- InterpolateOnScrollPositionChangeHelper
- MaterialShapeDrawable
- RoundedCornerTreatment
- ShapeAppearanceModel
- ShapePath
- ShapePath.PathArcOperation
- ShapePath.PathLineOperation
- ShapePath.PathOperation
- ShapePath.PathQuadOperation
- ShapePathModel
- TriangleEdgeTreatment
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.

