privatebooleanmeasureHierarchy(final View host, final WindowManager.LayoutParams lp, final Resources res, finalint desiredWindowWidth, finalint desiredWindowHeight){ boolean windowSizeMayChange = false; boolean goodMeasure = false; if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) { // On large screens, we don't want to allow dialogs to just stretch to fill // the entire width of the screen to display one line of text. // First try doing the layout at a smaller size to see if it will fit. // ... }
// ViewGroup.java /** * 根据父View的MeasureSpec & 子View的布局参数LayoutParams,计算子View的MeasureSpec * @param spec 父view的MeasureSpec * @param padding 父view的padding, margins * @param childDimension 子视图的LayoutParams(宽 or 高) * @return a MeasureSpec integer for the child */ publicstaticintgetChildMeasureSpec(int spec, int padding, int childDimension){ // 父View的测量模式 int specMode = MeasureSpec.getMode(spec); // 父View的size int specSize = MeasureSpec.getSize(spec); // 通过父View计算出的子view = 父大小 - 边距(父View要求的大小,但子view不一定用这个值) int size = Math.max(0, specSize - padding); // 子view想要的实际大小和模式 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; } elseif (childDimension == LayoutParams.MATCH_PARENT) { // Child wants to be our size. So be it. resultSize = size; resultMode = MeasureSpec.EXACTLY; } elseif (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; } elseif (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; } elseif (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; } elseif (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; } elseif (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); }
// ViewRootImpl int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width); int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
privatestaticintgetRootMeasureSpec(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; }
publicstaticintgetDefaultSize(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; }
ViewGroup.measureChildWithMargins
1 2 3 4 5 6 7 8 9 10 11 12
protectedvoidmeasureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed){ final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
privatevoidperformLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, int desiredWindowHeight){ mInLayout = true; final View host = mView; host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
mInLayout = false; int numViewsRequestingLayout = mLayoutRequesters.size(); if (numViewsRequestingLayout > 0) { // requestLayout() was called during layout. // we need to clear those flags and do a full request/measure/layout pass to handle this situation. ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, false); if (validLayoutRequesters != null) { mHandlingLayoutInLayoutRequest = true;
int numValidRequests = validLayoutRequesters.size(); for (int i = 0; i < numValidRequests; ++i) { final View view = validLayoutRequesters.get(i); // 对在layout期间调用了requestLayout()方法的View,在host.layout结束后重新requestLayout view.requestLayout(); } // 调用performMeasure重新测量 measureHierarchy(host, lp, mView.getContext().getResources(), desiredWindowWidth, desiredWindowHeight); mInLayout = true; // 重新layout host View host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
mHandlingLayoutInLayoutRequest = false;
// Check the valid requests again, this time without checking/clearing the // layout flags, since requests happening during the second pass get noop'd validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true); if (validLayoutRequesters != null) { final ArrayList<View> finalRequesters = validLayoutRequesters; // View.post // 将第二次请求发送到下一帧 getRunQueue().post(new Runnable() { @Override publicvoidrun(){ int numValidRequests = finalRequesters.size(); for (int i = 0; i < numValidRequests; ++i) { final View view = finalRequesters.get(i); view.requestLayout(); } } }); } } } mInLayout = false; }
@Override protectedvoidonLayout(boolean changed, int left, int top, int right, int bottom){ super.onLayout(changed, left, top, right, bottom); // ... }
@Override protectedvoidonLayout(boolean changed, int left, int top, int right, int bottom){ layoutChildren(left, top, right, bottom, false/* no force left gravity */); }
voidlayoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity){ finalint count = getChildCount();
finalint parentLeft = getPaddingLeftWithForeground(); finalint parentRight = right - left - getPaddingRightWithForeground(); finalint parentTop = getPaddingTopWithForeground(); finalint parentBottom = bottom - top - getPaddingBottomWithForeground();
for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { // 当前子View不是GONE则layout final LayoutParams lp = (LayoutParams) child.getLayoutParams(); finalint width = child.getMeasuredWidth(); finalint height = child.getMeasuredHeight();
int childLeft; int childTop; int gravity = lp.gravity; if (gravity == -1) { gravity = DEFAULT_CHILD_GRAVITY; }
publicvoiddraw(Canvas canvas){ // Step 1, draw the background, if needed int saveCount; if (!dirtyOpaque) { drawBackground(canvas); }
// skip step 2 & 5 if possible (common case) finalint viewFlags = mViewFlags; boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0; boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0; if (!verticalEdges && !horizontalEdges) { // Step 3, draw the content if (!dirtyOpaque) onDraw(canvas);
// Step 4, draw the children dispatchDraw(canvas);
drawAutofilledHighlight(canvas);
// Overlay is part of the content and draws beneath Foreground if (mOverlay != null && !mOverlay.isEmpty()) { mOverlay.getOverlayView().dispatchDraw(canvas); }
// Step 2, save the canvas' layers int paddingLeft = mPaddingLeft;
int left = mScrollX + paddingLeft; int right = left + mRight - mLeft - mPaddingRight - paddingLeft; int top = mScrollY + getFadeTop(offsetRequired); int bottom = top + getFadeHeight(offsetRequired); // ...
saveCount = canvas.getSaveCount();
// Step 3, draw the content if (!dirtyOpaque) onDraw(canvas);
// Step 4, draw the children dispatchDraw(canvas);
// Step 5, draw the fade effect and restore layers final Paint p = scrollabilityCache.paint; final Matrix matrix = scrollabilityCache.matrix; final Shader fade = scrollabilityCache.shader;
// Overlay is part of the content and draws beneath Foreground if (mOverlay != null && !mOverlay.isEmpty()) { mOverlay.getOverlayView().dispatchDraw(canvas); }