前言
公司主營(yíng)業(yè)務(wù):網(wǎng)站制作、成都做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出蘆淞免費(fèi)做網(wǎng)站回饋大家。
上一篇文章,筆者主要講述了DecorView以及ViewRootImpl相關(guān)的作用,這里回顧一下上一章所說(shuō)的內(nèi)容:DecorView是視圖的頂級(jí)View,我們添加的布局文件是它的一個(gè)子布局,而ViewRootImpl則負(fù)責(zé)渲染視圖,它調(diào)用了一個(gè)performTraveals方法使得ViewTree開始三大工作流程,然后使得View展現(xiàn)在我們面前。本篇文章主要內(nèi)容是:詳細(xì)講述View的測(cè)量(Measure)流程,主要以源碼的形式呈現(xiàn),源碼均取自Android API 21.
從ViewRootImpl#PerformTraveals說(shuō)起
我們直接從這個(gè)方法說(shuō)起,因?yàn)樗钦麄€(gè)工作流程的核心,我們看看它的源碼:
private void performTraversals() { ... if (!mStopped) { int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width); // 1 int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height); performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); } } if (didLayout) { performLayout(lp, desiredWindowWidth, desiredWindowHeight); ... } if (!cancelDraw && !newSurface) { if (!skipDraw || mReportNextDraw) { if (mPendingTransitions != null && mPendingTransitions.size() > 0) { for (int i = 0; i < mPendingTransitions.size(); ++i) { mPendingTransitions.get(i).startChangingAnimations(); } mPendingTransitions.clear(); } performDraw(); } } ... }
方法非常長(zhǎng),這里做了精簡(jiǎn),我們看到它里面主要執(zhí)行了三個(gè)方法,分別是performMeasure、performLayout、performDraw這三個(gè)方法,在這三個(gè)方法內(nèi)部又會(huì)分別調(diào)用measure、layout、draw這三個(gè)方法來(lái)進(jìn)行不同的流程。我們先來(lái)看看performMeasure(childWidthMeasureSpec, childHeightMeasureSpec)這個(gè)方法,它傳入兩個(gè)參數(shù),分別是childWidthMeasureSpec和childHeightMeasure,那么這兩個(gè)參數(shù)代表什么意思呢?要想了解這兩個(gè)參數(shù)的意思,我們就要先了解MeasureSpec。
理解MeasureSpec
MeasureSpec是View類的一個(gè)內(nèi)部類,我們先看看官方文檔對(duì)MeasureSpec類的描述:A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec represents a requirement for either the width or the height. A MeasureSpec is comprised of a size and a mode.它的意思就是說(shuō),該類封裝了一個(gè)View的規(guī)格尺寸,包括View的寬和高的信息,但是要注意,MeasureSpec并不是指View的測(cè)量寬高,這是不同的,是根據(jù)MeasueSpec而測(cè)出測(cè)量寬高。
MeasureSpec的作用在于:在Measure流程中,系統(tǒng)會(huì)將View的LayoutParams根據(jù)父容器所施加的規(guī)則轉(zhuǎn)換成對(duì)應(yīng)的MeasureSpec,然后在onMeasure方法中根據(jù)這個(gè)MeasureSpec來(lái)確定View的測(cè)量寬高。
我們來(lái)看看這個(gè)類的源碼:
public static class MeasureSpec { private static final int MODE_SHIFT = 30; private static final int MODE_MASK = 0x3 << MODE_SHIFT; /** * UNSPECIFIED 模式: * 父View不對(duì)子View有任何限制,子View需要多大就多大 */ public static final int UNSPECIFIED = 0 << MODE_SHIFT; /** * EXACTYLY 模式: * 父View已經(jīng)測(cè)量出子Viwe所需要的精確大小,這時(shí)候View的最終大小 * 就是SpecSize所指定的值。對(duì)應(yīng)于match_parent和精確數(shù)值這兩種模式 */ public static final int EXACTLY = 1 << MODE_SHIFT; /** * AT_MOST 模式: * 子View的最終大小是父View指定的SpecSize值,并且子View的大小不能大于這個(gè)值, * 即對(duì)應(yīng)wrap_content這種模式 */ public static final int AT_MOST = 2 << MODE_SHIFT; //將size和mode打包成一個(gè)32位的int型數(shù)值 //高2位表示SpecMode,測(cè)量模式,低30位表示SpecSize,某種測(cè)量模式下的規(guī)格大小 public static int makeMeasureSpec(int size, int mode) { if (sUseBrokenMakeMeasureSpec) { return size + mode; } else { return (size & ~MODE_MASK) | (mode & MODE_MASK); } } //將32位的MeasureSpec解包,返回SpecMode,測(cè)量模式 public static int getMode(int measureSpec) { return (measureSpec & MODE_MASK); } //將32位的MeasureSpec解包,返回SpecSize,某種測(cè)量模式下的規(guī)格大小 public static int getSize(int measureSpec) { return (measureSpec & ~MODE_MASK); } //... }
可以看出,該類的思路是相當(dāng)清晰的,對(duì)于每一個(gè)View,包括DecorView,都持有一個(gè)MeasureSpec,而該MeasureSpec則保存了該View的尺寸規(guī)格。在View的測(cè)量流程中,通過(guò)makeMeasureSpec來(lái)保存寬高信息,在其他流程通過(guò)getMode或getSize得到模式和寬高。那么問(wèn)題來(lái)了,上面提到MeasureSpec是LayoutParams和父容器的模式所共同影響的,那么,對(duì)于DecorView來(lái)說(shuō),它已經(jīng)是頂層view了,沒有父容器,那么它的MeasureSpec怎么來(lái)的呢?
為了解決這個(gè)疑問(wèn),我們回到ViewRootImpl#PerformTraveals方法,看①號(hào)代碼處,調(diào)用了getRootMeasureSpec(desiredWindowWidth,lp.width)方法,其中desiredWindowWidth就是屏幕的尺寸,并把返回結(jié)果賦值給childWidthMeasureSpec成員變量(childHeightMeasureSpec同理),因此childWidthMeasureSpec(childHeightMeasureSpec)應(yīng)該保存了DecorView的MeasureSpec,那么我們看一下ViewRootImpl#getRootMeasureSpec方法的實(shí)現(xiàn):
/** * @param windowSize * The available width or height of the window * * @param rootDimension * The layout params for one dimension (width or height) of the * window. * * @return The measure spec to use to measure the root view. */ private static int getRootMeasureSpec(int windowSize, int rootDimension) { int measureSpec; switch (rootDimension) { case ViewGroup.LayoutParams.MATCH_PARENT: // Window can't resize. Force root view to be windowSize. measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY); break; //省略... } return measureSpec; }
思路也很清晰,根據(jù)不同的模式來(lái)設(shè)置MeasureSpec,如果是LayoutParams.MATCH_PARENT模式,則是窗口的大小,WRAP_CONTENT模式則是大小不確定,但是不能超過(guò)窗口的大小等等。
那么到目前為止,就已經(jīng)獲得了一份DecorView的MeasureSpec,它代表著根View的規(guī)格、尺寸,在接下來(lái)的measure流程中,就是根據(jù)已獲得的根View的MeasureSpec來(lái)逐層測(cè)量各個(gè)子View。我們順著①號(hào)代碼往下走,來(lái)到performMeasure方法,看看它做了什么工作,ViewRootImpl#performMeasure:
private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure"); try { mView.measure(childWidthMeasureSpec, childHeightMeasureSpec); } finally { Trace.traceEnd(Trace.TRACE_TAG_VIEW); } }
方法很簡(jiǎn)單,直接調(diào)用了mView.measure,這里的mView就是DecorView,也就是說(shuō),從頂級(jí)View開始了測(cè)量流程,那么我們直接進(jìn)入measure流程。
measure 測(cè)量流程
ViewGroup的測(cè)量流程
由于DecorView繼承自FrameLayout,是PhoneWindow的一個(gè)內(nèi)部類,而FrameLayout沒有measure方法,因此調(diào)用的是父類View的measure方法,我們直接看它的源碼,View#measure:
public final void measure(int widthMeasureSpec, int heightMeasureSpec) { boolean optical = isLayoutModeOptical(this); if (optical != isLayoutModeOptical(mParent)) { ... if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT || widthMeasureSpec != mOldWidthMeasureSpec || heightMeasureSpec != mOldHeightMeasureSpec) { ... if (cacheIndex < 0 || sIgnoreMeasureCache) { // measure ourselves, this should set the measured dimension flag back onMeasure(widthMeasureSpec, heightMeasureSpec); mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT; } ... }
可以看到,它在內(nèi)部調(diào)用了onMeasure方法,由于DecorView是FrameLayout子類,因此它實(shí)際上調(diào)用的是DecorView#onMeasure方法。在該方法內(nèi)部,主要是進(jìn)行了一些判斷,這里不展開來(lái)看了,到最后會(huì)調(diào)用到super.onMeasure方法,即FrameLayout#onMeasure方法。
由于不同的ViewGroup有著不同的性質(zhì),那么它們的onMeasure必然是不同的,因此這里不可能把所有布局方式的onMeasure方法都分析一遍,因此這里選擇了FrameLayout的onMeasure方法來(lái)進(jìn)行分析,其它的布局方式讀者可以自行分析。那么我們繼續(xù)來(lái)看看這個(gè)方法:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //獲取當(dāng)前布局內(nèi)的子View數(shù)量 int count = getChildCount(); //判斷當(dāng)前布局的寬高是否是match_parent模式或者指定一個(gè)精確的大小,如果是則置measureMatchParent為false. final boolean measureMatchParentChildren = MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY; mMatchParentChildren.clear(); int maxHeight = 0; int maxWidth = 0; int childState = 0; //遍歷所有類型不為GONE的子View for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (mMeasureAllChildren || child.getVisibility() != GONE) { //對(duì)每一個(gè)子View進(jìn)行測(cè)量 measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); //尋找子View中寬高的最大者,因?yàn)槿绻鸉rameLayout是wrap_content屬性 //那么它的大小取決于子View中的最大者 maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); childState = combineMeasuredStates(childState, child.getMeasuredState()); //如果FrameLayout是wrap_content模式,那么往mMatchParentChildren中添加 //寬或者高為match_parent的子View,因?yàn)樵撟覸iew的最終測(cè)量大小會(huì)受到FrameLayout的最終測(cè)量大小影響 if (measureMatchParentChildren) { if (lp.width == LayoutParams.MATCH_PARENT || lp.height == LayoutParams.MATCH_PARENT) { mMatchParentChildren.add(child); } } } } // Account for padding too maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground(); maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground(); // Check against our minimum height and width maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); // Check against our foreground's minimum height and width final Drawable drawable = getForeground(); if (drawable != null) { maxHeight = Math.max(maxHeight, drawable.getMinimumHeight()); maxWidth = Math.max(maxWidth, drawable.getMinimumWidth()); } //保存測(cè)量結(jié)果 setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT)); //子View中設(shè)置為match_parent的個(gè)數(shù) count = mMatchParentChildren.size(); //只有FrameLayout的模式為wrap_content的時(shí)候才會(huì)執(zhí)行下列語(yǔ)句 if (count > 1) { for (int i = 0; i < count; i++) { final View child = mMatchParentChildren.get(i); final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); //對(duì)FrameLayout的寬度規(guī)格設(shè)置,因?yàn)檫@會(huì)影響子View的測(cè)量 final int childWidthMeasureSpec; /** * 如果子View的寬度是match_parent屬性,那么對(duì)當(dāng)前FrameLayout的MeasureSpec修改: * 把widthMeasureSpec的寬度規(guī)格修改為:總寬度 - padding - margin,這樣做的意思是: * 對(duì)于子Viw來(lái)說(shuō),如果要match_parent,那么它可以覆蓋的范圍是FrameLayout的測(cè)量寬度 * 減去padding和margin后剩下的空間。 * * 以下兩點(diǎn)的結(jié)論,可以查看getChildMeasureSpec()方法: * * 如果子View的寬度是一個(gè)確定的值,比如50dp,那么FrameLayout的widthMeasureSpec的寬度規(guī)格修改為: * SpecSize為子View的寬度,即50dp,SpecMode為EXACTLY模式 * * 如果子View的寬度是wrap_content屬性,那么FrameLayout的widthMeasureSpec的寬度規(guī)格修改為: * SpecSize為子View的寬度減去padding減去margin,SpecMode為AT_MOST模式 */ if (lp.width == LayoutParams.MATCH_PARENT) { final int width = Math.max(0, getMeasuredWidth() - getPaddingLeftWithForeground() - getPaddingRightWithForeground() - lp.leftMargin - lp.rightMargin); childWidthMeasureSpec = MeasureSpec.makeMeasureSpec( width, MeasureSpec.EXACTLY); } else { childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, getPaddingLeftWithForeground() + getPaddingRightWithForeground() + lp.leftMargin + lp.rightMargin, lp.width); } //同理對(duì)高度進(jìn)行相同的處理,這里省略... //對(duì)于這部分的子View需要重新進(jìn)行measure過(guò)程 child.measure(childWidthMeasureSpec, childHeightMeasureSpec); } } }
由以上的FrameLayout的onMeasure過(guò)程可以看出,它還是做了相當(dāng)多的工作的,這里簡(jiǎn)單總結(jié)一下:首先,F(xiàn)rameLayout根據(jù)它的MeasureSpec來(lái)對(duì)每一個(gè)子View進(jìn)行測(cè)量,即調(diào)用measureChildWithMargin方法,這個(gè)方法下面會(huì)詳細(xì)說(shuō)明;對(duì)于每一個(gè)測(cè)量完成的子View,會(huì)尋找其中最大的寬高,那么FrameLayout的測(cè)量寬高會(huì)受到這個(gè)子View的最大寬高的影響(wrap_content模式),接著調(diào)用setMeasureDimension方法,把FrameLayout的測(cè)量寬高保存。最后則是特殊情況的處理,即當(dāng)FrameLayout為wrap_content屬性時(shí),如果其子View是match_parent屬性的話,則要重新設(shè)置FrameLayout的測(cè)量規(guī)格,然后重新對(duì)該部分View測(cè)量。
在上面提到setMeasureDimension方法,該方法用于保存測(cè)量結(jié)果,在上面的源碼里面,該方法的參數(shù)接收的是resolveSizeAndState方法的返回值,那么我們直接看View#resolveSizeAndState方法:
public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) { final int specMode = MeasureSpec.getMode(measureSpec); final int specSize = MeasureSpec.getSize(measureSpec); final int result; switch (specMode) { case MeasureSpec.AT_MOST: if (specSize < size) { result = specSize | MEASURED_STATE_TOO_SMALL; } else { result = size; } break; case MeasureSpec.EXACTLY: result = specSize; break; case MeasureSpec.UNSPECIFIED: default: result = size; } return result | (childMeasuredState & MEASURED_STATE_MASK); }
可以看到該方法的思路是相當(dāng)清晰的,當(dāng)specMode是EXACTLY時(shí),那么直接返回MeasureSpec里面的寬高規(guī)格,作為最終的測(cè)量寬高;當(dāng)specMode時(shí)AT_MOST時(shí),那么取MeasureSpec的寬高規(guī)格和size的最小值。(注:這里的size,對(duì)于FrameLayout來(lái)說(shuō),是其最大子View的測(cè)量寬高)。
小結(jié):那么到目前為止,以DecorView為切入點(diǎn),把ViewGroup的測(cè)量流程詳細(xì)地分析了一遍,在ViewRootImpl#performTraversals中獲得DecorView的尺寸,然后在performMeasure方法中開始測(cè)量流程,對(duì)于不同的layout布局有著不同的實(shí)現(xiàn)方式,但大體上是在onMeasure方法中,對(duì)每一個(gè)子View進(jìn)行遍歷,根據(jù)ViewGroup的MeasureSpec及子View的layoutParams來(lái)確定自身的測(cè)量寬高,然后最后根據(jù)所有子View的測(cè)量寬高信息再確定父容器的測(cè)量寬高。
那么接下來(lái),我們繼續(xù)分析對(duì)于一個(gè)子View來(lái)說(shuō),是怎么進(jìn)行測(cè)量的。
View的測(cè)量流程
還記得我們上面在FrameLayout測(cè)量?jī)?nèi)提到的measureChildWithMargin方法,它接收的主要參數(shù)是子View以及父容器的MeasureSpec,所以它的作用就是對(duì)子View進(jìn)行測(cè)量,那么我們直接看這個(gè)方法,ViewGroup#measureChildWithMargins:
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); // 1 }
由源碼可知,里面調(diào)用了getChildMeasureSpec方法,把父容器的MeasureSpec以及自身的layoutParams屬性傳遞進(jìn)去來(lái)獲取子View的MeasureSpec,這也印證了“子View的MeasureSpec由父容器的MeasureSpec和自身的LayoutParams共同決定”這個(gè)結(jié)論。那么,我們一起來(lái)看看ViewGroup#getChildMeasureSpec方法:
public static int getChildMeasureSpec(int spec, int padding, int childDimension) { int specMode = MeasureSpec.getMode(spec); int specSize = MeasureSpec.getSize(spec); //size表示子View可用空間:父容器尺寸減去padding 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: //省略..具體可自行參考源碼 break; // Parent asked to see how big we want to be case MeasureSpec.UNSPECIFIED: //省略...具體可自行參考源碼 break; } return MeasureSpec.makeMeasureSpec(resultSize, resultMode); }
上面方法也非常容易理解,大概是根據(jù)不同的父容器的模式及子View的layoutParams來(lái)決定子View的規(guī)格尺寸模式等。那么,這里根據(jù)上面的邏輯,列出不同的父容器的MeasureSpec和子View的LayoutParams的組合情況下所出現(xiàn)的不同的子View的MeasureSpec:
(注:該表格呈現(xiàn)形式參考自《Android 開發(fā)藝術(shù)探索》 任玉剛 著)
當(dāng)子View的MeasureSpec獲得后,我們返回measureChildWithMargins方法,接著就會(huì)執(zhí)行①號(hào)代碼:child.measure方法,意味著,繪制流程已經(jīng)從ViewGroup轉(zhuǎn)移到子View中了,可以看到傳遞的參數(shù)正是我們剛才獲取的子View的MeasureSpec,接著會(huì)調(diào)用View#measure,這在上面說(shuō)過(guò)了,這里不再贅述,然后在measure方法,會(huì)調(diào)用onMeasure方法,當(dāng)然了,對(duì)于不同類型的View,其onMeasure方法是不同的,但是對(duì)于不同的View,即使是自定義View,我們?cè)谥貙懙膐nMeasure方法內(nèi),也一定會(huì)調(diào)用到View#onMeasure方法的,因此我們看看它的源碼:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); }
顯然,這里調(diào)用了setMeasureDimension方法,上面說(shuō)過(guò)該方法的作用是設(shè)置測(cè)量寬高,而測(cè)量寬高則是從getDefaultSize中獲取,我們繼續(xù)看看這個(gè)方法View#getDefaultSize:
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; }
好吧,又是類似的代碼,根據(jù)不同模式來(lái)設(shè)置不同的測(cè)量寬高,我們直接看AT_MOST和EXACTLY模式,它直接把specSize返回了,即View在這兩種模式下的測(cè)量寬高直接取決于specSize規(guī)格。也即是說(shuō),對(duì)于一個(gè)直接繼承自View的自定義View來(lái)說(shuō),它的wrap_content和match_parent屬性的效果是一樣的,因此如果要實(shí)現(xiàn)自定義View的wrap_content,則要重寫onMeasure方法,對(duì)wrap_content屬性進(jìn)行處理。
接著,我們看UNSPECIFIED模式,這個(gè)模式可能比較少見,一般用于系統(tǒng)內(nèi)部測(cè)量,它直接返回的是size,而不是specSize,那么size從哪里來(lái)的呢?再往上看一層,它來(lái)自于getSuggestedMinimumWidth()或getSuggestedMinimumHeight(),我們選取其中一個(gè)方法,看看源碼,View#getSuggestedMinimumWidth:
protected int getSuggestedMinimumWidth() { return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth()); }
從以上邏輯可以看出,當(dāng)View沒有設(shè)置背景的時(shí)候,返回mMinWidth,該值對(duì)應(yīng)于android:minWidth屬性;如果設(shè)置了背景,那么返回mMinWidth和mBackground.getMinimumWidth中的最大值。那么mBackground.getMinimumWidth又是什么呢?其實(shí)它代表了背景的原始寬度,比如對(duì)于一個(gè)Bitmap來(lái)說(shuō),它的原始寬度就是圖片的尺寸。到此,子View的測(cè)量流程也完成了。
總結(jié)
這里簡(jiǎn)單概括一下整個(gè)流程:測(cè)量始于DecorView,通過(guò)不斷的遍歷子View的measure方法,根據(jù)ViewGroup的MeasureSpec及子View的LayoutParams來(lái)決定子View的MeasureSpec,進(jìn)一步獲取子View的測(cè)量寬高,然后逐層返回,不斷保存ViewGroup的測(cè)量寬高。
從文章開始到現(xiàn)在,View的測(cè)量流程已經(jīng)全部分析完畢,View的measure流程是三大流程中最復(fù)雜的一個(gè)流程,其中的MeasureSpec貫穿了整個(gè)測(cè)量流程,占有非常重要的地位,希望讀者仔細(xì)體會(huì)這個(gè)流程,最后希望這篇文章能幫助你對(duì)View的測(cè)量流程有進(jìn)一步的了解,謝謝閱讀。
更多閱讀
Android View 布局流程(Layout)完全解析
Android View 繪制流程(Draw) 完全解析
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享名稱:AndroidView測(cè)量流程(Measure)全面解析
新聞來(lái)源:http://muchs.cn/article24/jehdje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、云服務(wù)器、全網(wǎng)營(yíng)銷推廣、外貿(mào)建站、網(wǎng)站收錄、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)