1. 程式人生 > >Android 自定義TextView實現描邊

Android 自定義TextView實現描邊

前言:

這裡呢就是給自己的做的專案一些小功能做一個筆記。也希望能對大家能有幫助。

正文:

這裡就簡單的自定義了TextView 描邊的效果。更多效果,

大家可以參照這個大神的:點選開啟連結      以及這位大神:點選開啟連結

我這裡寫的就不像上面兩位大神功能那麼強大了。

第一步:好現在就來開始我們的自定義的第一步吧!!!

public class StrokeTextView extends android.support.v7.widget.AppCompatTextView {

    public TextView borderText;///用於描邊的TextView
    private int colors;

    public StrokeTextView(Context context) {
        this(context,null);
    }
 
    public StrokeTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
 
    public StrokeTextView(Context context, AttributeSet attrs,int defStyle) {
        super(context, attrs, defStyle);
        borderText = new TextView(context,attrs);
        init(attrs);
    }
 
    public void init(AttributeSet attrs){

        TypedArray ta = getContext().obtainStyledAttributes(attrs,R.styleable.StrokeTextView);
        colors = ta.getColor(R.styleable.StrokeTextView_stroke_color,0xFFFFFF);
        ta.recycle();

        TextPaint tp1 = borderText.getPaint();                //new
        tp1.setStrokeWidth(4);                                  //設定描邊寬度
        tp1.setStyle(Style.STROKE);                             //對文字只描邊
        borderText.setTextColor(colors);                        //設定描邊顏色
        borderText.setGravity(getGravity());
    }

    @Override
    public void setLayoutParams (ViewGroup.LayoutParams params){
        super.setLayoutParams(params);
        borderText.setLayoutParams(params);
    }

    /**
     * onMeasure通過父View傳遞過來的大小和模式,
     * 以及自身的背景圖片的大小得出自身最終的大小,
     * 然後通過setMeasuredDimension()方法設定給mMeasuredWidth和mMeasuredHeight.
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        CharSequence tt = borderText.getText();
         
        //兩個TextView上的文字必須一致
        if(tt== null || !tt.equals(this.getText())){
            borderText.setText(getText());
            this.postInvalidate();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        borderText.measure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 該方法是View的放置方法,在View類實現。
     * 呼叫該方法需要傳入放置View的矩形空間左上角left、top值和右下角right、bottom值。
     * 這四個值是相對於父控制元件而言的。
     *
     * @param changed
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    protected void onLayout (boolean changed, int left, int top, int right, int bottom){
        super.onLayout(changed, left, top, right, bottom);
        borderText.layout(left, top, right, bottom);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        borderText.draw(canvas);
        super.onDraw(canvas);
    }

}


第二步:在Values資原始檔中去定義自定義TextView 在xml佈局中使用的屬性

第三步:在xml佈局中去使用自定義控制元件

注意:需要使用app:...自定義屬性需要在根佈局加上這句

 xmlns:app="http://schemas.android.com/apk/res-auto"


第四步:為了方便使用我在自定義檔案中(StrokeTextView.java)

裡面的 borderText 定義為了 public .這樣就省了我再去定義一個方法

在程式碼中使用也是很方便的,使用Butterknife(黃油刀)去到到控制元件。
當然你也可以去findViewbyId去找到控制元件

  接下來就直接使用就好了

找到控制元件後

這樣使用就可以啦

就這樣就好啦。好了到這裡簡單的自定義TextView描邊就結束了