layerlist
1.多張圖片可以疊加在一起做為某個ImageView的背景。
可以在xml或代碼中實(shí)現(xiàn):
xml中實(shí)現(xiàn):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_launcher"/>
<item
android:drawable="@mipmap/ic_launcher"
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
<!--
android:left
android:top
android:right
android:bottom
這四個屬性表示四個方向上的padding
-->
</layer-list>
效果如下:
在代碼中實(shí)現(xiàn):
private void practiceLayerList() {
ImageView img = (ImageView) findViewById(R.id.layer_list_img);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Drawable[] drawables = new Drawable[3];
drawables[0] = new PaintDrawable(Color.BLACK);
drawables[1] = new PaintDrawable(Color.GREEN);
drawables[2] = new BitmapDrawable(getResources(), bm);
LayerDrawable layerDrawable = new LayerDrawable(drawables);
layerDrawable.setLayerInset(1, 20, 20, 20, 20);
layerDrawable.setLayerInset(2, 40, 40, 40, 40);
/*
* layerlist可以在xml中實(shí)現(xiàn)搞疗,也可以在代碼中設(shè)置
* 代碼中使用layerDrawable
* layerDrawble.setLayerInset()
* 有五個int類型的參數(shù)城菊,第一個表示drawable數(shù)組的下標(biāo)
* 剩下四個分表表示left,top,right,bottom方向向內(nèi)的padding
* */
img.setBackground(layerDrawable);
}
關(guān)于selector的記錄:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke android:width="1dp"
android:color="@color/colorAccent"/>
<solid
android:color="@color/colorPrimary"/>
<gradient android:startColor="@color/colorAccent"
android:centerColor="@color/colorPrimaryDark"
android:endColor="@color/colorPrimary"
android:angle="180"
/>
<!--
android:angle
0表示從左到右
90表示從下到上
180表示從右到左
-->
<corners android:radius="10dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke android:width="1dp"
android:color="@color/colorPrimary"/>
<solid android:color="@color/colorAccent"/>
<corners android:radius="10dp"/>
</shape>
</item>
</selector>
實(shí)現(xiàn)效果如下: