The process of drawing the Android view to the window

Source: Internet
Author: User

previously in the study of custom space, viewed the process of view and ViewGroup drawing. It is only qualitative to know that these processes will go through Onmeasure-onlayout-ondraw. the process of loading a view into a window was analyzed in the process analysis of the previous Android view loaded into the window . is mainly a series of addview operations, this article from the AddView method to see how the view is drawn to the window.

Activitythread#handleresumeactivity method

final void Handleresumeactivity (IBinder token, Boolean clearhide, Boolean Isforward, Boolean Reallyresume) {... Activityclientrecord r = performresumeactivity (token, clearhide), ... if (R.window = = null &&!a.mfinished && Amp                willbevisible) {R.window = R.activity.getwindow (); View decor = R.window.getdecorview ();//Previous analysis, this is the window object maintained by the decoration of Windows, the topmost window decor.setvisibility (view.invisible)                ; Viewmanager wm = A.getwindowmanager ();//Get WindowManager, inherit from Viewmanager, not instantiated, is an interface Windowmanager.layoutparam                s 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);//Add to window by WindowManager} ...} 

You can see that the topmost decorative window is added by the Windowmanager#addview method during activity resume.

windowmanagerimpl# AddView

public void AddView (view view, Viewgroup.layoutparams params) {Mglobal.addview (view, params, Mdisplay, Mparentwind    ow);} public void AddView (view view, Viewgroup.layoutparams params, display display, Window parentwindow) {Vi        Ewrootimpl Root; View Panelparentview = null, ... root = new Viewrootimpl (View.getcontext (), display);            Create a Viewroot object View.setlayoutparams (wparams);                if (mviews = = null) {index = 1;                Mviews = new View[1];                Mroots = new Viewrootimpl[1];            Mparams = new Windowmanager.layoutparams[1];                } else {index = mviews.length + 1;                object[] old = mviews;                 Mviews = new View[index];                System.arraycopy (old, 0, mviews, 0, index-1);                Old = Mroots;                Mroots = new Viewrootimpl[index];                System.arraycopy (old, 0, mroots, 0, index-1); Old =Mparams;                Mparams = new Windowmanager.layoutparams[index];            System.arraycopy (old, 0, mparams, 0, index-1);            } index--;            Mviews[index] = view; Mroots[index] = root;//associating view with Viewrootimp Viewrootimp is a bridge linking view and windowmanagerservice mparams[index] = Wpar        AMs try {root.setview (view, Wparams, Panelparentview);//Call Viewroot Setview method} ...}

Setview:

public void Setview (view view, Windowmanager.layoutparams attrs, view panelparentview) {  requestlayout ();// Request UI start drawing redraw view tree        ... try {           morigwindowtype = Mwindowattributes.type;           Mattachinfo.mrecomputeglobalattributes = true;           Collectviewattributes ();//notifies windowmanagerservice that adding a window  will call to AddWindow method           res = Mwindowsession.addtodisplay (Mwindow, MSeq, Mwindowattributes,                     gethostvisibility (), Mdisplay.getdisplayid (),                     Mattachinfo.mcontentinsets, Minputchannel);}        }

Both cases result in calls to Requestlayout, which change the view display properties, such as Setvisibility, which is called directly or indirectly by the function.

@Override public void Requestlayout () {checkthread ();//Whether this call is called on the UI thread.        Mlayoutrequested = true;    Scheduletraversals ();            } void Scheduletraversals () {if (!mtraversalscheduled) {mtraversalscheduled = true;            Mtraversalbarrier = Mhandler.getlooper (). Postsyncbarrier ();//distributes an asynchronous message, and the handler calls Performtraversals () to re-traverse the view. Mchoreographer.postcallback (choreographer.callback_traversal, mtraversalrunnable, NULL);//MTraversalR        Unnable is a Runnable object Scheduleconsumebatchedinput ();    }}final traversalrunnable mtraversalrunnable = new traversalrunnable (); Final class Traversalrunnable implements Runnable {@Override public void run () {dotraversal ();            /execute Dotraversal ()}}void dotraversal () {if (mtraversalscheduled) {mtraversalscheduled = false;            Mhandler.getlooper (). Removesyncbarrier (Mtraversalbarrier), .... try {    Performtraversals ();            } finally {trace.traceend (Trace.trace_tag_view); }...... }}

The core function of view tree traversal Measure-layout--draw

private void Performtraversals () {...    Ask host How big it wants to be    performmeasure (Childwidthmeasurespec, Childheightmeasurespec); PerformLayout (), ... performdraw ();     ....... Misintraversal = false;}

private void Performmeasure (int childwidthmeasurespec, int childheightmeasurespec) {        Trace.tracebegin ( Trace.trace_tag_view, "measure");        try {            mview.measure (Childwidthmeasurespec, Childheightmeasurespec);        } finally {            trace.traceend ( Trace.trace_tag_view);        }    }

Mview.measure (CHILDWIDTHMEASURESPEC,CHILDHEIGHTMEASURESPEC); executed to the familiar onmeasure(Childwidthmeasurespec, CHILDHEIGHTMEASURESPEC); inside.

The process of drawing the Android view to the window

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.