Android SDK document Introduction: How to draw views in Android

Source: Internet
Author: User

When an activity gets the focus, it is required to draw its layout (that is, the layout set by the setcontentview () method in the oncreate method ). The android Framework processes the painting process, but the activity must also provide the layout-level root node information of the view to be drawn.

PS: The last sentence must be explained here. We all know that a layout file is an XML file containing multiple levels. The XML node information in it represents one layout level after another-that is, the so-called "layout level root node ". With the characteristics of XML technology, we can ensure the uniqueness of each level in layout, we can easily locate the position of the view to be drawn on a layout. In addition, those who have learned the data structure should know that the node is a concept in the "Tree" of the data structure. Therefore, I guess that after the XML file information of layout is loaded into the memory, it actually exists in the form of a "tree.

The work of drawing a view starts from the layout root node of the view to be drawn. Before that, the framework will pre-measure and draw the layout "tree ". The layout "Tree" will be drawn in this way: traversing this "Tree ", then, reserve a known size for each view that occupies an unused area (this size is the information contained in the layout_width and layout_height attributes that we often use) for future view painting. Then, each viewgroup requests to draw their child views in sequence (through the draw method of viewgoroup ), when each view is drawn, its ondraw method is called to draw itself. Because layout "Tree" is traversed in order, this means that the parent view (for example, viewgroup) will be drawn before the child view.

TIPS: Framework does not draw views that are not in the unused area, and it will draw a background for the view. You can forcibly draw a view by using the invalidate () method.

PS: the steps for drawing a complete layout are roughly as follows: 1. Resolve the layout. xml file to the memory so that the layout information exists in a "Tree" format. 2. Framework traverses the "Tree" and draws the view from top to bottom. 3. When a view is drawn, it reserves space for the view to be drawn in the unused area, and then calls the draw method of the view to draw it.

Drawing a layout involves two major processes: one is the dimensional test process. In this process, the size of each view is measured from top to bottom in the layout "tree. The size measurement is performed by the View classMeasure (INT, INT)Method. In the tree traversal project, each view calls this method, and then saves its size information by reading some developer-defined information (layout_height layout_width. When the size test ends, each view has saved its size information. Another process is the specific drawing. Each parent view callsLayout (INT, Int, Int, INT) method, combined with the size information stored in the size measurement process of the child view, to place the child view in its own internal position.

When a view's measure method returns, its width and height -- and its sub-View -- attributes must have been set. The width and height of a view must comply with the size constraints of its parent view-this ensures that, at the end of the size test process, all parent views can smoothly accept their child views. A parent view may call the measure method multiple times on its child view. For example, a parent view may first call its sub-View's measure method to know how big they are, so as to reserve space for them, then the parent view will call Measure to test whether the size is suitable for the view.

TIPS: You can call requestlayout () to initialize a layout. This method is usually called by view when it finds that its current range is not suitable for its current situation.

Two classes are important in the dimension test. One is view. measurespec. It is used by view to tell its parent view how big it wants and how it is placed. The other is layoutparams, which we are already familiar with. It only describes the height and width of the view. You can specify the height and width as follows:

 

1. An exact pixel value

2. fill_parent, which means the height
Or the width is the same as the parent view (minus the pixel value of padding ).

3. wrap_content, which means that the height and width of the view are based on the content that exactly contains the view.

Each viewgroup has its own corresponding layoutparams sub-classes to achieve personalized settings. For example, relativelayout has its own layoutparams subclass to set the relative position of its child view.

Measurespecs is used to pass the requirements of the parent View to the Child view. It can be set to one of the following three modes:

  • UnspecifiedThe parent view has no special requirements on the size of the Child view.
  • ExactlyParent view needs to set the child view to a specific pixel value. The child view must be this value, and make sure that all its child views are drawn within this size.
  • At_mostTells the child view a minimum size value.

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.