Android View from scratch process

Source: Internet
Author: User

When collecting knowledge about the Android view drawing process, it was found that the process was somewhat complicated and prepared for several days before it dared to lift the pen. Let's go straight to the topic below!
The view drawing process begins with the Performtraversals () method of Viewroot, where the trilogy of view drawing is executed, that is: measure (the size of the measured view), Layout (determines the position of the view) draw (the contents of the drawing view). The following diagram clearly shows the process:

1, the process of measure
Public final void measure (int widthmeasurespec, int heightmeasurespec) {      ...    Onmeasure (Widthmeasurespec, heightmeasurespec);      ...}  
You can see that the method is final, so there is no need to rewrite the subclass, the implementation is basically called onmeasure. So what are the two parameters passed in? That involves Measurespec, Measurespec by Specmode (specification) and specsize (size) composition, specifications have three kinds, it corresponds to the size of the following:
1. Exactly
Indicates that the parent view wants the size of the child view to be determined by the value of Specsize, which by default sets the size of the child view, and the developer can of course set it to any size as it wishes.
2. At_most
Indicates that the child view can only be as large as the size specified in Specsize, and the developer should be as small as possible to set up this view and ensure that no more than specsize. The system defaults to the size of the child view, which, of course, can be set to any size as you wish.
3. UNSPECIFIED
Indicates that the developer can set the view to any size they wish, without any restrictions. This is a rare situation and is less likely to be used.
For the outermost root view, how are these two parameters determined? The original is called the Getrootmeasurespec, specifically implemented as follows:
private int Getrootmeasurespec (int windowsize, int rootdimension) {      int measurespec;      Switch (rootdimension) {case      ViewGroup.LayoutParams.MATCH_PARENT:          measurespec = Measurespec.makemeasurespec (WindowSize, measurespec.exactly);          break;      Case ViewGroup.LayoutParams.WRAP_CONTENT:          measurespec = Measurespec.makemeasurespec (windowsize, measurespec.at _most);          break;      Default:          Measurespec = Measurespec.makemeasurespec (rootdimension, measurespec.exactly);          break;      }      return measurespec;  }  
The parameters passed in by this function are window size and match_parent, which is why the root view is always full of screens.
Take a look at the onmeasure, the concrete implementation is as follows:
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {      setmeasureddimension (getdefaultsize ( Getsuggestedminimumwidth (), Widthmeasurespec),      getdefaultsize (Getsuggestedminimumheight (), HeightMeasureSpec ));  }  
Onmeasure The main use of setmeasureddimension to set the size of the view, so that the completion of a measure process, of course, a layout generally contains a number of sub-views, each sub-view needs to go through a measure process.
A Measurechildren () method is defined in ViewGroup to measure the size of the child view, as follows:
protected void Measurechildren (int widthmeasurespec, int heightmeasurespec) {      final int size = Mchildrencount;      Final view[] children = Mchildren;      for (int i = 0; i < size; ++i) {          final View child = Children[i];          if ((Child.mviewflags & visibility_mask)! = GONE) {              measurechild (child, Widthmeasurespec, Heightmeasurespec); c6/>}}}    
Inside the loop called the Measurechild, in fact, is now:
protected void Measurechild (View child, int parentwidthmeasurespec,      int parentheightmeasurespec) {         ...       child.measure (Childwidthmeasurespec, Childheightmeasurespec);  }
This is called the measure method of the view, so this is actually a recursive call, constantly to measure the size of the sub-view, until the full test.
2. Layout process
public void layout (int l, int t, int r, int b) {    ...    Setframe (L, T, R, b);     ...     OnLayout (changed, L, T, R, b);    ...}
The main call is Setframe (used to set the coordinates) and the OnLayout method, the view inside the onlayout is an empty implementation, because the onlayout () process is to determine the position of the view in the layout, and this operation should be done by the layout, That is, the parent view determines where the child view is displayed. The ViewGroup inside is an abstract method, which requires its subclasses to be implemented.
Take LinearLayout as an example and look at this process:
@Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {if (morientation = = VERTICAL) {layoutvertic Al (L, T, R, b);} else {layouthorizontal (L, T, R, b);}} void layoutvertical (int left, int top, int right, int bottom) {... for (int i = 0; i < count; i++) {final View child = g  Etvirtualchildat (i); if (child = = null) {Childtop + = Measurenullchild (i);} else if (child.getvisibility ()! = GONE) {//final int childwidth = Child.getmeasuredwidth (), final int childheight = Child.getmeasuredheight (), .... Setchildframe (Child, Childleft, Childtop + getlocationoffset (child), Childwidth, childheight);}}} private void Setchildframe (View child, int left, int top, int width, int height) {child.layout (left, top, left + width, to p + height);}
You can see that you are actually traversing the child view and then calling layout again, so that it loops through all the child view. Because the view procedure calls the Setframe method, you can set the location of the views, which coincide with the function of measure, so the words set here may invalidate the previous measure calculation.
3. Draw Process
public void Draw (canvas canvas) {...//Step 1, draw the background, if needed   int savecount; if (!dirtyopaque) {fin Al drawable background = mbackground;  if (background! = NULL) {final int scrollx = mscrollx;final int scrolly = mscrolly;if (mbackgroundsizechanged) {background . SetBounds (0, 0,  Mright-mleft, mbottom-mtop); mbackgroundsizechanged = false;} if ((scrollx | scrolly) = = 0) {  ckground.draw (canvas);  } else {canvas.translate (scrollx, scrolly); Background.draw (canvas);  Canvas.translate (-SCROLLX,-scrolly); }}}...//Step 3, draw the content  if (!dirtyopaque) OnDraw (canvas);  ...//Step 4, Draw the children  Dispatchdraw (canvas);  ...//Step 6, Draw decorations (scrollbars)  ondrawscrollbars (canvas);  return;  }
The most important of these is the invocation of the OnDraw and Dispatchdraw methods. OnDraw is an empty method that needs to be implemented by the Sub view itself, while the ViewGroup Dispatchdraw () method is mainly to traverse the child view, then call the Drawchild method, and Drawchild is called the Draw method, This in turn constitutes a circular call.
To summarize:1, the three processes are from the top down, from the parent to the child, that is: first set the parent view, and then traverse the child view, and set it. 2, custom view, we can rewrite onmeasure (non-mandatory) and OnDraw method, in the implementation of Onmeasure call setmeasureddimension or super.onmeasure to set the view size. 3, Custom viewgroup, we can rewrite the onlayout (must) method, inside the call view layout method to set the location of the view.

Android View from scratch process

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.