一個簡單的工具類绎晃。主要是為了方便當你需要一個帶圓形邊框的textview的時候,可以不用寫一大堆的drawable杂曲。下面是代碼
public class CircleTextView extends AppCompatTextView {
private Paint circlePaint;
private Paint backPaint;
private Paint textPaint;
private int storkColor = Color.WHITE;
private int circleBackColor = Color.WHITE;
private float storkWidth;
public CircleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setGravity(Gravity.CENTER);
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setStyle(Paint.Style.STROKE);
backPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
backPaint.setStyle(Paint.Style.FILL);
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
storkWidth = 0;
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleTextView);
storkColor = typedArray.getColor(R.styleable.CircleTextView_storkColor, storkColor);
circleBackColor = typedArray.getColor(R.styleable.CircleTextView_backColor, circleBackColor);
storkWidth = typedArray.getDimension(R.styleable.CircleTextView_storkWidth, storkWidth);
typedArray.recycle();
}
if (storkWidth != 0) {
circlePaint.setStrokeWidth(storkWidth);
circlePaint.setColor(storkColor);
}
backPaint.setColor(circleBackColor);
textPaint.setColor(getCurrentTextColor());
textPaint.setTextSize(getTextSize());
}
public CircleTextView(Context context) {
this(context, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getHeight();
int width = getWidth();
int radius;
int storkRadius;
int textWidth = (int) textPaint.measureText(getText().toString());
if (width > height) {
if (height > textWidth) {
radius = height;
} else {
setHeight(textWidth + getPaddingTop() + getPaddingBottom());
radius = textWidth;
}
} else {
if (width > textWidth) {
radius = width;
} else {
setWidth(textWidth + getPaddingRight() + getPaddingLeft());
radius = textWidth;
}
}
storkRadius= (int) (radius/2-storkWidth);
radius= storkRadius-1;
if (storkWidth != 0)
canvas.drawCircle(getWidth() / 2, getHeight() / 2, storkRadius, circlePaint);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, backPaint);
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
canvas.drawText(getText().toString(), getWidth() / 2 - textPaint.measureText(getText().toString()) / 2, getHeight() / 2 - fontMetrics.descent + (fontMetrics.bottom - fontMetrics.top) / 2, textPaint);
}
public void setMyStorkColor(@ColorInt int color) {
this.storkColor = color;
circlePaint.setColor(storkColor);
invalidate();
}
public void setBackColor(@ColorInt int color) {
this.circleBackColor = color;
backPaint.setColor(circleBackColor);
invalidate();
}
public void setMyTextColor(@ColorInt int color) {
textPaint.setColor(color);
invalidate();
}
}
之后是attr屬性
<declare-styleable name="CircleTextView">
<attr name="storkColor" format="color"/>
<attr name="backColor" format="color"/>
<attr name="storkWidth" format="dimension"/>
</declare-styleable>
思路也很簡單庶艾,主要是先在背景上把邊框的圓畫出來,之后是背景的圓擎勘,最后把文字畫出來咱揍。因為繼承了TextView,感覺不用自己測量了棚饵。下面是一張效果圖
需要注意的是煤裙,當寬高是wrap_content時,需要自己設置padding
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者