TextView 文字描邊

又是一周開(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)描邊的效果。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 各種純css圖標(biāo) CSS3可以實(shí)現(xiàn)很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫出這些圖形,要比...
    劍殘閱讀 9,666評(píng)論 0 8
  • 概述 在網(wǎng)易云課堂學(xué)習(xí)李南江老師的《從零玩轉(zhuǎn)HTML5前端+跨平臺(tái)開(kāi)發(fā)》時(shí),所整理的筆記。筆記內(nèi)容為根據(jù)個(gè)人需求所...
    墨荀閱讀 2,367評(píng)論 0 7
  • 一、CSS入門 1、css選擇器 選擇器的作用是“用于確定(選定)要進(jìn)行樣式設(shè)定的標(biāo)簽(元素)”。 有若干種形式的...
    寵辱不驚丶?xì)q月靜好閱讀 1,618評(píng)論 0 6
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 6,510評(píng)論 0 17
  • 本文是針對(duì)剛學(xué)編程的小白,都是一些基礎(chǔ)知識(shí),如果想了解更多深層一點(diǎn)的東西,歡迎移步本人博客!! 博客地址 點(diǎn)擊跳轉(zhuǎn)...
    西巴擼閱讀 551評(píng)論 0 0