android LayerDrawable 图层的实现

package com.demo.laydrable;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class LayerDrawableDemoActivity extends Activity {
	/** Called when the activity is first created. */
	private ImageView mImageView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mImageView = (ImageView) this.findViewById(R.id.imageView);
		//分层图像的使用
		Bitmap bm = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);
		Drawable[] array = new Drawable[3];
		array[0] = new PaintDrawable(Color.RED); // 黑色
		array[1] = new PaintDrawable(Color.WHITE); // 白色
		array[2] = new BitmapDrawable(bm); // 位图资源
		LayerDrawable ld = new LayerDrawable(array); // 参数为上面的Drawable数组
		//这个方法用于设置指定索引Drawble的边界
		//(int index, int l, int t, int r, int b) 
		ld.setLayerInset(1, 1, 1, 1, 1);   
		ld.setLayerInset(2, 2, 2, 2, 2);  
		
		mImageView.setImageDrawable(ld);
		
		//当然也可以使用xml实现,放在drawble资源文件夹下
		/*
		 * <?xml version="1.0" encoding="utf-8"?>
		 * <layer-list xmlns:android="http://schemas.android.com/apk/res/android">    
		 * 		<item>      
		 * 			<bitmap android:src="@drawable/android_red"        
		 * 					android:gravity="center" />    
		 * 			</item>    
		 * 			<item android:top="10dp" android:left="10dp">      
		 * 				<bitmap android:src="@drawable/android_green"        
		 * 					android:gravity="center" />    
		 * 			</item>    
		 * 			<item android:top="20dp" android:left="20dp">      
		 * 				<bitmap android:src="@drawable/android_blue"        
		 * 				android:gravity="center" />    
		 * 			</item>
		 * </layer-list>
		 */
		 
	}
}

你可能感兴趣的:(android,xml,Class,encoding)