Android-view's Drawing Source Learning summary

Source: Internet
Author: User

# #前言

Is the first official GitHub blog, reviewed the previous view of the source code analysis, to do a view so far small summary of learning.

I think the source of the analysis and learning, all the process to write down the meaning is not very large, the most important is:

1. Know the basic role and usage
2. Understand the entire process and implementation approach
3. Find out where it can be expanded and use it more flexibly
4. The whole source design and details are there any highlights worthy of reference and learning
5. The idea of source code design

This is also the purpose of writing this article.

# #加载布局

# # #LayoutInflater

Http://www.cnblogs.com/qlky/p/5674975.html

-* * Function * *

Layoutinflater is the simplest code for loading layouts:

```
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.mainlayout);
}
```

The Setcontentview is implemented with Layoutinflater.

-* * Usage * *

Can be used to dynamically load layouts
```
Layoutinflater Layoutinflater = Layoutinflater.from (this);
View buttonlayout = layoutinflater.inflate (r.layout.button_layout,null);
Mainlayout.addview (buttonlayout);
```

-* * Principle * *

Inflate is a pull to parse the XML format, which calls the Createviewfromtag () method, and the node name and parameters are passed in. Seeing this method name, we should be able to guess that it is used to create a view object from a node name.
Indeed, within the Createviewfromtag () method, the CreateView () method is called, and the instance of the view is created and returned using reflection.

For layout child elements, the rinflate () is used to loop the implementation.

-* * Expand * *

From the source can know

The third parameter of public View inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot) attachtoroot

1. If Root is null,attachtoroot will lose its function, setting any value is meaningless.

2. If root is not set to true for Null,attachtoroot, a parent layout, root, is assigned to the loaded layout file.

3. If root is not set to False for Null,attachtoroot, all layout properties of the outermost layer of the layouts file will be set and the layout properties will automatically take effect when the view is added to the parent view.

4. If the Attachtoroot parameter is not set, the default is true if root is not the Null,attachtoroot parameter.


All controls, such as Layout_width, have a parent layout to take effect because this is used to set the size of the view in the layout, not the size of the view, so called layout_width, not width.

LinearLayout's layout_width is effective because, in the Setcontentview () method, Android automatically nests a framelayout,id as content in the outermost layer of the layout file

So called Setcontentview (), in fact, this method is also implemented with Layoutlnflater ()

# #视图绘制

Http://www.cnblogs.com/qlky/p/5676578.html
Http://www.jianshu.com/p/5a71014e7b1b

The view is drawn through three processes, measure,layout and draw


# # #Measure

-* * Function * *

Measure the size of a view and its sub-view


-* * Principle * *

Measurespec: The layoutparams of the Measurespec and child view of the parent view gives a simple calculation of the measurement requirements for the sub-view

The principle of calculation is simple:
If we write the value in the XML layout_width or layout_height, then the above measurement is completely unnecessary, the reason for this step is to measure, because Match_parent is full of the parent container, wrap_content Is how big they are, we write code when particularly cool, we encode convenient time, Google will help us calculate your match_parent time is how big, wrap_content is how big, this calculation process, Is the calculation of the parent view of the Measurespec constantly to the child view pass, combined with sub-view layoutparams together to calculate the sub-view Measurespec, and then continue to pass to the child view, Constantly calculating the measurespec of each view, the child view has measurespec to measure itself and its own sub-view.


Several cases:
1. Parent View Measurespec = exactly size ok, child view:
match_parent:exactly
Warp_content:at most
Determining values

2. Parent View Measurespec = exactly size is indeterminate, child view:
Match_parent:at most
Warp_content:at most
Determining values


The measurement of view is mainly in the Onmeasure method.

For view default measurement is very simple, most of the case is calculated measurespec size as the final measurement. And for some other view derived classes, such as TextView, Button, ImageView, and so on, their Onmeasure method system has been rewritten, it is not so simple to take the size of Measurespec directly, And go to the first to measure the height of characters or pictures, and then get the view itself content this height (character height, etc.), if Measurespec is At_most, and the view itself content height does not exceed measurespec size, Then you can directly use the height of the content of the view itself (character height, etc.), instead of the size of the view as View.java directly with measurespec size.


The root of the view is Decorview, so where does the drawing of the view begin, and we know that each activity creates a Phonewindow object that is the interface of activity and the entire view system interaction, Each window corresponds to a view and a Viewrootimpl,window and view to connect through the Viewrootimpl, and for the activity, Viewrootimpl is the link between WindowManager and Decorview, and the drawn entry is initiated by Viewrootimpl's Performtraversals method to initiate the Measure,layout,draw process.

Specific process: decorview, Content---linearlayout/....
Get the measurespec of each layer and finally get the size at the bottom. And then go up to the size of the parent view


-* * Expand * *

Overrides for the Onmeasure method

# # #Layout

-* * Function * *

Determines the position of the view in the layout. The Viewroot performtraversals () method continues after the measure is finished and invokes the view's layout () method to perform this procedure, as follows:

```
Host.layout (0, 0, host.mmeasuredwidth, host.mmeasuredheight);
```

-* * Principle * *

The Setframe () method is called first to determine if the size of the view has changed, to see if it is necessary to redraw the current view, and to assign the four parameters passed here to Mleft, Mtop, Mright, and Mbottom, respectively.
Next, the OnLayout () method is called.
The OnLayout () method In view is an empty method because the onlayout () procedure is to determine where the view is located in the layout, and this action should be done by the layout, that is, the parent view determines where the child view is displayed.
The OnLayout () method in ViewGroup is an abstract method in which the subclass must overload the OnLayout function, and the purpose of overloading the onlayout is to arrange its children at the specific location of the parent view


-* * Expand * *

Overloaded OnLayout functions


# # #Draw

-* * Function * *
Draw View


-* * Principle * *

Divided into six steps, of which the second and fifth steps are seldom used under normal circumstances

First step: Background drawing

The third step is to draw the contents of the view
OnDraw (Canvas) method is the view used to draw their own, how to draw, the color line what style needs sub-View to achieve their own, View.java OnDraw (canvas) is an empty implementation, ViewGroup is not implemented, The content of each view is different, so you need to implement specific logic by subclasses.

The fourth step draws all the child view of the current view
The Dispatchdraw (canvas) method is used to draw a sub-view, and the View.java Dispatchdraw () method is an empty method because the view has no child view and does not need to implement the Dispatchdraw () method, ViewGroup is not the same, it implements the Dispatchdraw () method, which is to traverse the child view and then Drawchild (), the Drawchild () method actually calls the sub-View.draw () method, The ViewGroup class has already implemented the default procedure for us to draw sub-view, which basically satisfies most requirements, so the subclass of the ViewGroup Class (Linearlayout,framelayout) is basically not overriding the Dispatchdraw method. We're implementing a custom control that doesn't have to be rewritten unless it's very special.

The 6th step is to draw the view scroll bar


-* * Expand * *

Overriding the OnDraw method

Android-view's Drawing Source Learning summary

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.