MainActivity如下:
package cc.testlayerdrawable; import android.os.Bundle; import android.widget.ImageView; import android.app.Activity; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; /** * Demo描述: * LayerDrawable使用示例 * 1 改变SeekBar的外观 * 2 利用LayerDrawable使ImageView一次加载多张图片 * 2.1 xml方式实现 * 2.2 代码方式实现 * * 示例备注: * 使用LayerDrawable时系统将会按这些Drawable对象的数组顺序来绘制它们 * 索引最大的Drawable对象将会被绘制在最上面.这一点在采用代码实现 * LayerDrawable的时候得以很好的体现.可见参考资料2. * * 参考资料 * 1 Android疯狂讲义(第二版) 作者李刚 * 2 http://wang-peng1.iteye.com/blog/657275 * 3 http://blog.csdn.net/lee576/article/details/7825930 * Thank you very much * */ public class MainActivity extends Activity { private ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } //利用LayerDrawable(代码)使ImageView一次加载多张图片 private void init(){ mImageView=(ImageView) findViewById(R.id.imageView); Drawable [] drawables=new Drawable[2]; drawables[0]=getResources().getDrawable(R.drawable.e); drawables[1]=getResources().getDrawable(R.drawable.ic_launcher); LayerDrawable layerDrawable=new LayerDrawable(drawables); mImageView.setImageDrawable(layerDrawable); } }
main.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <SeekBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progressDrawable="@drawable/seekbarlayer"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/imageviewlayer" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
imageviewlayer.xml如下:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <bitmap android:src="@drawable/e" android:gravity="center"/> </item> <item android:top="25dp" android:left="25dp"> <bitmap android:src="@drawable/ic_launcher" android:gravity="center"/> </item> <item android:top="60dp" android:left="55dp"> <bitmap android:src="@drawable/ic_launcher" android:gravity="center"/> </item> </layer-list>
seekbarlayer.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 定义轨道的背景 --> <item android:id="@android:id/background" android:drawable="@drawable/e" /> <!-- 定义轨道上已完成部分的外观--> <item android:id="@android:id/progress" android:drawable="@drawable/ic_launcher" /> </layer-list>