當(dāng)前位置:首頁 > IT技術(shù) > 移動(dòng)平臺(tái) > 正文

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

接著Android自定義控件(二)---實(shí)戰(zhàn)篇的講解,這篇我們來詳細(xì)講一下測(cè)量(onMeasure)和繪制(onDraw)這兩個(gè)方法

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

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、設(shè)置自定義屬性變量
    private int mTextSize = 16;
    private int mTextColor = Color.RED;
    private String mText;

    //設(shè)置文字畫筆
    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、獲取裝有自定義屬性值的數(shù)值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.mTextView);
        //3、精確獲取自定義屬性值
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.mTextView_mTextSize,mTextSize);//源碼用的這個(gè)方法,參照 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);
        //設(shè)置顏色
        textPaint.setColor(mTextColor);
        //設(shè)置字體大小
        textPaint.setTextSize(mTextSize);
    }

    //5、測(cè)量
    @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 模式,則 布局寬高 與 字體大小和字體長(zhǎng)度有關(guān)
        if(widthMode == MeasureSpec.AT_MOST){
            //設(shè)置文字邊界
            Rect bounds = new Rect();
            //獲取文字邊界
            textPaint.getTextBounds(mText,0,mText.length(),bounds);
            //獲取邊界寬度  ===  獲取文字寬度
            width = bounds.width();
        }
        if (heightMode == MeasureSpec.AT_MOST){
            //設(shè)置文字邊界
            Rect bounds = new Rect();
            //獲取文字邊界
            textPaint.getTextBounds(mText,0,mText.length(),bounds);
            //獲取邊界高度  ===  獲取文字高度
            height = bounds.height();
        }
        //最后設(shè)置寬高
        setMeasuredDimension(width,height);
    }

    //6、繪制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

現(xiàn)在可以去運(yùn)行一把了,可以看到一個(gè)方塊,之所以沒有漢字,是因?yàn)槲覀儧]有編寫繪制(onDraw)方法,效果圖如下:

Android自定義控件(三)---實(shí)戰(zhàn)篇(詳解onMeasure)_寬高

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

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

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

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

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

開通會(huì)員,享受整站包年服務(wù)立即開通 >