View drawing process a little bit of experience

Source: Internet
Author: User

I am a rookie, I do not have that level of research on the drawing process of view, but after a variety of consulting predecessors Daniel's information or blog know that the drawing process of the view is divided into Onmeasure,onlayout,ondraw three important processes, Let's take it as a conclusion to guide your study of Android graphics, and take a lot less detours. The following is affixed to their own experience and experience, the wrong place to welcome criticism, to learn together.

Before opening the first talk about the relationship between view and ViewGroup, ViewGroup is the sub-class of view, but the same viewgroup also encapsulates many view references including the view collection of references, which can be seen from the source, From the design pattern is the typical combination pattern, so the relationship between them can be used as shown:

There can be other tree-shaped structures, of course.

View class:

1) The onmeasure of the view is called in the measure method, and the Onmeasure calls Setmeasuredimension (Intmeasurewidth,int measureheight) To complete the measure process. Onmeasure method to complete the specific measurement logic, it should be noted that measure is the final method, when you want to implement your own measurement logic in the subclass can not override the measure method, can only override the Onmeasure method to complete their own measurement logic

2) The OnLayout method is an empty method in the view class, which is done by subclasses

3) Same OnDraw method for empty methods, subclasses go time their own drawing logic

4) The View class also provides other relevant methods for drawing the view:

4.1) layout (int l,int t,int r,int b), the function of the method is to draw a view relative to the location of the parent view, the parameters can be represented as follows: Red, black, green, blue arrows are l,t,r,b

4.2) Dispatchdraw method, also an empty method, implemented by subclasses ViewGroup, is mainly used to draw the sub view of the current view, the programmer can also implement this method to customize the drawing of the relevant view, The plot of the GridView on GitHub is a rewrite of this square method for Headerview drawing.

4.3) draw (canvas canvas, viewgroup parent, Long Drawingtime): As the note says, this method is called by the Drawchild method of the view's subclass ViewGroup, The function of this method is to let the child view draw itself, the method designed by itself does not want the quilt class to be rewritten, nor is it called anywhere outside of the Drawchild method.

4.4) The view class also provides draw's overloaded method draw (canvas canvas), which implements the logic for drawing the view, noting that it is best to rewrite the OnDraw () method instead of overriding the method when the user customizes the view. Of course, if you really want to rewrite this method, remember Super.draw (); The steps to draw the view are clearly written on the comment of the method, as follows:

1.Draw the background

2.If necessary, save the canvas ' layers to prepare for fading

3.Draw View ' s content

4.Draw Children

5.If necessary, draw the fading edges and restore layers

6.Draw Decorations (scrollbars for instance)

Note that the OnDraw method was called at step three to draw, and the Dispatchdraw method was called at Step 4.

Introduction to this place basically finished the view of the drawing process, but in the OnDraw method there are not often see the situation, the personal level is limited, it is not astray.

ViewGroup class:

1) ViewGroup does not override the Onmeasure method of the parent class, which means that all measurements are given to the parent Class View or ViewGroup subclass to dynamically bind the implementation. However, Measurechild, Measurechildwithmargins and Measurechildren methods are provided. According to the method name is not difficult to detect Measurechildren method inside loop through each view and then call the Measechild method:

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/>}}}    

The Measurechild method eventually executes the measure method of the view class


protected void Measurechild (View child, int parentwidthmeasurespec,            int parentheightmeasurespec) {        final Layoutparams LP = Child.getlayoutparams ();        Final int childwidthmeasurespec = Getchildmeasurespec (Parentwidthmeasurespec,                mpaddingleft + mpaddingright, Lp.width);        Final int childheightmeasurespec = Getchildmeasurespec (Parentheightmeasurespec,                mpaddingtop + MPaddingBottom, Lp.height);        Child.measure (Childwidthmeasurespec, Childheightmeasurespec);    }

2) In this class, the OnLayout method of the parent class is changed to the abstract method, which is given to the ViewGroup subclass to realize. and re-wrote the layout method.

3) as mentioned in 4.3 above, this class provides a Drawchild method that invokes the method of the parent class.

Protected Boolean  drawchild (canvas canvas, View child, Long Drawingtime) {        return Child.draw (canvas, this, Drawingtime); },

This method allows each sub-view to be called in the Dispatchdraw method.

4) This class implements the Dispatchdraw method, loops through each of the sub-view, draws each view in the calling Drawchild party. So call Dispatchdraw to draw the calling method in the order of:dispatchdraw--> Drawchild-->draw

if ((Flags & flag_use_child_drawing_order) = = 0) {for            (int i = 0; i < count; i++) {                final View child = Chi Ldren[i];                if (child.mviewflags & visibility_mask) = = VISIBLE | | child.getanimation ()! = null) {More                    |= drawchild (canvas, C Hild, Drawingtime);}}}        else {            for (int i = 0; i < count; i++) {                final View child = Children[getchilddrawingorder (count, i)];                if (child.mviewflags & visibility_mask) = = VISIBLE | | child.getanimation () = null) {More                    |= drawchild (canvas, Child, Drawingtime);}}}        

So from the above analysis, when you customize the view to implement your own drawing logic can be based on the need to determine whether your view class is inherited ViewGroup or inherit view, in fact, most of the Android source view is directly or indirectly inherited from the ViewGroup , such as Linearlayout,framelayout,relativelayout, and so on, there are components directly inherited from the view, such as Textview,imageview. So the UML diagram of the method involved in the drawing process involves a simple summary of the following



After finishing the review, in addition to a few pictures, basically in the interpretation of the role of the method or to write a comment on the relevant methods, but in the process of blogging through their own browsing and analysis of the source code, really harvest is not small, I hope to help readers, in addition to the improper place also please enlighten


View drawing process a little bit of experience

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.