自定義ShapeTextView 其實就是代碼代替xml實現(xiàn)shape的過程
屬性的定義
每個View都有一些它的特殊屬性难礼,在創(chuàng)建新的View的時候脑又,應(yīng)該考慮到它所具有的屬性并在
在res-values-styles
文件中定義View需要的屬性。關(guān)于屬性的介紹可參考繪制鐘表
<declare-styleable name="ShapeTextView">
<attr name="shape" format="enum">
<enum name="rectangle" value="0" />
<enum name="oval" value="1" />
</attr>
<attr name="solidNormal" format="color" />
<attr name="solidPressed" format="color" />
<attr name="cornersRadius" format="dimension" />
<attr name="cornerTopLeft" format="dimension" />
<attr name="cornerTopRight" format="dimension" />
<attr name="cornerBottomLeft" format="dimension" />
<attr name="cornerBottomRight" format="dimension" />
<attr name="strokeWidth" format="dimension" />
<attr name="strokeColor" format="color" />
</declare-styleable>
屬性 | 類型 | 作用 |
---|---|---|
shape | enum | 枚舉類型扬霜,定義了oval 和rectangle 常用的兩種 |
solidNormal | color | 填充色(正常顯示) |
solidPressed | color | 填充色(點擊顯示) |
cornersRadius | dimension | 圓角半徑(shape:rectangle )可用 |
cornerTopLeft、cornerTopRight均牢、cornerBottomLeft涩禀、cornerBottomRight | dimension | 自定義每個角的半徑,不能同時設(shè)置cornersRadius 屬性咆繁,或設(shè)置cornersRadius 為0 |
strokeWidth | dimension | 描邊的寬度 |
strokeColor | color | 描邊的顏色 |
創(chuàng)建ShapeTextView
繼承TextView
在構(gòu)造方法中獲取自定義的屬性
public ShapeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView);
shape = array.getInteger(R.styleable.ShapeTextView_shape, SHAPE_RECTANGEL);
solidNormalColor = array.getColor(R.styleable.ShapeTextView_solidNormal, Color.parseColor("#00000000"));
solidPressedColor = array.getColor(R.styleable.ShapeTextView_solidPressed, Color.parseColor("#00000000"));
cornersRadius = array.getDimension(R.styleable.ShapeTextView_cornersRadius, 0);
cornersTopLeft = array.getDimension(R.styleable.ShapeTextView_cornerTopLeft, 0);
cornersTopRight = array.getDimension(R.styleable.ShapeTextView_cornerTopRight, 0);
cornersBottomLeft = array.getDimension(R.styleable.ShapeTextView_cornerBottomLeft, 0);
cornersBottomRight = array.getDimension(R.styleable.ShapeTextView_cornerBottomRight, 0);
strokeWidth = array.getDimension(R.styleable.ShapeTextView_strokeWidth, 0);
strokeColor = array.getColor(R.styleable.ShapeTextView_strokeColor, Color.parseColor("#00000000"));
array.recycle();
}
實現(xiàn)shape
標簽
使用
GradientDrawable
類在代碼中實現(xiàn)shape
標簽中的屬性
// normal state
GradientDrawable drawableNormal = new GradientDrawable();
// 設(shè)置Shape
drawableNormal.setShape(shape);
// 設(shè)置圓角半徑
drawableNormal.setCornerRadius(cornersRadius);
// 圓角半徑(每個圓角半徑的值)
if (cornersRadius == 0) {
drawableNormal.setCornerRadii(new float[]{
cornersTopLeft, cornersTopLeft,
cornersTopRight, cornersTopRight,
cornersBottomRight, cornersBottomRight,
cornersBottomLeft, cornersBottomLeft});
}
//描邊的寬度和顏色
drawableNormal.setStroke((int) strokeWidth, strokeColor);
//設(shè)置填充色
drawableNormal.setColor(solidNormalColor);
實現(xiàn)selector
標簽
使用
StateListDrawable
在代碼中實現(xiàn)selector
標簽中的屬性
// 設(shè)置背景選擇器
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed);
stateListDrawable.addState(new int[]{}, drawableNormal);
// 設(shè)置視圖的背景
setBackground(stateListDrawable);
重寫onDraw()
方法
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setShape();//方法內(nèi)主要內(nèi)容為上面代碼段
}
效果預(yù)覽
enter image description here