1、GradientDrawable
生成一個有漸變的Drawable
public static Drawable getGradientDrawable(int[] colors, int cornerRadius)
GradientDrawable gradientDrawableN = new GradientDrawable();
gradientDrawableN.setCornerRadius(cornerRadius);
gradientDrawableN.setOrientation(TL_BR);//top left to bottom right
gradientDrawableN.setColors(colors);//colors的長度必須大于等于2
return gradientDrawableN;
}
public static Drawable getDrawable(int color, int cornerRadius) {
GradientDrawable drawable = new GradientDrawable();
drawable.setCornerRadius(cornerRadius);//設(shè)置4個角的弧度
drawable.setColor(color);// 設(shè)置顏色
return drawable;
}
2、StateListDrawable
多狀態(tài)Drawable
public static Drawable getStateListDrawable() {
Drawable drawable_p = getDrawable(Color.GREEN,100);//正常狀態(tài)下的Drawable
Drawable drawable_n = getDrawable(Color.RED,100);//按下和獲取焦點是的Drawable
Drawable drawable_b = getDrawable(Color.YELLOW,100);//被禁用時的Drawable
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, drawable_p);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, drawable_p);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, drawable_n);
stateListDrawable.addState(new int[]{-android.R.attr.state_enabled,}, drawable_b);
stateListDrawable.addState(new int[]{-android.R.attr.state_enabled, android.R.attr.state_pressed}, drawable_b);
stateListDrawable.addState(new int[]{-android.R.attr.state_enabled, android.R.attr.state_focused}, drawable_b);
stateListDrawable.addState(new int[]{}, drawable_n);
return stateListDrawable;
}
3蜂厅、LayerDrawable
多個Drawable組合成一個Drawable
public static Drawable getLayerDrawable(int[] solidColors, int[] strokeColors, int cornerRadius, int solidWidth) {
Drawable solidDrawable = getXSLGradientDrawable(solidColors, cornerRadius);//外層Drawable
Drawable strokeDrawable = getXSLGradientDrawable(strokeColors, cornerRadius - (2 * solidWidth));//內(nèi)層Drawable
Drawable[] drawables = {solidDrawable, strokeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawables);
layerDrawable.setLayerInset(1, solidWidth, solidWidth, solidWidth, solidWidth)
return layerDrawable;
}