Problem:
In the process of getting familiar with android, I found that the initialization of a view or layout or the construction process is still clear, that is, it is added to the parent control, then the corresponding lifecycle is started. But how did the parent control of the entire interface, or the first view of the system, initialize and draw it?
Overview:
This article attempts to analyze and understand the measure process of the view, and analyzes the correspondence between MATCH_PARENT and MATCH_PARENT in LayoutParams; the calculation process of onMeasure default value; I have explained the problem in the annotation in the onMeasure interface and raised a question: how does ViewRootImpl be created? For the next article. Finally, we will discuss how to override the onMeasure () method.
Mappings between MATCH_PARENT and MATCH_PARENT in LayoutParams
Why does this article start with perform? See Android animation for in-depth analysis
Start the measure process in android. view. ViewRootImpl. javasmtraversals ().
childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width); childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height); host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
In android. view. ViewRootImpl, we can see that the three values in LayoutParams correspond internally, that is
LayoutParams. MATCH_PARENT corresponds to MeasureSpec. EXACTLY
. LayoutParams. WRAP_CONTENT corresponds to MeasureSpec. AT_MOST
The default value (that is, the specific value) corresponds to MeasureSpec. EXACTLY.
That is, there are only two internal modes: EXACTLY exact mode and AT_MOST Max mode.
private 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; }
In this getRootMeasureSpe (), the makeMeasureSpec () function is called. From the implementation point of view, the mode Value and the specific size are merged into a value through the bitwise AND method. Because the screen size is far smaller than 30 digits, the maximum two digits are used to identify the calculation mode.
private static final int MODE_SHIFT = 30; public static final int EXACTLY = 1 << MODE_SHIFT; public static final int AT_MOST = 2 << MODE_SHIFT;
public static int makeMeasureSpec(int size, int mode) { return size + mode; }After obtaining the wide and high measureSpec, call View. measure (). You can see that the measure process calls the onMeasure () method of view. This is the onMeasure () method to be rewritten if you want to customize the view.
public final void measure(int widthMeasureSpec, int heightMeasureSpec) { if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT || widthMeasureSpec != mOldWidthMeasureSpec || heightMeasureSpec != mOldHeightMeasureSpec) { // measure ourselves, this should set the measured dimension flag back onMeasure(widthMeasureSpec, heightMeasureSpec); } mOldWidthMeasureSpec = widthMeasureSpec; mOldHeightMeasureSpec = heightMeasureSpec; }
From the onMeasure comment, we can see that:
1. The onMeasure () method is used to calculate the width and height. The subclass needs to override this method to provide a more accurate and efficient calculation method.
2. If you rewrite this method, you must call setMeasuredDimension (int, int) to save the calculated results with a high width. Otherwise, an IllegalStateException is thrown;
3. The implementation of the base class uses the background size by default to calculate the width and height,
4. if you override this method, make sure that the result of calculating the width height is not smaller than the smallest width and height of the view. ({@ link # getSuggestedMinimumHeight ()} and * {@ link # getSuggestedMinimumWidth ()})
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); }
Calculation process of onMeasure Default Value
Here we will look at how the default value is calculated, and then discuss how to write the custom onMeasure.
1. First, get the recommended minimum height, getSuggestedMinimumWidth (). if the background is not empty, use the minimum width and height of the background. The mMinWidth here is the minWidth or minHeight in the layout attribute. If no value is written, the default value is zero. It can be seen that the minimum value written in the layout process can indeed ensure the minimum size of the view by default.
Why is the maximum value not provided?
protected int getSuggestedMinimumWidth() { int suggestedMinWidth = mMinWidth; if (mBGDrawable != null) { final int bgMinWidth = mBGDrawable.getMinimumWidth(); if (suggestedMinWidth < bgMinWidth) { suggestedMinWidth = bgMinWidth; } } return suggestedMinWidth;
}
<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">android.view.View.View(Context, AttributeSet, int)</span>
<span style="font-family: Arial, Helvetica, sans-serif;"> case R.styleable.View_minWidth:</span>
mMinWidth = a.getDimensionPixelSize(attr, 0); break;
2. next, let's take a look at getDefaultSize (). Here, according to The measureSpec mode, if the EXACTLY exact mode and the AT_MOST Max mode are used, specSize is used. Otherwise, the passed parameter is the getSuggestedMinimumWidth () calculated just (), max (minimum width and high value, background width and height)
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; }3. Therefore, the width and height attribute is set in the xml file. The default calculation result is specSize. For rootViewImpl, both LayoutParams. MATCH_PARENT and LayoutParams. WRAP_CONTENT are windowSize; otherwise, the lp width and height, that is, m?wattributes
WindowManager.LayoutParams lp = mWindowAttributes;
The value assigned to msf-wattributes is copyfrom () in android. view. ViewRootImpl. setView (View, LayoutParams, View), that is, the attribute of the Root view. Is the attribute of the initial view.
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { synchronized (this) { if (mView == null) { mView = view; mFallbackEventHandler.setView(view); mWindowAttributes.copyFrom(attrs);
So where does ViewRootImpl come from? To be continued...
Discuss how to override the onMeasure () method
After reading the default processing method. As well as the comments left by google's siege lions. You can provide to provide better measurements of their content. Because the content is customized by yourself, you should calculate the width and height according to your own requirements.
According to the requirements of the system, 1. Ensure that the value is greater than the min value; 2. Call setMeasuredDimension (int, int) in onMeasure () to save the results of the calculated width and height.
Leave a tail
In the analysis process, where does ViewRootImpl come from? To be continued...