The view drawing process for Android GUI

Source: Internet
Author: User

In the previous article, we tracked the source code and we learned about the relationship between activity, Window, Decorview, and view (see article: Http://www.cnblogs.com/jerehedu/p/4607599.html#gui). So how did the entire activity interface be drawn? Since Decorview as the top-level interface view of activity, then the whole interface of the drawing work should start from it, the following we continue to follow the source code to see if this is the case.

The activity calls methods Performlaunchactivity and handleresumeactivity in the main thread activitythread during startup. In method Handleresumeactivity, the created Decorview and Windowmanagerimpl objects are associated, and the key source sections are as follows:

 Public Final classActivitythread {...Final voidhandleresumeactivity (IBinder token,BooleanClearhide,BooleanIsforward,BooleanReallyresume) {...if(R.window = =NULL&&!a.mfinished &&willbevisible) {R.window=R.activity.getwindow (); View Decor=R.window.getdecorview ();                Decor.setvisibility (view.invisible); Viewmanager WM=A.getwindowmanager (); Windowmanager.layoutparams L=r.window.getattributes (); A.mdecor=Decor; L.type=WindowManager.LayoutParams.TYPE_BASE_APPLICATION; L.softinputmode|=Forwardbit; if(a.mvisiblefromclient) {a.mwindowadded=true;                Wm.addview (decor, L); }} ...}

Windowmanagerimpl Key Code:

     Public Final class Implements WindowManager {privatefinal windowmanagerglobal Mglobal =  Windowmanagerglobal.getinstance (); @Overridepublicvoid  addview (view view, Viewgroup.layoutparams params) {      Mglobal.addview (view, params, mdisplay, Mparentwindow);} ...}

Windowmanagerglobal Key Code:

 Public Final class Windowmanagerglobal {    ...      Public void AddView (view view, Viewgroup.layoutparams params,            display display, Window parentwindow) {           ...        Viewrootimpl root;         NULL ;......         Root.setview (view, Wparams, Panelparentview);           ...    }

Depending on the source call relationship, you can get:

, we can see that the decorview generated in Activitythread is Windowmanagerimpl, Windowmanagerglobal, and finally calls the Viewrootimpl method in Setview, The Decorview setting is assigned to the Mview property in Viewrootimpl. By tracing Viewrootimpl We found that the Performtraversals method was eventually called, and the key code of the method is as follows:

Private void performtraversals () {        //  cache MView since it's used so much below ...        Final View host = MView;        ... Performmeasure (Childwidthmeasurespec, childheightmeasurespec);        ... PERFORMLAYOUT (LP, Desiredwindowwidth, desiredwindowheight);        ... Performdraw ();        ...}

From the above source code, we can see that performtraversals actually called three key methods in turn, namely, Performmeasure,performlayout, Performdraw.

1, the method performmeasure, the internal actually calls the Mview.measure (Childwidthmeasurespec, Childheightmeasurespec); Don't forget that the mview here is the decorview that we pass in to measure the size of the view. The key source code is as follows:

        Private void performmeasure (intint  childheightmeasurespec) {        "measure");         Try {            mview.measure (Childwidthmeasurespec, childheightmeasurespec);         finally {            trace.traceend (Trace.trace_tag_view);        }    }

2, Method PerformLayout, internal actually call Host.layout (0, 0, host.getmeasuredwidth (), Host.getmeasuredheight ()); This method is used to determine the position of the view. The key source code is as follows:

    Private voidPerformLayout (Windowmanager.layoutparams LP,intDesiredwindowwidth,intdesiredwindowheight) {mlayoutrequested=false; Mscrollmaychange=true; Minlayout=true; FinalView host =MView; ...Try{host.layout (0, 0, Host.getmeasuredwidth (), Host.getmeasuredheight ()); Minlayout=false; intNumviewsrequestinglayout =mlayoutrequesters.size (); if(Numviewsrequestinglayout > 0) {...if(Validlayoutrequesters! =NULL) {                    //Set This flag to indicate it any further requests is happening during... host.layout (0, 0, Host.getmeasuredwidth (), Host.getmeasuredheight ());                ...... } }            }        } finally{trace.traceend (Trace.trace_tag_view); } minlayout=false; }

3, Method Performdraw, used to draw the view, trace the source discovery, finally called the Mview.draw (canvas) method, for drawing.

Through the above process can basically determine the view of the drawing process, the flowchart is as follows:

Inquiries or technical exchanges, please join the official QQ Group: (452379712)

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

The view drawing process for Android GUI

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.