又是一周開(kāi)始,但是心情不是很爽,被擺了一道。小小的鄙視一下!
老板不地道,咱該干咱的活就得干咱的活。
今天就記錄一下上周要實(shí)現(xiàn)的一個(gè)小功能,就是給文字描邊。
第一種實(shí)現(xiàn)思路
就是用兩個(gè)TextView疊加,描邊的TextView為底,實(shí)體TextView疊加在上面。
具體代碼如下:
/*
* StrokeTextView的目標(biāo)是給文字描邊
* 實(shí)現(xiàn)方法是兩個(gè)TextView疊加,只有描邊的TextView為底,實(shí)體TextView疊加在上面
* 看上去文字就有個(gè)不同顏色的邊框了
*/
@SuppressLint("AppCompatCustomView")
public class StrokeTextView extends TextView {
private TextView contentText = null;///用于描邊的TextView
private float borderWidth = 10;
private int borderColor = Color.RED;
public StrokeTextView(Context context) {
this(context,null);
contentText = new TextView(context);
}
public StrokeTextView(Context context, AttributeSet attrs) {
this(context, attrs,0);
contentText = new TextView(context,attrs);
}
public StrokeTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
contentText = new TextView(context,attrs,defStyle);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
borderWidth = typedArray.getDimension(R.styleable.StrokeTextView_boder_width, 5);
borderColor = typedArray.getColor(R.styleable.StrokeTextView_boder_color,0xffffff);
typedArray.recycle();
contentText.setTextColor(getTextColors());
init();
}
public void init(){
TextPaint tp1 = this.getPaint();
tp1.setStrokeWidth(borderWidth); //設(shè)置描邊寬度
tp1.setStyle(Paint.Style.STROKE); //設(shè)置畫筆為邊框模式
tp1.setColor(borderColor);
//對(duì)文字只描邊
this.setTextColor(borderColor); //設(shè)置描邊顏色
this.setGravity(getGravity());
}
@Override
public void setLayoutParams (ViewGroup.LayoutParams params){
super.setLayoutParams(params);
contentText.setLayoutParams(params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
CharSequence tt = contentText.getText();
//兩個(gè)TextView上的文字必須一致
if(tt== null || !tt.equals(this.getText())){
contentText.setText(getText());
this.postInvalidate();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
contentText.measure(widthMeasureSpec, heightMeasureSpec);
}
protected void onLayout (boolean changed, int left, int top, int right, int bottom){
super.onLayout(changed, left, top, right, bottom);
contentText.layout(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
contentText.draw(canvas);
}
}
自定義屬性
<declare-styleable name="StrokeTextView">
<attr name="boder_width" format="dimension"/>
<attr name="boder_color" format="color"/>
</declare-styleable>
參數(shù)說(shuō)明:
boder_widht
:設(shè)置描邊寬度;
boder_color
:設(shè)置描邊顏色
如何使用應(yīng)該就不用贅述了,就當(dāng)作一個(gè)普通的textView就可以。需要描邊就把邊框和顏色加上。
第二種實(shí)現(xiàn)思路
上一種是用了兩個(gè)TextView,我們用同一個(gè)TextView,繪制兩次,分別用不用的顏色不一樣可以實(shí)現(xiàn)這個(gè)效果嗎?試一試!
代碼如下
@SuppressLint("AppCompatCustomView")
public class StrokeTextView1 extends TextView {
private boolean hasStroke = false; // 默認(rèn)不采用描邊
private Paint mTextPaint;
private float borderWidth;
private int borderColor;
private ColorStateList textColor;
public StrokeTextView1(Context context) {
this(context,null);
}
public StrokeTextView1(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public StrokeTextView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
m_TextPaint = getPaint();
init(context,attrs,defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView1);
hasStroke = ta.getBoolean(R.styleable.StrokeTextView1_has_border,false);
borderWidth = ta.getDimension(R.styleable.StrokeTextView1_border_width, 5);
borderColor = ta.getColor(R.styleable.StrokeTextView1_border_color,0xffffff);
ta.recycle();
textColor = getTextColors();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (hasStroke) {
// 描外層
this.setTextColor(borderColor);
mTextPaint.setStrokeWidth(borderWidth); // 描邊寬度
mTextPaint.setStyle(Paint.Style.STROKE); //描邊種類
mTextPaint.setFakeBoldText(true); // 外層text采用粗體
mTextPaint.setShadowLayer(1, 0, 0, 0); //字體的陰影效果,可以忽略
super.onDraw(canvas);
// 描內(nèi)層,恢復(fù)原先的畫筆
this.setTextColor(textColor);
mTextPaint.setStrokeWidth(0);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setFakeBoldText(false);
mTextPaint.setShadowLayer(0, 0, 0, 0);
}
super.onDraw(canvas);
}
}
就是重寫了onDraw()方法,外層描邊,內(nèi)側(cè)填充。
其中自定義屬性
<declare-styleable name="StrokeTextView1" >
<attr name="has_boder" format="boolean"/>
<attr name="border_width" format="dimension"/>
<attr name="border_color" format="color"/>
</declare-styleable>
參數(shù)說(shuō)明:
has_border
:是否描邊
border_widht
:設(shè)置描邊寬度
border_color
:設(shè)置描邊顏色
使用同樣很簡(jiǎn)單,設(shè)置這幾個(gè)屬性就可以實(shí)現(xiàn)描邊的效果。