Android Interface drawing process--------How Android draws views

Source: Internet
Author: User

In the learning of custom components, accidentally found the official website of this article, feel good, so try to translate it out. One is to share with you, the second is to deepen their impressions. The level is limited, the translation process has the wrong place, welcome correction.


The original address is: How Android draws views


When an activity is present in front of the user, its layout is drawn. The Android system will process the drawing, but only if the activity needs to provide the root node for its layout.


The drawing process starts at the root node of the layout and then measures and plots the entire layout tree structure, drawing along the layout tree structure, rendering each view in turn. During this traversal, when encountering ViewGroup, each viewgroup (note that the ViewGroup also belongs to the view) will draw the view in turn, and the view will draw itself when the view is encountered. Since the drawing process is traversed along the layout tree structure, the parent node in the layout tree structure will be drawn before the child node, and the sibling nodes are drawn in the order in which they appear.


It takes two pass process to draw a layout (pass-through?) ), measure pass and Layout pass (can be translated as a measurement transfer process, calculate the layout of the transfer process?) )。 The measure pass is performed in method measure (Int,int), and the measure pass recursively traverses the layout structure from top to bottom. In the recursive process, each view passes dimension specifications along the layout tree, and at the end of the measure pass, each view will have its own measurement results. The second pass process is the layout pass, which is carried out in layout (Int,int,int,int) and is top-down (Top-down), as with the measure pass, in the layout pass process, Each parent node calculates the location of each sub-node using the measure pass's measurement results.


When the measure () method of a View object finishes executing, the return value of the view and the Getmeasurewidth () and Getmeasureheight () of its child nodes must already be assigned a value. (In fact, these two methods return the variables Mmeasurewidth and mmeasureheigth in the View object respectively, the comments of the two variables are: Height/width as measured during measure pass). The measurement width and measurement height of each view must be in accordance with the constraints passed by the parent node (in fact, the dimension specifications mentioned above). This ensures that at the end of the measure pass, each parent node can accept the measurements of its various sub-nodes. A parent view may not only trigger the measure () method of its child nodes in order, for example, parents may first measure each sub-node without a specified size, measure how much space they need to occupy, and, if the first round of measurements, the results of each sub-node conflict with each other, The parent node then calls the Onmeasure () method of each child node again to measure each sub-node.


Measure pass uses two classes to pass values about the measured size. These two classes are viewgroup.layoutparams and Measurespec, respectively. The View object can use the Viewgroup.layoutparams object to tell its parent node what size it needs and where it is placed. The most basic Viewgroup.layoutparams class can only describe the height and width of the view. It can specify the height and width of the following three categories

    • An exact value.
    • Math_parent, which represents the view hope is as large as its parent node (minus padding).
    • Wrap_content, which represents the view hope size enough to surround it with content (plus padding)

Viewgroup.layoutparams has several subclasses that exist for several corresponding subclasses of ViewGroup. For example, Relativelayout has its own viewgroup.layoutparams subclass, which has a special function that can be used to set the midpoint of the sub-node in the horizontal direction, or the midpoint in the vertical direction.


The Measurespec object is an object that is passed to the child node by the parent node, and the constraint information for the parent node is stored in the object. The Measurespec object can have the following three modes:

    • UNSPECIFIED: There are no requirements for the size of the parent node's child nodes.
    • Exactly: The parent node requires that the size of its child nodes be specified as an exact value. Its child nodes and other descendants need to be adapted to that size.
    • At most: The parent node requires that its child nodes not be larger than a certain maximum, and that the size of its child nodes and other descendants must be less than this value


The article also mentions two tips:

    • Invalidate (): Call this method to force redrawing of the interface
    • Requestlayout (): When the current layout size changes, you can call the method to refresh the layout.



Android Interface drawing process--------How Android draws views

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.