有两种方法将多个图片层叠,合并为一个图片显示:
1. xml
建立user_profile.xml文件:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/face_bg"
android:drawable="@drawable/ic_contact_picture" />
<item android:id="@+id/face"
android:drawable="@drawable/emo_im_cool" />
</layer-list>
2. 代码中实现:
ImageView profileImageView = (ImageView)findViewById(R.id.user_profile);
Drawable[] layerlist = new Drawable[2];
layerlist[0] = getResources().getDrawable(R.drawable.ic_contact_picture);
layerlist[1] = getResources().getDrawable(R.drawable.emo_im_cool);
LayerDrawable layerDrawable = new LayerDrawable(layerlist);
profileImageView.setImageDrawable(layerDrawable);
3. 引用方式
和引用普通图片的引用方式一样:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_user_profile" />
<ImageView
android:id="@+id/user_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_user_profile" />
4. 效果
两种实现的效果是一样的: