layer-list:两张图片和成一张图片
state-list:状态图,如button按钮的点击,点击前与点击时的状态颜色不一样。
level-list:打分图片
shape:绘制形状图片
布局代码:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical"> 5 6 <ImageView 7 android:id="@+id/iv" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:src="@drawable/layerlist"/> 11 12 <Button 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:onClick="onClick" 16 android:text=" 下 一 张" 17 android:background="@drawable/shape"/> 18 </LinearLayout>
MainActivity:
1 package com.android.hzy.drawable; 2 import android.app.Activity; 3 import android.graphics.drawable.LayerDrawable; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.ImageView; 7 8 public class MainActivity extends Activity { 9 10 private ImageView iv; 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 iv = (ImageView) findViewById(R.id.iv); 17 } 18 19 public void onClick(View v){ 20 LayerDrawable layerDrawable = (LayerDrawable) getResources().getDrawable(R.drawable.layerlist); 21 // 替换图片 22 layerDrawable.setDrawableByLayerId(R.id.user, getResources().getDrawable(R.drawable.ic_launcher)); 23 // 将图片复制给imageview 24 iv.setImageDrawable(layerDrawable); 25 } 26 }
res/drawable下创建与之对应的xml
layerlist.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <item android:drawable="@drawable/faceback"/> 5 <item 6 android:id="@+id/user" 7 android:bottom="22dp" 8 android:drawable="@drawable/user" 9 android:left="18dp" 10 android:right="18dp" 11 android:top="68dp"/> 12 13 </layer-list>
statelist.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <selector xmlns:android="http://schemas.android.com/apk/res/android" > 3 <!-- 按下采用的是什么图片 --> 4 <item android:state_pressed="true" android:drawable="@drawable/bg_selected"></item> 5 <!-- 默认状态就是个正常状态,因此默认状态的条目要放在最后写 --> 6 <item android:drawable="@drawable/bg_normal"></item> 7 8 </selector>
levellist.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <level-list xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <item 5 android:drawable="@drawable/faceback" 6 android:maxLevel="10" 7 android:minLevel="0"/> 8 <item 9 android:drawable="@drawable/faceback" 10 android:maxLevel="20" 11 android:minLevel="11"/> 12 13 </level-list>
shape.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- 3 android:shape=["rectangle" | "oval" | "line" | "ring"]:表示图形的形状 4 corners :代表圆角 5 gradient : 渐变色 6 padding:内边距 7 size:图片的大小 8 solid:内填充色 和 gradient 只能使用一个 9 stroke:边线 (虚线图片) 10 --> 11 <shape 12 xmlns:android="http://schemas.android.com/apk/res/android" 13 android:shape="rectangle"> 14 <corners 15 android:radius="3dp" /> 16 <gradient 17 android:centerColor="#D0F4F9" 18 android:endColor="#4BED23" 19 android:startColor="#5FF2FA" /> 20 <padding 21 android:left="2dp" 22 android:top="2dp" 23 android:right="2dp" 24 android:bottom="2dp" /> 25 26 <stroke 27 android:width="1dp" 28 android:color="#f00" 29 android:dashWidth="10dp" 30 android:dashGap="2dp" /> 31 </shape>