❶ android裡面layer-list中的inset和clip到底有什麼作用
Inset Drawable:用於通過指定的間距把圖片插入到XML中,它在View需要比自身小的背景時常用。有些像padding的作用。例子:
第一步:drawable文件中建立inset_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<inset
xmlns:android="http://schemas。android。com/apk/res/android"
android:drawable="@drawable/photo2"
android:insetTop="100dp"
android:insetRight="100dp"
android:insetBottom="200dp"
android:insetLeft="100dp" />
第二部,在xml中引用
<LinearLayout xmlns:android="http://schemas。android。com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/inset_drawable">
Clip Drawable:可以剪載圖片顯示,例如,可以通過它來做進度度。你可以選擇是從水平或垂直方向剪載。其中的gravity設置從整個部件的哪裡開始。例子:
第一步,在drawable文件中建立:clip_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas。android。com/apk/res/android"
android:drawable="@drawable/test_img"
android:clipOrientation="horizontal"
android:gravity="left" />
第二步,在ImageView中引用:
<LinearLayout xmlns:android="http://schemas。android。com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/clipimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/clip_drawable"/>
</LinearLayout>
Dev Guide中在ImageView中設置的是android:background="@drawable/clip_drawable",但是我使用background的時,會在程序中報空指針的錯誤。
最後,使用程序控制:
ImageView imageView=(ImageView)findViewById(R.id.clipimage);
ClipDrawable clipDrawable=(ClipDrawable)imageView.getDrawable();
clipDrawable.setLevel(5000);
level的值為0到10000 。當值為10000時圖全部顯示。