The elevation of a view determines the size of its shadow: views with higher Z values cast bigger shadows. Views only cast shadows on the Z=0 plane under an orthographic projection (the views do not scale for different values of Z).
Elevation is also useful to create animations where widgets temporarily rise above the view plane when performing some action.
The Z value for a view has two components, elevation and translation. The elevation is the static component, and the translation is used for animations:
Z = elevation + translationZ
To set the elevation of a view:
- In a layout definition, use the
- In the code of an activity, use the
To set the translation of a view, use the
methods enable you to easily animate the elevation of views. For more information, see
the API reference for
ViewPropertyAnimator and the Property Animation
The Z values are measured in the same units as the X and Y values.
Shadows and Outlines
The bounds of a view's background drawable determine the default shape of its shadow. Outlines represent the outer shape of a graphics object and define the ripple area for touch feedback.
For example, if you define a view with a background drawable:
<TextView android:id="@+id/myview" ... android:elevation="2dp" android:background="@drawable/myrect" />
where the background drawable is defined as a rectangle with rounded corners:
<!-- res/drawable/myrect.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#42000000" /> <corners android:radius="5dp" /> </shape>
Then this view and drawable cast the appropriate shadow.
You can also create outlines in your code using the methods in the
and you can assign them to views with the
To prevent a view from casting a shadow, set its outline to
Clip a view to its outline area using the
View.setClipToOutline method. Only rectangle, circle, and round rectangle outlines
support clipping, as determined by the
To clip a view to the shape of a drawable, set the drawable as the background of the view
(as shown above) and call the
Because clipping views is an expensive operation, don't animate the shape you use to clip a view. To achieve this effect, use a Reveal Effect animation.