MEASUREMENT PROCESS OF View AND View GROUP
Views have three main processes, measurement, layout, draw, i.e. measurement, layout and drawing. Let's first analyze the first process measure.
1. Measurement process of View
View measurement process is completed by its measure(), which is final type, that is, can not be overridden, colleague measure() will transfer onMeasure(), so we can focus on onMeasure() for the time being. In fact, when we customize a view control, we also override onMeasure method.
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
The above method is to call setMeasuredDismension() to set mMeasuredWidth and mMeasuredHeight. These two variables will be used in the layout and draw ing processes of View. But when setMeasuredDismension is called, its parameters are obtained by getDefaultSize(). The source code is as follows:
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;
}
protected int getSuggestedMinimumWidth() {
return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}
Where getDefaultSize is based on View's MesureSpec and another parameter size to get the return value.
Among them, we need to pay attention to the two modes of MeasureSpec.AT_MOST and MesureSpec. EXACTLY, that is, both return specSize. This specSize is the size of getsize called by MesureSpec of View generated by MesureSpec of parent container and LayoutParams of View itself, which can be understood as View.
The width and height obtained after measurement.
Another pattern above
MeasureSpec.UNSPECIFIED is used for internal measurement of the system. In this case, the return value is size, that is, the return value of getSuggested MinimumWidth and getSuggested MinimumHeight.
From the getSuggested MinimumWidth () source code, you can conclude that if View has no background, View
Width is the value of android:minWidth. If this value is not set, it is 0. If the background is set, it is the larger value of the width of android:minWidth and background icon.
2. Measurement process of ViewGroup
Since measure cannot be overridden and ViewGroup does not override onMeasure(), this measurement process is handled in other ways. In fact, measureChildren() is used.
ViewGroup.java
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) {//Visible
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
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);
}
The above code calls the measure() method of child view iteratively, and if child view has children, the measureChildren() will be executed recursively.
Where getChildMeasureSpec() has previously noted "View"
MeasureSpec and LayoutParams Relations have been analyzed in this article, and we will repeat the analysis here.
So we can know that ViewGroup does not perform a specific measurement process, but only calls the measure() method of child view. This is because there are too many ViewGroup and so on, which is not easy to handle uniformly. In fact, it is all kinds of things to rewrite onMeasure from their own processing.