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

    Android自定義控件(三)---實戰篇(詳解onMeasure)
    2021-09-16 11:40:25

    接著Android自定義控件(二)---實戰篇的講解,這篇我們來詳細講一下測量(onMeasure)和繪制(onDraw)這兩個方法

    首先,我們來看測量(onMeasure)方法,在這個方法里,我們主要是設置控件的寬高,widthMeasureSpec、heightMeasureSpec這兩個參數已經在基礎篇講解了,不過多贅述Android自定義控件(一)---基礎篇

    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,height);
        }
    
        //6、繪制
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
        }
    }
    

    現在可以去運行一把了,可以看到一個方塊,之所以沒有漢字,是因為我們沒有編寫繪制(onDraw)方法,效果圖如下:

    Android自定義控件(三)---實戰篇(詳解onMeasure)_寬高

    在這里,我還想多說一句,那就是內邊距(padding)和外邊距(margin)對布局尺寸的影響

    padding會使布局寬高增加,并且會繼承布局的背景色,如果布局使用的是? match_parent 還會導致布局超出屏幕

    margin會使布局寬高減少,并且不會繼承布局的背景色

    這點不做要求,你們碰見問題之后知道怎么解決接好了

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

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