"Onmeasure"
A custom control that inherits view directly needs to override the Onmeasure method and set its own size when wrap_content, otherwise using wrap_content in the layout is equivalent to match_parent.
@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { intWidthmode =Measurespec.getmode (WIDTHMEASURESPEC); intHeightmode =Measurespec.getmode (HEIGHTMEASURESPEC); intWidthsize =measurespec.getsize (WIDTHMEASURESPEC); intHeightsize =measurespec.getsize (HEIGHTMEASURESPEC); intWidth= -;//Adjust the default value size as required intheight= -;//Adjust the default value size as required if(Widthmode = = Measurespec.at_most && Heightmode = =measurespec.at_most) {setmeasureddimension (width, height); }Else if(Widthmode = =measurespec.at_most) {setmeasureddimension (width, heightsize); }Else if(Heightmode = = Measurespec.at_most){
Setmeasureddimension (widthsize, height);
}
}
LinearLayout calls through Measure Onmeasure
ViewGroup is an abstract class, there is an abstract method OnLayout; Onmeasure method is not overridden, but a measurechildren is provided to measure each child element
ViewGroup's measurement process onmeasure is generally overridden in its subclasses, such as Onmeasure in LinearLayout, which first determines the linear direction and then traverses the measurement (performed on each child element
Measurechildbeforelayout () method, which internally calls the child element's measure () method to measure its own size) child element, the child element is measured, linearlayout will measure its own size
Setmeasureddimension (Resolvesizeandstate (MaxWidth, Widthmeasurespec, childstate), heightsizeandstate);
View is not an abstract class
The exact width and height of the view may not be available in the activity's lifecycle Oncreat,onresume, because the measure process of the view and the activity's life cycle are not synchronized.
WORKAROUND: 1. Execute View.getmeasuredwidth () in the onwindowfocuschanged of the view
"OnLayout"//linearlayout through layout call OnLayout
The layout method determines the location of the view itself, and onlayout iterates through the child elements (through Setchildframe) to invoke the layout method of the child elements to determine the position of the child elements.
"OnDraw"
Call the following four methods by using the Draw method
1. Draw the background background.draw (canvas).
2. Draw your own OnDraw (canvas).
3. Draw Children (Dispatchdraw (canvas)).
4. Draw Decorations (Ondrawscrollbars (canvas)).
The rendering of the view is implemented through Dispatchdraw, where the dispatchdraw iterates through the draw method of all the child elements, so that the draw event can be passed down one layer at a
The viewroot corresponds to the Viewrootimpl class, which is the link between WindowManager and Deco view (framelayout).
Android draws a small knowledge point of view