Android Kernel Analysis Reading Notes Chapter 1 view working principle [view tree traversal]

Source: Internet
Author: User

View status classification

The view defines various States related to the interface effects, such as focusing focused and pressing pressed. Different States usually display different interface effects, in addition, the view status changes with the user's operations. Generally, the selector in the XML file is used to declare the background image used in various States. All status codes are located inStatelistdrawable,Common status codes include:

    1. Enable: whether the current view is available. developers can change it through setenable (), which is completely controlled by developers;
      When the status is unavailable, view will not respond to any events;
    2. Focused: whether the current view has a focus. Only one view in a window has a focus, which is changed dynamically with user operations;
      This status mainly applies to keys, because all key messages will be distributed to the focused view;
    3. Pressed: whether or not the current view is being pressed, mainly for touch messages. Generally, when you press the view, there will be a significant change, but also dynamic changes with user operations;
    4. Selected: whether the current view has been selected. Multiple Views can be selected in a window. developers can change the view by setselected (), which is completely controlled by developers;

Overall cause of view tree re-Traversal

Traversing the view tree means that the entire view needs to be re-allocated and re-painted to its subviews. Generally, there are three reasons for re-Traversing: first, the internal status of the view changes, for example, the display attribute is changed from gone to visible. Second, if a view is added or deleted in viewgroup, the position of the subview needs to be assigned again. Third, the size of the view changes, for example, the text content in textview becomes less variable;

InCodeIn the end, all three functions in view are directly or indirectly called: requestlayout/requestfocus/invalidate, therefore, viewrootimpl will be executed in the top parent view.Scheduletraversals (); In this method, the system will initiate an asynchronous message (sent directly through handler in the old version, choreographer is introduced in the new version 4.1, and support for vsync and third-level buffer, make the page display and Operations smoother. For details, see android
Project butter analysis), and then call javasmtraversals () to complete the view tree traversal during asynchronous message execution. For details, see:

How do I manage multiple attribute variables in view?

A large view class involves a lot of status codes, such as whether the status code is available, whether it is pressed, whether it needs to be re-allocated, and whether it needs to be re-painted; the view tree performs operations based on different variable values when traversing and re-painting. For this reason, the view introduces bit tags to manage these State values, the mviewflags and mprivateflags variables are used for management respectively (as the status code increasesMprivateflags2/mprivateflags3They are all int type, that is, in theory, each variable can be used to indicate 32 State values. when changing the State values, the bitwise operator is used;

Among them, the mviewflags variable is mainly used to save values related to the view status, such as whether to click, whether to double-click, whether to be available, and whether to have focus;

The mprivateflags variable is mainly used to save attributes related to internal logic, such as whether to re-allocate the location, whether to re-paint, and whether to refresh the View cache;

Note: These two variables are closely related. You often need to set some status values for both variables. For details, see the setflags (...) method;

Requestlayout ()

The execution process of this method is very simple, because when the view tree is re-laid, it is always re-laid for all views, rather than re-painting, you can specify to draw only a small area;

At the code level, he only adds the force_layout identifier to the mprivateflags variable, and then requests mparent. requestlayout () layer by layer. For details, see:

Invalidate ()

The function of this method is to request the view tree to be re-painted. The view and its parent view are displayed in hierarchical order on the interface. The parent view is located under the Child view and is being drawn, first, draw the bottom-layer Root View, and then draw the child view it contains. If the child view is a viewgroup, continue to draw its child view, so that the iteration ends until there is no child view;

In the specific re-painting process, generally, all views are not repainted, but only those views that need to be drawn are drawn. How can we find the areas that need to be drawn? This is the function to be completed by the invalidate method!

The general idea is: when the view needs to be re-painted, the drawn ID will be added to the mprivateflags variable, and then the rectangle block to be re-painted will be determined based on all the view boundaries with the logo, the conversion between different coordinate systems is involved. For details, see:

The code execution process is as follows:

    1. After necessary status bits are set in view. invalidate (), it is executed to mparent. invalidatechild (..);
      There are two mparent cases: parent viewgroup and viewrootimpl;
    2. If viewgroup is used
      Invalidatechildinparent (...) Then call mparent. invalidatechildinparent (...);
    3. Finally, viewrootimpl is called.Invalidatechildinparent (...), Then run scheduletraversals ();
      Note: It will be judged in advance. MwilldrawsoonLocal variable value. If you have already executed mongomtraversals () traversal and re-painting, scheduletraversals () will not be called, and no asynchronous message will be sent for the re-painting, however, the status values set in the view are still valid, but they will only take effect the next time you re-paint them;

Scheduletraversals ()

This method will be called in multiple places, such as requestlayout ()/invalidate (), and we will often see continuous calls to these two methods, wouldn't it initiate two view tree traversal and re-painting requests? Actually, no, because a local variable is set in the scheduletraversals () method.MtraversalscheduledIf requestlayout () is executed firstMtraversalscheduledIf this parameter is set to false, an asynchronous message request is initiated to redraw andMtraversalscheduledSet the variable value to true, and then judge when invalidate () is called.MtraversalscheduledThe variable value is no longer false, so that only one asynchronous re-painting request is initiated. See:

Extends mtraversals ()

The core method of this method is to traverse the view tree in the system and redraw the page. The internal logic is still very complicated, with about 800 lines of code. To be honest, I have not fully understood the details yet, there are too many associated variables involved in the middle, but the general subject process is clear, that is, determine whether to recalculate the View Size (measure) based on the various State values set previously) whether to re-allocate the view location (also called layout) and whether to re-paint the view (draw). For details about the Framework process, see. For details about the specific process of each item, see the following description:

If the above content is reprinted, please indicate the source. Welcome to visitLaotang's column http://blog.csdn.net/sfdev

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.