For a normal view, the measurement is measured in the Measurechildwithmargins function in ViewGroup, which calls child view's measure.
1: Start with the measure function
1 Public Final voidMeasureintWidthmeasurespec,intHeightmeasurespec) {2 if((Mprivateflags & force_layout) = = Force_layout | |3Widthmeasurespec! = Moldwidthmeasurespec | |4Heightmeasurespec! =Moldheightmeasurespec) {5 6 //First clears the measured dimension flag7Mprivateflags &= ~Measured_dimension_set;8 9 if(viewdebug.trace_hierarchy) {TenViewdebug.trace ( This, ViewDebug.HierarchyTraceType.ON_MEASURE); One } A - //measure ourselves, this should set the measured dimension flag back - onmeasure (Widthmeasurespec, heightmeasurespec); the - //flag not set, Setmeasureddimension () is not invoked, we raise - //An exception to warn the developer - if((Mprivateflags & measured_dimension_set)! =Measured_dimension_set) { + Throw NewIllegalStateException ("Onmeasure () did not set the" -+ "measured dimension by calling" ++ "setmeasureddimension ()"); A } at -Mprivateflags |=layout_required; - } - -Moldwidthmeasurespec =Widthmeasurespec; -Moldheightmeasurespec =Heightmeasurespec; in}
Official description of this function: The measure function is used to calculate the size of a view, and its incoming parameter is the width/height limit information of Parentview for this view. The actual dimension measurements will be called Onmeasure to complete , so the subclass must override the Onmeasure function.
Through the source code of the measure function we can also know: first, it is final, so it can not be rewritten, and secondly, it is mainly on the parentview of the width of the limit information is the same as the last time the same judgment, if the same does not call onmeasure re-measurement;
2:measure--> onmeasure function
1 protected void onmeasure (intint heightmeasurespec) {2 Setmeasureddimension (Getdefaultsize (Getsuggestedminimumwidth (), Widthmeasurespec),3 Getdefaultsize (Getsuggestedminimumheight (), Heightmeasurespec)); 4 }
Through its code, we can know that the main use of these three functions, we analyze each:
2.1:getsuggestedminimumwidth function: Returns the maximum value between the Android:minwidth and the background width
1 protected intgetsuggestedminimumwidth () {2 intSuggestedminwidth = Mminwidth;//mminwidth corresponds to the value specified by the Android:minwidth property, and defaults to 0 if it is not specified3 4 if(Mbgdrawable! =NULL) {//If you set a background5 Final intBgminwidth = Mbgdrawable.getminimumwidth ();//the width of the background, if it is shapedrawable, then 0, if it is bitmapdrawable, it has the original width height6 if(Suggestedminwidth < Bgminwidth) {//If the minimum setting is less than the background image width, set the minimum value to the background image width7Suggestedminwidth =Bgminwidth;8 }9 }Ten One returnSuggestedminwidth; A}
2.2:getdefaultsize: Returns size if Measurespec is not qualified, otherwise returns the size in Measurespec
1 Public Static intGetdefaultsize (intSizeintMeasurespec) {2 intresult =size;3 intSpecmode =Measurespec.getmode (measurespec);4 intSpecsize =measurespec.getsize (measurespec);5 6 Switch(specmode) {7 Casemeasurespec.unspecified:8result =size;9 Break;Ten CaseMeasurespec.at_most: One Casemeasurespec.exactly: Aresult =specsize; - Break; - } the returnresult; -}
To this: we can summarize getdefaultsize (Getsuggestedminimumwidth (), Widthmeasurespec) Logic: If the Parentview has no limit on dimensions (that is, the Parentview mode is unspecified), the suggested minimum value of the view is returned, otherwise the specified size of Parentview is returned. Getdefaultsize (Getsuggestedminimumheight (), Heightmeasurespec) are similar.
2.3 Setmeasureddimension: Very simple, set the measurement of the width of the height (note is not the actual width of the view, the actual width of the layout after the completion of the definition, although almost all is the width of the measure after the high)
1 protected Final void setmeasureddimension (intint measuredheight) {2 Mmeasuredwidth = measuredwidth; 3 Mmeasuredheight = measuredheight; 4 5 Mprivateflags |= measured_dimension_set; 6 }
At this point, the analysis of the Onmeasure function ends.
Measure process for normal view