Set up a shadow in Android is very simple, just two steps:
- Eleavation set value (height)
- Outline or add a background (i.e., the shape of the shadow)
Description:
View is through the size of the position x, y is determined, and now has the concept of the z-axis, and this
z
value isView的高度(elevation)
, the高度决定了阴影(shadow)的大小
.
NOTE: z value of View consists of two parts, elevation and translationZ (Android properties which are newly introduced L).
eleavation
: Static member: Set the component "float" up height;translationZ
: Is used for animation: the assembly is provided displaced in the Z direction (vertical screen direction).
Z(阴影大小) = elevation + translationZ
Use to define attributes in the layout:
android:elevation="10dp"
android:translationZ="10dp"
Using java code
View.setElevation(float);
View.setTranslationZ(float);
for example:
TextView to set up android:background
and elevation
(height determines the size of the shadow).
<TextView
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="20dp"
android:elevation="10dp"
android:translationZ="1dp"
android:text="阴影效果"
android:gravity="center"
android:background="@drawable/myrect" />
Then define a xml (myrect.xml, the paper put rec / drawable directory) background outline profile (profile determines the default background shadow shape):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" />
<corners android:radius="7dp" />
</shape>
When a profile view of this background drawable, rounded shadows projected view. Providing a custom profile, you can override the default view of the shape of the shadow.
The effect is as follows: