代码实现控件的背景选择器和边框形状

代码如下:

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;

public class DrawableUtils {

	public static Drawable colorDrawable(int color){
		if(color!=0){
			return new ColorDrawable(color);
		}
		return null;
	}
	
	public static ColorStateList colorStateList(int ncolor, int pcolor){
		if(ncolor!=-1 && pcolor!=-1){
			int[][] states = new int[2][];
			states[0] = new int[]{android.R.attr.state_checked};
			states[1] = new int[]{android.R.attr.state_enabled};
			
			int[] colors = {pcolor, ncolor};
			ColorStateList list = new ColorStateList(states, colors);
			return list;
		}
		return null;
	}
	
	public static ShapeDrawable shapeDrawable(int color, int radius){
		float[] outerR = new float[]{radius, radius, radius, radius, radius, radius, radius, radius };
		// 构造一个圆角矩形,可以使用其他形状,这样ShapeDrawable 就会根据形状来绘制。
		RoundRectShape roundRectShape = new RoundRectShape(outerR, null, null);
		// 如果要构造直角矩形可以
		//RectShape rectShape = new RectShape();
		// 组合圆角矩形和ShapeDrawable
		ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
		// 设置形状的颜色
		shapeDrawable.getPaint().setColor(color);
		// 设置绘制方式为填充
		shapeDrawable.getPaint().setStyle(Paint.Style.FILL);   
		return shapeDrawable;
	}
	
	public static ShapeDrawable borderDrawable(int color, int radius, int borderWidth){
		float[] outerR = new float[]{radius, radius, radius, radius, radius, radius, radius, radius };
		RoundRectShape roundRectShape = new RoundRectShape(outerR, null, null);
		ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
		shapeDrawable.getPaint().setColor(color);
		shapeDrawable.getPaint().setStrokeWidth(borderWidth);
		shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
		return shapeDrawable;
	}
	
	public static Drawable checkedSelector(int ncolor, int pcolor){
		if(ncolor!=-1 && pcolor!=-1){
			StateListDrawable selector  = new StateListDrawable();
			selector.addState(new int[]{android.R.attr.state_checked}, new ColorDrawable(pcolor));
			selector.addState(new int[]{android.R.attr.state_enabled}, new ColorDrawable(ncolor));
			return selector;
		}
		return null;
	}
	
	public static Drawable checkedSelector(Context context, int nid, int cid){
		if(nid!=-1 && cid!=-1){
			Resources res = context.getResources();
			StateListDrawable selector  = new StateListDrawable();
			selector.addState(new int[]{android.R.attr.state_checked}, res.getDrawable(cid));
			selector.addState(new int[]{android.R.attr.state_enabled}, res.getDrawable(nid));
			return selector;
		}
		return null;
	}
	
	public static Drawable pressedSelector(int ncolor, int pcolor){
		if(ncolor!=-1 && pcolor!=-1){
			StateListDrawable selector  = new StateListDrawable();
			selector.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(pcolor));
			selector.addState(new int[]{android.R.attr.state_enabled}, new ColorDrawable(ncolor));
			return selector;
		}
		return null;
	}
	
	public static Drawable pressedSelector(Context context, int nid, int pid){
		if(nid!=-1 && pid!=-1){
			Resources res = context.getResources();
			StateListDrawable selector  = new StateListDrawable();
			selector.addState(new int[]{android.R.attr.state_pressed}, res.getDrawable(pid));
			selector.addState(new int[]{android.R.attr.state_enabled}, res.getDrawable(nid));
			return selector;
		}
		return null;
	}

}


你可能感兴趣的:(代码实现控件的背景选择器和边框形状)