The display of each view/viewgroup goes through three processes:
1, measure process (measuring the size of view display, position);
2. Layout process (placement of view);
3, Draw process (the previous article said through the canvas drawing to the interface display, formed a variety of view)
Here's a look at each of the processes:
Measure Process:
Because Decorview is actually a class derived from Framelayout, which is a viewgroup instance, the Contentviews inside the ViewGroup is a ViewGroup instance, Embed a view or viewgroup in turn to form a view tree. So the measure function is to calculate the actual size for the entire view tree, setting the Layout size ("window" size) for each view object. The actual corresponding attribute is the Mmeasuredheight (high) and mmeasurewidth (wide) in the view.
View in the measure process, the main three methods:
Public final void measure (int widthmeasurespec, int heightmeasurespec)
Protected final void setmeasureddimension (int measuredwidth, int measuredheight)
protected void onmeasure (int Widthmeasurespec, int heightmeasurespec)
protected final void setmeasureddimension (int measuredwidth, int Measuredheight). (This method is called in the Onmesare method, and if no call is required manually)
The first two functions are final and cannot be overloaded, so we must overload the Onmeasure function in a non-abstract class derived from ViewGroup. The principle of implementing measure is: If the view also has child view, then measure the child view, until all sub-view completes measure operation, then measure oneself. The Measurechild or measurechildwithmargins provided in ViewGroup is the implementation of this function. Each view/viewgroup eventually calls the Setmeasureddimension method, sets the size of the view, and if it is not called, the measurement does not work.
Before we introduce the principle of measurement, we should first understand some basic knowledge, that is, the parameters of the measure function are a 32-bit integer generated by the Makemeasurespec function method of class Measurespec, the high two-bit representation mode (mode) of the integer. The lower 30 bits are the specific size (specsize).
There are three modes of measurespec, namely unspecified, exactly and At_most, each representing the following meanings:
If the at_most,specsize represents the largest available size;
If the exactly,specsize represents a precise size;
In the case of unspecified, there is no reference to the size of the control.
So, for a view of the above mode and specsize values are obtained by default, they are based on the view of the Layoutparams parameters to obtain:
When the parameter is fill_parent/match_parent, mode is exactly,specsize for all remaining space;
The parameter is a specific value, such as a pixel value (px or DP), and mode is exactly,specsize for the incoming value;
parameter is Wrap_content,mode for At_most,specsize runtime decision
Specific measuring principle
The mode and specsize provided above are just one of the desired dimensions of the program for the view, and ultimately the allowable size of a view object from the parent view is determined by the child view's desired size and the parent view's ability size (available dimensions). The settings for the desired size can be set by android:layout_width and Android:layout_height defined in the layout resource file, It can also be set by the Layoutparams parameter passed in when the code is called by the AddView function. The size of the parent view's ability is ultimately the Decorview size, which is full screen and is determined by the phone's resolution. The relationship between the desired size, the size of the capability and the final allowable size can be obtained by reading the source code of the Getchildmeasurespec function that measurechild or Measurechildwithmargins will invoke.
According to the Getdefaultsize function called by the Onmeasure function of the source view, the control has a minimum size by default, which can be set by setting Android:minheight and Android:minwidth ( Default is 0 when no setting is set, the minimum size of background drawable is compared with the minimum size of the previous setting, whichever is larger, as the minimum size of the control. This minimum size is selected in the case of unspecified, and other cases are based on allowable dimensions.
Layout process:
The result of the above measure process is to set the height and width of the view, the function of the layout process is to set the view in the parent view of the four points (respectively, the view four member variable Mleft,mtop,mright,mbottom). The same layout is also defined by the FIANL modifier as not overloaded, but in ViewGroup the OnLayout function is modified by the abstract, that is, all classes derived from ViewGroup must implement the OnLayout function, This enables layout settings for all of the child views it contains.
Let's take a look at any of the layout layouts, using LinearLayout as an example, the OnLayout (Layoutvertical method) method part of the source code:
voidlayoutvertical () {Final intPaddingleft =Mpaddingleft; intChildtop; intChildleft; //Where right end of the child should go Final intwidth = mright-Mleft; intChildright = width-Mpaddingright; //Space available for child intChildspace = Width-paddingleft-Mpaddingright; Final intCount =Getvirtualchildcount (); Final intmajorgravity = mgravity &Gravity.vertical_gravity_mask; Final intminorgravity = mgravity &Gravity.relative_horizontal_gravity_mask; Switch(majorgravity) { CaseGravity.bottom://Mtotallength contains the padding alreadyChildtop = mpaddingtop + Mbottom-mtop-mtotallength; Break; //Mtotallength contains the padding already CaseGravity.CENTER_VERTICAL:childTop= Mpaddingtop + (mbottom-mtop-mtotallength)/2; Break; CaseGravity.top:default: Childtop=Mpaddingtop; Break; } for(inti = 0; I < count; i++) { FinalView Child =Getvirtualchildat (i); if(Child = =NULL) {Childtop+=Measurenullchild (i); } Else if(Child.getvisibility ()! =GONE) { Final intChildwidth =child.getmeasuredwidth (); Final intChildheight =child.getmeasuredheight (); FinalLinearlayout.layoutparams LP =(Linearlayout.layoutparams) child.getlayoutparams (); intGravity =lp.gravity; if(Gravity < 0) {Gravity=minorgravity; } Final intLayoutDirection =getresolvedlayoutdirection (); Final intAbsolutegravity =gravity.getabsolutegravity (Gravity, layoutdirection); Switch(Absolutegravity &gravity.horizontal_gravity_mask) { CaseGravity.CENTER_HORIZONTAL:childLeft= Paddingleft + ((childspace-childwidth)/2) + Lp.leftmargin-Lp.rightmargin; Break; CaseGravity.RIGHT:childLeft= Childright-childwidth-Lp.rightmargin; Break; CaseGravity.left:default: Childleft= Paddingleft +Lp.leftmargin; Break; } if(Hasdividerbeforechildat (i)) {Childtop+=Mdividerheight; } childtop+=Lp.topmargin; Setchildframe (Child, Childleft, Childtop+Getlocationoffset (Child), Childwidth, childheight); Childtop+ = Childheight + Lp.bottommargin +Getnextlocationoffset (child); I+=Getchildrenskipcount (Child, I); } } }View Code
From the point of view of the code, it is clear that the layout is high and wide according to the measure process, and the starting position in the parent view is combined. Plus the Layoutgravity property of the view to set the exact position of the four points (in LinearLayout, the Layoutweight attribute is also added). This process is relatively less complex than measure. When we write the control, we can use the results of the measure calculation to control the position of each control according to our own needs, and realize various brilliant animation interactions.
This is what I wrote in the project a linelayout that is written using measure+layout to display a line, display text on the left, and icon on the right; when the text shines through a screen, automatically intercept the ellipsis, the right icon needs to always show And, of course, there are many shortcomings and areas to be improved, and expect to point out and improve.
http://download.csdn.net/detail/huangbiao86/7521721