• 當前位置:首頁 > IT技術 > 移動平臺 > 正文

    Android自定義控件(五)---實戰篇(優化篇)
    2021-09-16 11:43:49

    上一篇,我們發現我們已經成功了,可是文字和四條邊挨得太近了,然而我們在布局文件里給 控件加 padding 發現并不起作用,不行你可以加著試試看。

    這是因為我們代碼里沒寫邏輯,哈哈哈,廢話不多說,直接上碼?。?!

    package com.example.mytextview;
    
    //import javax.swing.text.View;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.view.View;
    
    import androidx.annotation.Nullable;
    
    public class mTextView extends View {
        //1、設置自定義屬性變量
        private int mTextSize = 16;
        private int mTextColor = Color.RED;
        private String mText;
    
        //設置文字畫筆
        private Paint textPaint;
    
        public mTextView(Context context) {
            this(context,null);
        }
    
        public mTextView(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs,0);
        }
    
        public mTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
    
            //2、獲取裝有自定義屬性值的數值
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.mTextView);
            //3、精確獲取自定義屬性值
            mTextSize = typedArray.getDimensionPixelSize(R.styleable.mTextView_mTextSize,mTextSize);//源碼用的這個方法,參照 TextView
            mTextColor = typedArray.getColor(R.styleable.mTextView_mTextColor,mTextColor);
            mText = typedArray.getString(R.styleable.mTextView_mText);
            //4、回收 typedArray
            typedArray.recycle();
    
            initData();
        }
    
        private void initData() {
            textPaint = new Paint();
            //抗鋸齒
            textPaint.setAntiAlias(true);
            //設置顏色
            textPaint.setColor(mTextColor);
            //設置字體大小
            textPaint.setTextSize(mTextSize);
        }
    
        //5、測量
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            //獲取寬高
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
            //獲取模式
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            //判斷 如果是 wrap_content 模式,則 布局寬高 與 字體大小和字體長度有關
            if(widthMode == MeasureSpec.AT_MOST){
                //設置文字邊界
                Rect bounds = new Rect();
                //獲取文字邊界
                textPaint.getTextBounds(mText,0,mText.length(),bounds);
                //獲取邊界寬度  ===  獲取文字寬度
                width = bounds.width();
            }
            if (heightMode == MeasureSpec.AT_MOST){
                //設置文字邊界
                Rect bounds = new Rect();
                //獲取文字邊界
                textPaint.getTextBounds(mText,0,mText.length(),bounds);
                //獲取邊界高度  ===  獲取文字高度
                height = bounds.height();
            }
            //最后設置寬高
            setMeasuredDimension(width + getPaddingLeft() + getPaddingRight(),height + getPaddingBottom() + getPaddingTop());  、、、、修改的地方
        }
    
        //6、繪制
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            //drawText 中存在四個參數分別是: 要顯示的文本,基線x方向的起始點,基線y方向的起始點,畫筆
            Paint.FontMetricsInt fontMetricsInt = textPaint.getFontMetricsInt();
            //botto 為正值 top 為負值(看圖中畫的坐標系)
            int dy = (fontMetricsInt.bottom - fontMetricsInt.top)/2 - fontMetricsInt.bottom;
            int baseLine = getHeight()/2 + dy;
    
            float dx = getPaddingLeft();  、、、、修改的地方
            canvas.drawText(mText,dx,baseLine,textPaint);    、、、、修改的地方
        }
    }
    

    效果圖:

    Android自定義控件(五)---實戰篇(優化篇)_android

    完美收官,哈哈哈

    本文摘自 :https://blog.51cto.com/u

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码