欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android View 測(cè)量流程(Measure)全面解析

 更新時(shí)間:2017年02月16日 14:49:50   作者:程序員的自我反思  
這篇文章主要為大家全面解析了Android View 測(cè)量流程Measure,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

上一篇文章,筆者主要講述了DecorView以及ViewRootImpl相關(guān)的作用,這里回顧一下上一章所說的內(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說起

我們直接從這個(gè)方法說起,因?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();
  }
 } 
 ...
}

方法非常長,這里做了精簡(jiǎn),我們看到它里面主要執(zhí)行了三個(gè)方法,分別是performMeasure、performLayout、performDraw這三個(gè)方法,在這三個(gè)方法內(nèi)部又會(huì)分別調(diào)用measure、layout、draw這三個(gè)方法來進(jìn)行不同的流程。我們先來看看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.它的意思就是說,該類封裝了一個(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來確定View的測(cè)量寬高。
我們來看看這個(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è)量流程中,通過makeMeasureSpec來保存寬高信息,在其他流程通過getMode或getSize得到模式和寬高。那么問題來了,上面提到MeasureSpec是LayoutParams和父容器的模式所共同影響的,那么,對(duì)于DecorView來說,它已經(jīng)是頂層view了,沒有父容器,那么它的MeasureSpec怎么來的呢?

為了解決這個(gè)疑問,我們回到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ù)不同的模式來設(shè)置MeasureSpec,如果是LayoutParams.MATCH_PARENT模式,則是窗口的大小,WRAP_CONTENT模式則是大小不確定,但是不能超過窗口的大小等等。

那么到目前為止,就已經(jīng)獲得了一份DecorView的MeasureSpec,它代表著根View的規(guī)格、尺寸,在接下來的measure流程中,就是根據(jù)已獲得的根View的MeasureSpec來逐層測(cè)量各個(gè)子View。我們順著①號(hào)代碼往下走,來到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,也就是說,從頂級(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)行了一些判斷,這里不展開來看了,到最后會(huì)調(diào)用到super.onMeasure方法,即FrameLayout#onMeasure方法。

由于不同的ViewGroup有著不同的性質(zhì),那么它們的onMeasure必然是不同的,因此這里不可能把所有布局方式的onMeasure方法都分析一遍,因此這里選擇了FrameLayout的onMeasure方法來進(jìn)行分析,其它的布局方式讀者可以自行分析。那么我們繼續(xù)來看看這個(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í)行下列語句
 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來說,如果要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過程
  child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
 }
 }
}

由以上的FrameLayout的onMeasure過程可以看出,它還是做了相當(dāng)多的工作的,這里簡(jiǎn)單總結(jié)一下:首先,F(xiàn)rameLayout根據(jù)它的MeasureSpec來對(duì)每一個(gè)子View進(jìn)行測(cè)量,即調(diào)用measureChildWithMargin方法,這個(gè)方法下面會(huì)詳細(xì)說明;對(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來說,是其最大子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來確定自身的測(cè)量寬高,然后最后根據(jù)所有子View的測(cè)量寬高信息再確定父容器的測(cè)量寬高。

那么接下來,我們繼續(xù)分析對(duì)于一個(gè)子View來說,是怎么進(jìn)行測(cè)量的。

View的測(cè)量流程

還記得我們上面在FrameLayout測(cè)量內(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)去來獲取子View的MeasureSpec,這也印證了“子View的MeasureSpec由父容器的MeasureSpec和自身的LayoutParams共同決定”這個(gè)結(jié)論。那么,我們一起來看看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來決定子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,這在上面說過了,這里不再贅述,然后在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方法,上面說過該方法的作用是設(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ù)不同模式來設(shè)置不同的測(cè)量寬高,我們直接看AT_MOST和EXACTLY模式,它直接把specSize返回了,即View在這兩種模式下的測(cè)量寬高直接取決于specSize規(guī)格。也即是說,對(duì)于一個(gè)直接繼承自View的自定義View來說,它的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從哪里來的呢?再往上看一層,它來自于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來說,它的原始寬度就是圖片的尺寸。到此,子View的測(cè)量流程也完成了。

總結(jié)

這里簡(jiǎn)單概括一下整個(gè)流程:測(cè)量始于DecorView,通過不斷的遍歷子View的measure方法,根據(jù)ViewGroup的MeasureSpec及子View的LayoutParams來決定子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) 完全解析

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android開發(fā)實(shí)現(xiàn)標(biāo)題隨scrollview滑動(dòng)變色的方法詳解

    Android開發(fā)實(shí)現(xiàn)標(biāo)題隨scrollview滑動(dòng)變色的方法詳解

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)標(biāo)題隨scrollview滑動(dòng)變色的方法,涉及Android針對(duì)滑動(dòng)事件的響應(yīng)、界面布局、屬性動(dòng)態(tài)變換等相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android Studio使用小技巧:提取方法代碼片段

    Android Studio使用小技巧:提取方法代碼片段

    這篇文章主要介紹了Android Studio使用小技巧:提取方法代碼片段,本文分享了一個(gè)快速復(fù)制粘貼方法代碼片段的小技巧,并用GIF圖演示,需要的朋友可以參考下
    2015-05-05
  • Android之rk3588?開發(fā)環(huán)境準(zhǔn)備及問題解決方法

    Android之rk3588?開發(fā)環(huán)境準(zhǔn)備及問題解決方法

    這篇文章主要介紹了Android中的rk3588?開發(fā)環(huán)境準(zhǔn)備,本文給大家分享遇到的問題及解決方法,本文給大家講解的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-11-11
  • Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法

    Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法

    這篇文章主要介紹了Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-06-06
  • Android使用token維持登陸狀態(tài)的方法

    Android使用token維持登陸狀態(tài)的方法

    這篇文章主要介紹了Android使用token維持登陸狀態(tài)的方法,包括token的概念及作用介紹,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2017-01-01
  • Android?Camera開發(fā)實(shí)現(xiàn)可復(fù)用的相機(jī)組件

    Android?Camera開發(fā)實(shí)現(xiàn)可復(fù)用的相機(jī)組件

    這篇文章主要為大家詳細(xì)介紹了Android?Camera開發(fā)實(shí)現(xiàn)可復(fù)用的相機(jī)組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android XML數(shù)據(jù)的三種解析方式

    Android XML數(shù)據(jù)的三種解析方式

    這篇文章主要為大家詳細(xì)介紹了Android XML數(shù)據(jù)的三種解析方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android PopUpWindow使用詳解

    Android PopUpWindow使用詳解

    PopupWindow與AlertDialog最關(guān)鍵的區(qū)別是AlertDialog不能指定顯示位置,只能默認(rèn)顯示在屏幕最中間(當(dāng)然也可以通過設(shè)置WindowManager參數(shù)來改變位置)。而PopupWindow是可以指定顯示位置的,隨便哪個(gè)位置都可以,更加靈活
    2021-10-10
  • 如何安裝adb工具及常用的adb命令

    如何安裝adb工具及常用的adb命令

    ADB全稱為Android Debug Bridge,起到調(diào)試橋的作用,是一個(gè)客戶端-服務(wù)器端程序,ADB 也是 Android SDK 中的一個(gè)工具,可以直接操作管理 Android 模擬器或者真實(shí)的 Android 設(shè)備,本文介紹如何安裝adb工具及常用的adb命令,感興趣的朋友一起看看吧
    2024-01-01
  • Android實(shí)現(xiàn)可拖拽列表和多選功能

    Android實(shí)現(xiàn)可拖拽列表和多選功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可拖拽列表和多選功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06

最新評(píng)論