定义View的阴影
android 5.0增加了Z属性,用来控制View的高度,从而产生阴影效果
阴影是由Z属性控制,Z属性由两部分组成:
Elevation(海拔高度)
Translation(Z轴的平移)
XML 方式:
XML
1
2
3
4
5
6
7
android:id="@+id/v1"
android:layout_width="100dp"
android:layout_height="100dp"
android:translationZ="20dp"
android:elevation="20dp"
android:background="@drawable/shape_circle"/>
Java Code方式:
Java
1
2
3
4// 方法1
v.setTranslationZ(progress);
// 方法2
// v.setElevation(progress);
当阴影不起效果时:
http://stackoverflow.com/questions/26572048/elevation-on-android-lollipop-not-working给ImageView加入此背景xxx.drawable"http://schemas.android.com/apk/res/android"android:shape="oval">
裁剪View
Clipping views使你更容易改变View的外形。
Java Code:
1
2
3
4
5
6
7
8
9
10
11
12// 1.继承ViewOutlineProvider 并重写getOutline()方法
privateclassClipOutlineProviderextendsViewOutlineProvider {
@Override
publicvoidgetOutline(View view, Outline outline) {
finalintmargin = Math.min(view.getWidth(), view.getHeight()) /10;
outline.setRoundRect(margin, margin, view.getWidth() - margin, view.getHeight() - margin, margin /2);
}
}
// 2.设置OutlineProvider
clippedView.setOutlineProvider(newClipOutlineProvider());
// 3.使用时setClipToOutline为true,否则为false
clippedView.setClipToOutline(true);
网友评论