Onmeasure (measurement) Introduction and use of detailed
When customizing the view, it's really simple, just know the 3 steps:
1. Measurement--onmeasure (): Determines the size of the view
2. Layout--onlayout (): Determines the location of the view in ViewGroup
3. Draw--ondraw (): How to draw this view.
and the 3rd step of the OnDraw system has been packaged very well, basically do not worry about us, only need to focus on 1, 22 steps in the good.
In general, a custom control overrides the view's Onmeasure () method, because it specifies the size of the control on the screen. Before we say the Onmeasure () method, let's look at the class Measurespec
Detailed description of the Measurespec class (detailed measurement values)
Android.view.View.MeasureSpec
Measure's Chinese meaning is measurement. So its function is to measure the size of the view.
It only takes two values to determine the size of the view: wide detailed measurements (WIDTHMEASURESPEC) and high-detail measurements (HEIGHTMEASURESPEC). You can also interpret the detailed measurements as a description of the size of the view you want (not necessarily the final size ).
For a detailed measurement (MEASURESPEC), two things are needed to determine it, that is size and pattern (mode). and measurespec,size,mode their three relationships, are encapsulated in the view class in an inner class, called Measurespec.
Measurespec It has three modes :
(1) Measurespec. exactly is the exact size , when we specify a specific value for the control, such as andorid:layout_width= "50dip", or fill_parent is, is the size of the control has been determined, are precise dimensions.
(2) Measurespec. At_most is the largest size , such as: When the control's layout_width is specified as wrap_content, the size of the control generally changes as the content is not larger than the maximum size allowed by the parent control. Therefore, mode at this point is at_most,size gives the maximum size allowed by the parent control.
(3) Measurespec. UNSPECIFIED is not a specified size , this is not a lot, it is generally the parent control is Adapterview, passed through the measure method of the mode.
Measurespec The three functions it uses:
1.static int getmode(int measurespec): Extract mode according to the measured value (format) provided (one of the three modes above)
2.static int getsize(int measurespec): Extracts the size value according to the measured value (format) provided (this size is what we usually call size)
3.static int makemeasurespec(int size,int mode): Creates a measure value (format) based on the provided size value and mode
Note that using exactly and at_most is usually the same effect, if you want to distinguish them, then you will use the above function view.resolvesize () to return a size value, Then use your view to call the Setmeasureddimension (int,int) function.
The following is the source code for the Measurespec class:
/*** Measurespec encapsulates the layout requirements that the parent layout passes to the child layout, each Measurespec represents a set of requirements for width and height * Measurespec consists of size and mode. * Three mode: * 1.UNSPECIFIED * The parent does not have any constraints on the child, which can be any size (that is, unspecified) * (UNSPECIFIED in the source of processing and exactly. When the width height value of the view is set to 0 or if the width is not set, the pattern is unspecified * 2.EXACTLY * The parent determines the exact size of the child, the child is confined to the given boundary, ignoring its desired size. * (When the width or height is set to Match_parent, the mode is exactly, because the child view occupies the space of the remaining container, so it is determined by the size) * 3.at_most * Sub Maximum can reach the specified size * (When set to WRAP_ Content, the pattern is At_most, which indicates the maximum size of the child view, so that the child view will set its own size according to this limit) * * Measurespecs uses binary to reduce the allocation of objects. */ Public classMeasurespec {//The Carry size is 2 of the 30 square (the size of int is 32 bits, so the carry 30 bit is to use the highest bit of int and the second-to-last digit is 32 and 31 bits to do the flag bit) Private Static Final intMode_shift = 30; //operation Matte, 0x3 is 16, 10 binary is 3, binary is 11. 3 left carry 30, that is 11 00000000000 (11 followed by 30 0)//(The effect of the mask is to use 1 to annotate the desired value, and 0 to label the unwanted value.) Because 1 and any number do and operations are any number, 0 and any number do and operations are 0) Private Static Final intMode_mask = 0x3 <<Mode_shift; //0 left carry 30, that is 00 00000000000 (00 followed by 30 0) Public Static Final intUNSPECIFIED = 0 <<Mode_shift; //1 left carry 30, that is 01 00000000000 (01 followed by 30 0) Public Static Final intexactly = 1 <<Mode_shift; //2 left carry 30, that is 10 00000000000 (10 followed by 30 0) Public Static Final intAt_most = 2 <<Mode_shift; /*** Get a detailed measurement result based on the size and mode provided*/ //measurespec = size + mode; (Note: binary addition, not 10-binary addition!) ) //The purpose of this design is to use a 32-bit binary number, 32 and 31 bits to represent the value of mode, and the last 30 bits to represent the value of size//For example size=100 (4), Mode=at_most, then measurespec=100+10000...00=10000..00100 Public Static intMakemeasurespec (intSizeintmode) { returnSize +mode; } /*** Get mode with detailed measurement results*/ //mode = Measurespec & mode_mask; //Mode_mask = 11 00000000000 (11 followed by 30 0), the principle is to replace the 30-bit 0 in the post-mode_mask 30 bit with 1 in the post-Measurespec, and then retain the MODE value of 32 and 31 bits. //For example, 00..00100 & 11 00..00 (11 followed by 30 0) = 00..00 (At_most), which gives the value of mode Public Static intGetMode (intMeasurespec) { return(Measurespec &mode_mask); } /*** Get size with detailed measurement results*/ //size = Measurespec & ~mode_mask; //The principle is the same as above, but this time is to reverse the mode_mask, that is, 00 111111 (00 followed by 30 1), 32, 31 is replaced by 0 is to remove mode, retain the 30 bit size Public Static intGetSize (intMeasurespec) { return(Measurespec & ~mode_mask); } /*** Override the ToString method, print the mode and size information, omitted here*/ Public StaticString toString (intMeasurespec) { return NULL; }}
Onmeasure () method detailed description
Therefore, when overriding the Onmeasure method, the dimensions are calculated differently depending on the pattern.
/** * This method needs to be rewritten and should be determined by subclasses to determine the measured width and height,
*/ protected void onmeasure (intint heightmeasurespec) { setmeasureddimension( Getdefaultsize(getsuggestedminimumwidth(), Widthmeasurespec), getdefaultsize ( Getsuggestedminimumheight (), Heightmeasurespec));}
The two parameters passed in by onmeasure (int, int) are the size passed in by the previous control , and in many cases, override the method to calculate The actual size of the control, and then call setmeasureddimension (int, int) to set the actual size.
(1) getdefaultsize (): this is Get minimum width as the default value, and then based on the specific measurement values and selected modes to get Widthmeasurespec. Then pass him to the following method.
(2) setmeasureddimension (): This method is the ultimate goal that we want to achieve by rewriting onmeasure (). Its function is to set our measured wide and high values.
Onmeasure incoming Widthmeasurespec and Heightmeasurespec are not general dimensional values, but are values that combine patterns and dimensions .
// Get Patterns int mode = Measurespec.getmode (widthmeasurespec)// get dimension int size = Measurespec.getsize (WIDTHMEASURESPEC)
The following code is a more typical approach:
@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {setmeasureddimension (Getmeasuredlength (Widthmeasurespec,true), Getmeasuredlength (Heightmeasurespec,false)); } Private intGetmeasuredlength (intLengthBooleaniswidth) { intSpecmode =measurespec.getmode (length); intSpecsize =measurespec.getsize (length); intsize; intpadding = iswidth? Getpaddingleft () +getpaddingright (): Getpaddingtop ()+Getpaddingbottom (); if(Specmode = =measurespec.exactly) {size=specsize; } Else{size= Iswidth? padding + MWAVE.LENGTH/4: Default_height+padding; if(Specmode = =measurespec.at_most) {Size=math.min (size, specsize); } } returnsize; }
Before measuring, be clear about whether a view (for example, TextView), a viewgroup (such as linearlayout), or multiple viewgroup nesting are measured.
If there is only one view we can measure this one, and if there are multiple views or viewgroup nested we need to iterate through all of the view.
Android--measurespec class and Onmeasure () methods