引言:常規(guī)shape的用法
當(dāng)我們在Android項(xiàng)目中畫一個(gè)圓角矩形的時(shí)候,我們通常會(huì)這樣先在res/drawable里建一個(gè)drawable resouce file,比如round_rect.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorPrimary"/>
<corners android:radius="10dp"/>
</shape>
然后在xml中設(shè)置給xml,或都直接用Drawable draw = getResources().getDrawable(R.drawable.round_rect);來實(shí)例化后使用。一直以來,我以為shape對應(yīng)的是ShapeDrawable,然而我打個(gè)斷點(diǎn),看到的卻是這樣的

沒錯(cuò),xml中的shape被解析后實(shí)例化的是GradientDrawable,不相信的具體可以去查看源碼。
GradientDrawable實(shí)例化用法
當(dāng)計(jì)設(shè)圖里有很多圓角背景,但我們又不相創(chuàng)造那么多xml的時(shí)候,就可以用GradientDrawable來實(shí)例化對象。
/**
* @param roundRadius
* 圓角幅度
* @param bgfillColor
* 背景填充色
* @param bgfillColorAlpha
* 背景填充色透明度 double類型范圍(0 ~ 1)
* @param strokeWidth
* 邊框?qū)挾?(0 無邊框)
* @param strokeColor
* 邊框顏色
* @return
*/
public static GradientDrawable myCustomShape(int roundRadius,
int bgfillColor, double bgfillColorAlpha, int strokeWidth,
int strokeColor) {
GradientDrawable gradientDrawable = new GradientDrawable();
if (0 != bgfillColorAlpha) {// 設(shè)置為0 則為全透明 不需要設(shè)置以下參數(shù)
gradientDrawable.setColor(bgfillColor);
gradientDrawable.setAlpha((int) (bgfillColorAlpha * 255));
}
gradientDrawable.setCornerRadius(roundRadius);
gradientDrawable.setStroke(strokeWidth, strokeColor == 0 ? bgfillColor
: strokeColor);
return gradientDrawable;
}
當(dāng)然你也可以給每個(gè)角設(shè)置不同的圓角弧度,如果是有兩種狀態(tài)的,則可以用StateListDrawable,比如
/**
* // 1、2兩個(gè)參數(shù)表示左上角,3、4表示右上角,5、6表示右下角,7、8表示左下角
* @param nor_bg
* @param nor_stroke
* @param sel_bg
* @param sel_stroke
* @param topLeft
* @param bottomLeft
* @param topRight
* @param bottomRight
* @return
*/
public static Drawable getDrawable(int nor_bg,int nor_stroke,int sel_bg,int sel_stroke, int topLeft, int bottomLeft, int topRight,
int bottomRight){
StateListDrawable bg = new StateListDrawable();
GradientDrawable normal_drawable = new GradientDrawable();
normal_drawable.setColor(nor_bg);
normal_drawable.setCornerRadii(new float[] { topLeft, topLeft,
topRight, topRight, bottomRight, bottomRight, bottomLeft,
bottomLeft });
normal_drawable.setStroke(Util.toDip(1), sel_stroke);
GradientDrawable checked_drawable = new GradientDrawable();
checked_drawable.setColor(sel_bg);
checked_drawable.setCornerRadii(new float[] { topLeft, topLeft,
topRight, topRight, bottomRight, bottomRight, bottomLeft,
bottomLeft });
checked_drawable.setStroke(Util.toDip(1), sel_stroke);
bg.addState(new int[] { android.R.attr.state_checked },
checked_drawable);
bg.addState(new int[] {}, normal_drawable);
return bg;
}
更多的屬性大家可以根據(jù)需要自己進(jìn)行封裝。
ShapeDrawable實(shí)例化用法
說完GradientDrawable,我們來看一下ShapeDrawable怎么用呢。其實(shí)在沒有用GradientDrawable之前,我是一直在用ShapeDrawable來寫這種背景的
static public ShapeDrawable getRoundRect(int color,float roundLeftTop,float roundRightTop,float roundRightBottom,float roundLeftBottom){
ShapeDrawable shapeDrawable = new ShapeDrawable();
float[] outRadii=new float[]{roundLeftTop,roundLeftTop,roundRightTop,roundRightTop,roundRightBottom,roundRightBottom,roundLeftBottom,roundLeftBottom};
RoundRectShape shape=new RoundRectShape(outRadii,null,null);
shapeDrawable.setShape(shape);
shapeDrawable.getPaint().setColor(color);
return shapeDrawable;
}
有沒有發(fā)現(xiàn)RoundRectShape 第二個(gè)第三個(gè)參數(shù)傳的都是null.
public RoundRectShape(@Nullable float[] outerRadii, @Nullable RectF inset, @Nullable float[] innerRadii) 這個(gè)類一共有3個(gè)參數(shù),我在上面的注解已經(jīng)說明了.
注意!這3個(gè)參數(shù)都是可以為null的.意思就是,你可以取消任意一個(gè).獲得一些其他效果,比如設(shè)置第二個(gè)和第三個(gè)參數(shù)為null,你就可以得到一個(gè)實(shí)心的圓角矩形.
如果你給后兩個(gè)參數(shù)設(shè)置了,效果圖大概是這個(gè)樣子:

innerRadii 比較好理解就是圓角的弧度,inset 代表的是圖里面黑色部分的厚度,中間白色的部分其實(shí)是父對象的背景。
ShapeDrawable除了可以畫GradientDrawable能畫的純色圖外,還可以畫很多別的圖形,比如扇形,或者是自定義的path圖。更多關(guān)于ShapeDrawable用法可以參考Android開發(fā) ShapeDrawable詳解