Android 理解MeasureSpec

Android View 的測量過程中使用到了MeasureSpec,正如其字面意思所表達的那個-“測量規(guī)格”。View根據(jù)該規(guī)格從而決定自己的大小。MeasureSpec由倆部分組成,一部分是SpecMode(測量模式),另一部分是SpecSize(規(guī)格大?。?。View的MeasureSpec由父容器和自己布局參數(shù)共同決定,這個后面會具體解釋。

一.MeasureSpec組成

MeasureSpec是由一個32位 int 值來表示的。其中該 int 值對應的二進制的高2位代表SpecMode,低30位代表SpecSize。這種方法設計很巧妙,減少了空間占用。

private static final int MODE_SHIFT = 30;
private static final int MODE_MASK  = 0x3 << MODE_SHIFT;
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
public static final int EXACTLY     = 1 << MODE_SHIFT;
public static final int AT_MOST     = 2 << MODE_SHIFT;

public static int makeMeasureSpec(int size, int mode) {
        if (sUseBrokenMakeMeasureSpec) {
            return size + mode;
        } else {
            return (size & ~MODE_MASK) | (mode & MODE_MASK);
        }
 }

makeMeasureSpec是用來構建MeasureSpec,可以看出MeasueSpec的確是由Size和Mode 組成起來構建的

二. SpecMode 測量模式

在說具體模式前,有必要大概講下MeasureSpec是如何來用到View的測量中的。

public final void measure(int widthMeasureSpec, int heightMeasureSpec)

上面是View的measure測量方法。View的測量是一層一層去進行繪制的。首先會繪制ViewGroup,然后由ViewGroup去繪制子View。從View的measure方法,可以看到這里已經(jīng)傳入了widthMeasureSpec和heightMeasureSpec。也就是說父ViewGroup去測量子View的時候,已經(jīng)知道了子View的測量規(guī)格。也就是子View的測量模式和測量大小。

  1. UNSPECIFIED:父容器不對子View有限制,子View要多大給多大,這種一般我們不會接觸到
  2. EXACTLY: 表示精確模式,View的大小已經(jīng)確認,為SpecSize所指定的值。
  3. AT_MOST:表示子View的大小不確認,指定了該子View最大可以為多少。子View可以在該范圍內(nèi)設定自己的大小。

可以這樣去理解:MeasureSpec是系統(tǒng)提供給了我們一種能力(范圍)。View可以在允許的范圍內(nèi),繪制自己的東西。

三. MeasureSpec的創(chuàng)建發(fā)生在何時
  1. View的測量是由ViewGroup去進行的。以FrameLayout為例。
  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        ...
        int maxHeight = 0;
        int maxWidth = 0;
        int childState = 0;
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (mMeasureAllChildren || child.getVisibility() != GONE) {
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                ...
            }
        }
        ...
    }

2 . FrameLayout的onMeasure方法會遍歷自己所有的子View,然后調(diào)用measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0)去測量子View。

protected void measureChildWithMargins(View child,
            int parentWidthMeasureSpec, int widthUsed,
            int parentHeightMeasureSpec, int heightUsed) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
                        + widthUsed, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
                        + heightUsed, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

可以發(fā)現(xiàn)measureChildWithMargins方法的getChildMeasureSpec方法創(chuàng)建了子View的MeasureSpec方法。然后調(diào)用child.measure執(zhí)行子View的測量

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);

        int size = Math.max(0, specSize - padding);

        int resultSize = 0;
        int resultMode = 0;

        switch (specMode) {
        // Parent has imposed an exact size on us
        case MeasureSpec.EXACTLY:
            if (childDimension >= 0) {
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size. So be it.
                resultSize = size;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent has imposed a maximum size on us
        case MeasureSpec.AT_MOST:
            if (childDimension >= 0) {
                // Child wants a specific size... so be it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size, but our size is not fixed.
                // Constrain child to not be bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent asked to see how big we want to be
        case MeasureSpec.UNSPECIFIED:
            if (childDimension >= 0) {
                // Child wants a specific size... let him have it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size... find out how big it should
                // be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size.... find out how
                // big it should be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
        //noinspection ResourceType
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }

3 . getChildMeasureSpec方法參數(shù)的spec是父容器的測量規(guī)格。childDimension則是通過getLayoutParams獲取的。所以說子View的MeasureSpec是由 父容器和子View的布局參數(shù)共同決定的。

  1. 下面具體來分析上面方法的情況

由于上面方法情況很多,就只分析父ViewGroup為Exactly模式。

父容器為Exactly模式,那么就可以知道父容器的大小,其值為SpecSize。在來考慮子View的布局參數(shù)。

a. 如果子MATCH_PARENT時,那么子View的大小也就確認了,其值和父View一樣大,為SpecSize。 子View的大小是確認的,故子View屬于Exactly模式,最后通過MeasureSpec.makeMeasureSpec方法創(chuàng)建即可.

b.View的布局參數(shù)如果是具體的值,也是同理的。

c. View的布局參數(shù)是WRAP_CONTENT的情況,那么子View在大也不能大過父容器SpecSize的大小。此時由于View的布局是根據(jù)自己的內(nèi)容的,所以大小是不確認的,所以測量模式應該為AT_MOST模式。

下面包含了所有情況


MeasureSpec的創(chuàng)建
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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