View and viewgroup

Source: Internet
Author: User

1. In viewroot. Java, the application calls invalidate or setenable to call invalidate indirectly. invalidate will traverse the view tree and request to redraw the area to be drawn.

Invalidate is mainly used to determine the area to be repainted, and then call scheduletraversals to send a request for redrawing. scheduletraversals finally calls descrimtraversals to traverse the view tree for repainting.

2. Measure: descrimtraversals first traverses and calls the measure method of all views in the view tree to measure the size of the view.

Measure starts from viewroot's mview. This mview is a decorview object (inherited from framelayout, which belongs to viewgroup). The measure function is not overwritten in viewgroup. In fact, measure is a final method, it cannot be overwritten, so the measure of all view subclass calls the measure of view.

Public final void measue (INT widthmeasurespec, int heightmeasurespec );

In measure, viewroot determines widthmeasurespec and heightmeasurespec Based on the screen size and decorview layout (framelayout) as parameters to pass in measue. In measue, viewroot calls back onmeasure, the onmeasure of view calculates the accurate length and width of the input parameters through a certain algorithm. So far, the size of the decview itself is measured. How can the child control in the decview be measure?

Measure is final, but the onmeasure of the callback is virtual. In fact, the size of view and viewgroup is calculated in onmeasure, the default onmeasure only calculates the size of the current view or viewgroup. If it is a custom viewgroup, We can override onmeasure, the viewgroup is case-insensitive (the layout of match_parent or wrap_content written in XML won't work). Of course, this is not recommended. Generally, the subclass of viewgroup will override onmeasure, in addition to calling onmeasure of view to determine its own size, it also determines the size of all child views (if the child view is a viewgroup, continue this process ), how can we determine the size of the sub-view? similar to determining decview in viewroot, we first determine the parameter width of the measure function passed to the sub-view based on the size and layout of the parent viewgroup. Measurespec and heightmeasurespec. The sub-view calls the measure function to calculate the final size of the sub-view (this process is encapsulated in the measurechildren of the viewgroup.

When customizing the subclass of viewgroup, you generally need to override the onmeasure method. In this onmeasure method, you can call the measurechildren method of viewgroup, you can also directly call the measure of all sub-views (measurechildren is not automatically called, but is provided to the user manually when needed, many viewgroup subclasses at the Android Application Layer call the viewgroup subclass to perform measure on the Child view)

Before measure, the view has no size, and getmeasuredwidth and getmeasuredheight () are both 0. Before layout, the getwidth and getheight values are both 0, and the control size can be determined again in layout, therefore, the values obtained by getwith and getheight are not necessarily the same as those obtained by getmeasuredwidth and getmeasuredheight (). The former is the actual size drawn on the screen, the latter is the measurement size after measure is executed. Of course, by default, layout uses the measurement size in measure, unless onlayout is overwritten and the size is redefined.

Therefore, for multiple viewgroup subclasses, onmeasure is not required, because the size and position of the final sub-view are determined by layout, and onlayout is required.

3. Layout: After measure is completed, layout will be called, and the view tree will be traversed for layout. The layout method is also final:

Public void layout (INT left, int top, int right, int bottom)

When a custom viewgroup is placed in the layout XML, the parent layout calls its measure and layout, so you don't need to worry about it. However, its child views depend on their own measure and layout, and the child views (non-viewgroup) onmeasure and onlayout of are only the default methods called by the parent viewgroup, and do not need to be overwritten. The child viewgroup must override onmeasure and onlayout to call Measure and layout (onmeasure is optional for all the child views below, onlayout required)

Viewroot calls layout (, measurewith, measureheight) of decorview Based on the measurewith and measureheight obtained in the preceding measurement. decorview overrides onlayout and calls layout of all sub-views, if the child view is a viewgroup, the Child view overwrites onlayout, and so on.

Layout mainly determines the position and size of its sub-view (the size can be measured by measure, or you can define the size by yourself). Taking linearlayout as an example, if it is a vertical layout, for each layout sub-view, the top of the next sub-view is added with the height of the previous view. If it is a custom viewgroup, the layout position of the sub-view is determined based on the layout needs. For example, you can design a viewgroup that adapts to the automatic line feed of the screen by length.

4. Draw: View Traversal
Tree calls the draw of all views. The size and position of the draw have been determined by measure and layout.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.