View的測(cè)量

在現(xiàn)實(shí)生活中,我們想要畫一個(gè)圖形,要知道他的大小和繪制,才回去進(jìn)行繪畫。同樣,在Android系統(tǒng)中,繪制View前,也需要知道View的大小和位置,即告訴系統(tǒng)該畫一個(gè)多大的View(onMeasure),在哪個(gè)位置繪畫(onLayout),今天主要了解view的測(cè)量。

為了更好地理解View的測(cè)量,我分別打印出不同的寬、高和模式

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSize=MeasureSpec.getSize(widthMeasureSpec);
    int widthMode=MeasureSpec.getMode(widthMeasureSpec);
    int heightSize=MeasureSpec.getSize(heightMeasureSpec);
    int heightMode=MeasureSpec.getMode(heightMeasureSpec);

    Log.e("tag","widthMode:"+widthSize+"\nwidthSize:"+widthMode+"\nheightMode:"+heightMode+"\nheightSize:"+heightSize);
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.zx.test.CustomTextView
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:background="@android:color/darker_gray"
        app:text="測(cè)量"
        app:textColor="@color/colorPrimary"
        app:textSize="22dp"/>
</RelativeLayout>  

效果圖:

N~RBMEAHKL`RQCC70DMHX`5.png

Log打印輸出:

 E/tag: widthMode:300
       widthSize:1073741824
       heightMode:1073741824
       heightSize:300

布局文件:

<com.zx.test.CustomTextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    app:text="測(cè)量"
    app:textColor="@color/colorPrimary"
    app:textSize="22dp"/>

Log打印輸出:

E/tag: widthMode:480
      widthSize:1073741824
      heightMode:1073741824
      heightSize:732

布局文件:

 <com.zx.test.CustomTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    app:text="測(cè)量"
    app:textColor="@color/colorPrimary"
    app:textSize="22dp"/>

效果圖:

![NEJF$ZCJWO_$FN%K$AMR]{X.png](http://upload-images.jianshu.io/upload_images/2129339-0b76974301b1a6d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Log打印輸出:

 E/tag:widthMode:480
      widthSize:1073741824
      heightMode:-2147483648
      heightSize:732

總結(jié):
SpecMode有三類
EXACTLY 父容器已經(jīng)檢測(cè)出view的大小,View的大小就是SpecSize所指定的值
AT_MOST 父容器指定了可用的大小即SpecSize,View的帶下不能超過這個(gè)值
UNSPECIFIED 父容器不對(duì)View有任何的限制,要多大有多大,一般用于系統(tǒng)內(nèi)部測(cè)量

但是,為什么上面的自定義view的width和height設(shè)置為wrap_content仍然是填充整個(gè)屏幕,來看一下onMeausre源碼

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
        getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}

public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

switch (specMode) {
case MeasureSpec.UNSPECIFIED:
    result = size;
    break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
    result = specSize;
    break;
}
return result;
}

通過源碼發(fā)現(xiàn)onMeasure方法最終調(diào)用了setMeasuredDimension(int measuredWidth, int measuredHeight)方法,而傳入的參數(shù)已經(jīng)是測(cè)量過的默認(rèn)寬和高的值了;我們看看getDefaultSize 方法是怎么計(jì)算測(cè)量寬高的。根據(jù)父控件給予的約束,發(fā)現(xiàn)AT_MOST (相當(dāng)于wrap_content )和EXACTLY (相當(dāng)于match_parent )兩種情況返回的測(cè)量寬高都是specSize,而這個(gè)specSize正是我們上面說的父控件剩余的寬高,所以默認(rèn)onMeasure方法中wrap_content 和match_parent 的效果是一樣的。

下面重寫onMeasure方法,通過判斷測(cè)量的模式,給出不同的測(cè)量值。當(dāng)specMode為EXACTLY時(shí),直接使用指定的specSize即可,當(dāng)specMode為其他兩種模式時(shí),如果是AT_MOST(wrap_content)模式時(shí),要計(jì)算控件的尺寸,控件帶下=文本的尺寸+左邊距+右邊距

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSize=MeasureSpec.getSize(widthMeasureSpec);
    int widthMode=MeasureSpec.getMode(widthMeasureSpec);
    int heightSize=MeasureSpec.getSize(heightMeasureSpec);
    int heightMode=MeasureSpec.getMode(heightMeasureSpec);
    Log.e("tag","widthMode"+widthSize+"\nwidthSize"+widthMode+"\nheightMode"+heightMode+"\nheightSize"+heightSize);

    int width=0;
    int height=0;

    if(widthMode==MeasureSpec.AT_MOST){
        width=mRect.width()+getPaddingLeft()+getPaddingRight();
        Log.e("tag","text width"+width);
    }else{
        width=widthSize;
    }
    if(heightMode==MeasureSpec.AT_MOST){
        height=mRect.height()+getPaddingBottom()+getPaddingTop();
        Log.e("tag","text height"+height);
    }else{
        height=heightSize;
    }

    setMeasuredDimension(width,height);
}

Log打印輸出:

E/tag:  widthMode480
        widthSize-2147483648
        heightMode-2147483648
        heightSize732
        text width63
        text height31

效果如圖所示:

![H6{(YS]H)7NX$CZ0}FAR_IB.png](http://upload-images.jianshu.io/upload_images/2129339-924c3a4d29bffe5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

好了,暫時(shí)就寫到這吧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容