引言:常規(guī)shape的用法
當(dāng)我們?cè)贏ndroid項(xiàng)目中畫(huà)一個(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);
來(lái)實(shí)例化后使用句占。一直以來(lái)暮蹂,我以為shape對(duì)應(yīng)的是ShapeDrawable,然而我打個(gè)斷點(diǎn)然痊,看到的卻是這樣的
沒(méi)錯(cuò),xml中的shape被解析后實(shí)例化的是GradientDrawable焦人,不相信的具體可以去查看源碼募疮。
GradientDrawable實(shí)例化用法
當(dāng)計(jì)設(shè)圖里有很多圓角背景草讶,但我們又不相創(chuàng)造那么多xml的時(shí)候逝嚎,就可以用GradientDrawable
來(lái)實(shí)例化對(duì)象扁瓢。
/**
* @param roundRadius
* 圓角幅度
* @param bgfillColor
* 背景填充色
* @param bgfillColorAlpha
* 背景填充色透明度 double類(lèi)型范圍(0 ~ 1)
* @param strokeWidth
* 邊框?qū)挾?(0 無(wú)邊框)
* @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í)例化用法
說(shuō)完GradientDrawable
,我們來(lái)看一下ShapeDrawable
怎么用呢更扁。其實(shí)在沒(méi)有用GradientDrawable之前民褂,我是一直在用ShapeDrawable來(lái)寫(xiě)這種背景的
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;
}
有沒(méi)有發(fā)現(xiàn)RoundRectShape
第二個(gè)第三個(gè)參數(shù)傳的都是null
.
public RoundRectShape(@Nullable float[] outerRadii, @Nullable RectF inset, @Nullable float[] innerRadii)
這個(gè)類(lèi)一共有3個(gè)參數(shù),我在上面的注解已經(jīng)說(shuō)明了.
注意!這3個(gè)參數(shù)都是可以為null
的.意思就是,你可以取消任意一個(gè).獲得一些其他效果,比如設(shè)置第二個(gè)和第三個(gè)參數(shù)為null
,你就可以得到一個(gè)實(shí)心的圓角矩形.
如果你給后兩個(gè)參數(shù)設(shè)置了,效果圖大概是這個(gè)樣子:
innerRadii
比較好理解就是圓角的弧度疯潭,inset
代表的是圖里面黑色部分的厚度赊堪,中間白色的部分其實(shí)是父對(duì)象的背景。
ShapeDrawable
除了可以畫(huà)GradientDrawable
能畫(huà)的純色圖外竖哩,還可以畫(huà)很多別的圖形哭廉,比如扇形,或者是自定義的path圖相叁。更多關(guān)于ShapeDrawable用法可以參考Android開(kāi)發(fā) ShapeDrawable詳解