簡(jiǎn)介
自定義View必須要知道View的工作原理揣钦,我們都知道View的工作流程是measure->layout->draw,今天我們就逐個(gè)分析一下三個(gè)步驟伐庭。
首先要知道ViewRoot和Window和DecorView三者的關(guān)系璧函,ViewRoot對(duì)應(yīng)的是ViewRootImpl類笋籽,它是連接WindoManage和DecorView的紐帶柿菩,View的整個(gè)工作流程都是在ViewRootImpl的performTraversals中完成的振诬。
今天我們一起看看Measure的工作流程蹭睡。
理解MeasureSpec
在View的Measure過(guò)程MeasureSpec是一個(gè)很重要的內(nèi)容,它描述了要測(cè)量的尺寸和模式贷揽。View在Measure的過(guò)程是根據(jù)自身的LayoutParams和父容器的MeasureSpec轉(zhuǎn)換成對(duì)應(yīng)自身的MeasureSpec棠笑,然后再根據(jù)這個(gè)MeasureSpec測(cè)量出View的寬和高。
我們先看一下MeasureSpec的定義
public static class MeasureSpec {
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
/** @hide */
@IntDef({UNSPECIFIED, EXACTLY, AT_MOST})
@Retention(RetentionPolicy.SOURCE)
public @interface MeasureSpecMode {}
/**
* Measure specification mode: The parent has not imposed any constraint
* on the child. It can be whatever size it wants.
*/
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
/**
* Measure specification mode: The parent has determined an exact size
* for the child. The child is going to be given those bounds regardless
* of how big it wants to be.
*/
public static final int EXACTLY = 1 << MODE_SHIFT;
/**
* Measure specification mode: The child can be as large as it wants up
* to the specified size.
*/
public static final int AT_MOST = 2 << MODE_SHIFT;
public static int makeMeasureSpec( int size, @MeasureSpecMode int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
@UnsupportedAppUsage
public static int makeSafeMeasureSpec(int size, int mode) {
if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {
return 0;
}
return makeMeasureSpec(size, mode);
}
@MeasureSpecMode
public static int getMode(int measureSpec) {
//noinspection ResourceType
return (measureSpec & MODE_MASK);
}
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
}
MeasureSpec 代表一個(gè)32位的int值禽绪,高2位代表SpecMode蓖救,低30位代表SpecSize。
通過(guò)靜態(tài)方法makeMeasureSpec將SpecMode和SpecSize打包成一個(gè)int值印屁,避免過(guò)多的對(duì)象內(nèi)存分配循捺,另外還提供了解包方法。
SpecMode有三種模式:
**UNSPECIFIED **
父容器對(duì)view沒有任何約束雄人,view想要多大就多大从橘,這種模式一般系統(tǒng)使用或者是scrollView使用。
EXACTLY
父容器檢測(cè)出View需要的精確大小础钠,View的大小是SpecSize指定的值恰力。它對(duì)應(yīng)于LayoutParams中的match_parent和具體的數(shù)值。
AT_MOST
父容器指定一個(gè)最大值SpecSize旗吁,View的大小不能超過(guò)該值踩萎,它對(duì)應(yīng)的是LayoutParams的wrap_content
MeasureSpec在DecorView和View以及ViewGroup中的表現(xiàn)是不一樣的,我們分別分析一下三者的區(qū)別很钓。
DecorView的MeasureSpec
DecorView的MeasureSpec是由Window的尺寸和自身的LayoutParams共同決定的香府。在ViewRootImpl的measureHierarchy中展示了MeasureSpec的創(chuàng)建過(guò)程
childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
desiredWindowWidth和desiredWindowHeight表示屏幕的尺寸,接著看一下getRootMeasureSpec
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;
case ViewGroup.LayoutParams.WRAP_CONTENT:
// Window can resize. Set max size for root view.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
break;
default:
// Window wants to be an exact size. Force root view to be that size.
measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
break;
}
return measureSpec;
}
到此可以明確的看出DecorView的MeasureSpec產(chǎn)生遵守如下規(guī)則:
- MATCH_PARENT:精確模式码倦,大小是窗口大小
- WRAP_CONTENT: 最大模式企孩,最大大小是窗口大小
- 其他:精確模式,大小是LayoutParams指定的具體值袁稽。
View的MeasureSpec
先看一下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);
}
上述方法對(duì)子元素進(jìn)行measure勿璃,measure前的childXXXMeasureSpec是通過(guò)父容器的MeasureSpec,自身(ViewGroup)的被占用的空間(padding和margin),子View的LayoutParams綜合測(cè)量計(jì)算出來(lái)的蝗柔。我們看一下getChildMeasureSpec的實(shí)現(xiàn)
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) {
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
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) {
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
//noinspection ResourceType
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
方法內(nèi)部主要是根據(jù)父容器的MeasureSpec和自身的LayoutParams來(lái)確定子元素的MeasureSpec闻葵,其中size是根據(jù)父容器的尺寸減去已經(jīng)被占用的空間尺寸,及可用的尺寸。為了更清楚的展示getChildMeasureSpec的流程我們繪制一個(gè)表格:
如果只是一個(gè)View的measure會(huì)調(diào)用onMeasure方法完成測(cè)量
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
重點(diǎn)是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;
}
從這里可以看出View的layoutParams是wrap_content的時(shí)候癣丧,getDefaultSize得到的是specSize,根據(jù)上邊的圖表分析知道specSize是父容器的剩余空間栈妆,這種情況和match_content的大小一樣胁编,所以我們?cè)谧远xView的時(shí)候需要處理specMode是AT_MOST的情況。
ViewGroup的measure過(guò)程
對(duì)于ViewGroup除了完成自身的測(cè)量還要完成子元素的測(cè)量鳞尔,ViewGroup沒有onMeasure方法但是它有一個(gè)meausreChildren方法
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
final int size = mChildrenCount;
final View[] children = mChildren;
for (int i = 0; i < size; ++i) {
final View child = children[i];
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
只要子元素是可見的都進(jìn)行測(cè)量,我們看一下measureChild的內(nèi)容
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
measureChild的思想就是根據(jù)子元素的LayoutParams,然后再通過(guò)getChildMeasureSpec計(jì)算出子元素的MeasureSpec嬉橙,將這個(gè)MeasureSpec傳遞給子元素的measure方法。
至此View,ViewGroup寥假,DecorView的meausure都介紹完了市框。