前言
之前一直對View繪制過程隻知道個大概,最近看了扔物線的視訊,突然來了興趣,于是找了一個比較簡單的ViewGroup來分析下源碼,鞏固下了解。
onMeasure
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();//子View的數量
//①判斷是否需要再次測量子View
final boolean measureMatchParentChildren =
MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
mMatchParentChildren.clear();
int maxHeight = ;//子View占的最大高度
int maxWidth = ;//子View占的最大寬度
int childState = ;
for (int i = ; i < count; i++) {
final View child = getChildAt(i);
//如果子View的Visibility屬性不為GONE,或者mMeasureAllChildren為True則測量子View的寬高
if (mMeasureAllChildren || child.getVisibility() != GONE) {
//②開始測量子View的寬高與MeasureMode
measureChildWithMargins(child, widthMeasureSpec, , heightMeasureSpec, );
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
//帶上Margin計算出子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());
//如果測量被測量的子ViewLayoutParams為Match_Parent,添加這些子View到需要重新測量的子View集合
if (measureMatchParentChildren) {
if (lp.width == LayoutParams.MATCH_PARENT ||
lp.height == LayoutParams.MATCH_PARENT) {
mMatchParentChildren.add(child);
}
}
}
}
// Account for padding too FrameLayout最後的寬高還需要加上間距值
maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();
// Check against our minimum height and width ,比較目前FrameLayout的寬高是否小于使用者設定的最小值
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
...
//③根據最後計算出的最大寬高,設定FrameLayout的寬高
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec,
childState << MEASURED_HEIGHT_STATE_SHIFT));
count = mMatchParentChildren.size();
if (count > ) {
//根據LayoutParams的寬高是否為Match_Parent與重新計算過後确定的FrameLayout的寬高,重新計算子View寬高
for (int i = ; i < count; i++) {
final View child = mMatchParentChildren.get(i);
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec;
if (lp.width == LayoutParams.MATCH_PARENT) {
final int width = Math.max(, 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);
}
final int childHeightMeasureSpec;
if (lp.height == LayoutParams.MATCH_PARENT) {
final int height = Math.max(, getMeasuredHeight()
- getPaddingTopWithForeground() - getPaddingBottomWithForeground()
- lp.topMargin - lp.bottomMargin);
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
height, MeasureSpec.EXACTLY);
} else {
childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
lp.topMargin + lp.bottomMargin,
lp.height);
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
分析
①判斷是否需要再次測量Match_parent的子View
final boolean measureMatchParentChildren =
MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
這裡先判斷下FrameLayout本身的MeasureSpec是否都是EXACTLY,精确模式那麼FrameLayout的寬高都是确定了的,子View在測量時是Match_Parent,子View的的MeasureSpec也會變成EXACTLY,這樣就沒有再重新測量子View的必要了。可以看下ViewGroup中的getChildMeasureSpec()方法。
case MeasureSpec.EXACTLY:
if (childDimension >= ) {
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;
②開始測量子View的寬高與MeasureMode
measureChildWithMargins(child, widthMeasureSpec, , heightMeasureSpec, );
我們現在具體來看
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);
}
可以看到這裡就是把Margin和Padding加起來再調用getChildMeasureSpec()方法
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
int size = Math.max(, specSize - padding);
int resultSize = ;
int resultMode = ;
switch (specMode) {
// Parent has imposed an exact size on us
case MeasureSpec.EXACTLY:
if (childDimension >= ) {
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 >= ) {
// 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 >= ) {
// 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 ? : 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 ? : size;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
//noinspection ResourceType
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
getChildMeasureSpec()這個方法就是根據父View的MesasureSpec來确定FrameLayout期望子View的MearsureSpec,這部分相關很多部落格都說的很好,我就不重複了。
疑惑思考
onMeasure()代碼裡面看到Match_Parent的子View再FrameLayout不是EXACTIVITY的測量模式時計算了兩次,看代碼的時候一直沒有想通,為什麼需要計算兩次呢?
看着代碼想了很久,分别看了FramLayout的幾種情況,最後發現,如果Framelayout的MeasureMode是AT_MOST的話,第一輪測量子View時,全部子View的Size的最大值還不能确定是多少,是以需要等到一輪測量完成後,FrameLayout的寬高确定了,最後才能給LayoutParams為Match_Parent的子View重新算出寬高。
onLayout
FrameLayout中就一個方法layoutChildren()
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
layoutChildren(left, top, right, bottom, false /* no force left gravity */);
}
具體來分析下layoutChildren:
void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
final int count = getChildCount();
//算出各未知的的padding
final int parentLeft = getPaddingLeftWithForeground();
final int parentRight = right - left - getPaddingRightWithForeground();
final int parentTop = getPaddingTopWithForeground();
final int parentBottom = bottom - top - getPaddingBottomWithForeground();
//和onMeasure類似,循環确定各子View的位置并調用子View的Layout方法
for (int i = ; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
int childLeft;
int childTop;
int gravity = lp.gravity;
if (gravity == -) {
gravity = DEFAULT_CHILD_GRAVITY;
}
final int layoutDirection = getLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
//确定子View的Left與Top的坐标
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
//①這裡可能稍微有點抽象需要多想想
case Gravity.CENTER_HORIZONTAL:
childLeft = parentLeft + (parentRight - parentLeft - width) / +
lp.leftMargin - lp.rightMargin;
break;
case Gravity.RIGHT:
if (!forceLeftGravity) {
childLeft = parentRight - width - lp.rightMargin;
break;
}
case Gravity.LEFT:
default:
childLeft = parentLeft + lp.leftMargin;
}
switch (verticalGravity) {
case Gravity.TOP:
childTop = parentTop + lp.topMargin;
break;
case Gravity.CENTER_VERTICAL:
childTop = parentTop + (parentBottom - parentTop - height) / +
lp.topMargin - lp.bottomMargin;
break;
case Gravity.BOTTOM:
childTop = parentBottom - height - lp.bottomMargin;
break;
default:
childTop = parentTop + lp.topMargin;
}
//知道Left和Top加上寬高就能推出四個點的坐标了
child.layout(childLeft, childTop, childLeft + width, childTop + height);
}
}
}
分析
①這裡可能稍微有點抽象需要多想想,特别是要求子View處于父View中心位置時。
case Gravity.CENTER_HORIZONTAL:
childLeft = parentLeft + (parentRight - parentLeft - width) / +
lp.leftMargin - lp.rightMargin;
break;
直接看這個可能想象起來可能有點複雜,是以我們要先拆一下
先假定父View的左邊位置parentLeft是0,
(parentRight-parentLeft-width)/2=(parentRight-parentLeft)-width/2
(parentRight-parentLeft):求出ParentView的中點
(parentRight-parentLeft)-width/2:減去子View寬度的一半就是子View左邊的位置
(parentRight-parentLeft)-width/2+lp.leftMargin - lp.rightMargin:
求出子View左邊的位置加上Margin後的位置
parentLeft + (parentRight-parentLeft)-width/2+lp.leftMargin - lp.rightMargin,加上父View的左側的位置就是子View最後的位置
後記
由于個人水準有限,可能某些地方分析的不太好或者有錯誤,特别是有很多地方用到了位操作的地方還是沒看的太明白,希望大家多多了解,給予指正。
參考資料
HenCode