<span id="Label3"></p><span style="font-size:18px"><span style="font-size:18px">Create Window</span></span><p><p><span style="font-size:18px">In the Activity's attach method, a window is created by calling policymanager.makenewwindo, and when a view is add to windowmanager, Windowmanagerimpl creates a viewroot to manage the root view of the WINDOW. and pass the view to viewroot through the Viewroot.setview method.</span></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_1_8250970" name="code" class="java"><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_1_8250970" name="code" class="java"> final void attach (context context, activitythread athread, instrumentation instr, ibinder token, int ident, Application application, Intent Intent, activityinfo info, charsequence title, Activity parent, String id, Nonconfigurationinstances lastnonconfigurationinstances, Configuration config) { attachbasecontext ( context); Mfragments.attachactivity (this, mcontainer, null); Mwindow = Policymanager.makenewwindow (this); Mwindow.setcallback (this); Mwindow.getlayoutinflater (). setprivatefactory (this);</pre></pre><p><p></p></p><span style="font-size:18px"><span style="font-size:18px">Create <span style="font-size:18px">Decorview</span> </span></span><p><p><span style="font-size:18px">The Decorview is the topmost view of the entire window Interface.<br></span></p></p><p><p><span style="font-size:18px"><span style="font-size:18px">The Window object <span style="font-size:18px"></span> in activity helped us create a top-level view of the Phonewindow inner class Decorview (the parent class is framelayout). The XML content layout is then parsed into the view tree structure by Layoutinflater and added to the Decorview top-level view of the Framelayout parent container with the ID Content. The content layout of the activity is eventually added to the top-level view of the Decorview window.</span><br></span></p></p><p><p><span style="font-size:18px"><span style="font-size:18px"></span></span></p></p><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_2_9221298" name="code" class="java"><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_2_9221298" name="code" class="java"> Protected Boolean Initializepaneldecor (panelfeaturestate st) { st.decorview = new Decorview (getcontext (), st.featureid); st.gravity = Gravity.center | gravity.bottom; St.setstyle (getcontext ()); return true; }</pre></pre><p><p></p></p><span style="font-size:18px"><span style="font-size:18px">Create <span style="font-size:18px">Viewroot and associate View</span> </span></span><p><p><span style="font-size:18px"><span style="font-size:18px">Windowmanagerimpl Save Decorview to mviews, create corresponding viewroot;<br></span></span></p></p><p><p><span style="font-size:18px">The viewroot is used to manage the root view of the window and interact with the global window Manger. Viewroot has a nested class:w,w is a binder subclass that is used to receive various messages from the global window manager, such as keystroke messages, touch messages, and so On. Viewroot has a member of the W type Mwindow,viewroot creates a W instance in constructor and assigns a value to MWINDOW. Viewroot is a subclass of handler, and W passes the message to Viewroot through Looper. Viewroot the Mwindow to swindowsession in the Setview method.<br></span></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_3_2808566" name="code" class="java"> public void AddView (view view, viewgroup.layoutparams params, display display, Window Parentwindow) { if (view = = Null) {throw new IllegalArgumentException ("view must not is null"); } if (display = = Null) {throw new IllegalArgumentException ("display must not is null"); } If (! ( params instanceof Windowmanager.layoutparams) {throw new illegalargumentexception ("params must be Windowmanag Er. Layoutparams "); } final Windowmanager.layoutparams wparams = (windowmanager.layoutparams) params; If (parentwindow! = Null) {parentwindow.adjustlayoutparamsforsubwindow (wparams); } Viewrootimpl root; View Panelparentview = null; Synchronized (mLock) {//Start watching for system property Changes. if (msystempropertyupdater = = Null) {msystempropertyupdater = new Runnable () {@Overrid E public void Run (){synchronized (mLock) {for (viewrootimpl viewroot:mroots) { Viewroot.loadsystemproperties (); } } } }; Systemproperties.addchangecallback (msystempropertyupdater); } int index = findviewlocked (view, false); If (index >= 0) {throw New illegalstateexception ("view" + view + "has already Been added to the window Manager. "); If This is a panel window and then find the window it's being//attached to for future reference. If (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW && wparams.type <= Windo WManager.LayoutParams.LAST_SUB_WINDOW) {final int count = mviews! = null? mviews.length:0; For (int i=0; i<count; I+ +) {if (mroots[i].mwindow.asbinder () = = Wparams.token) {panelparentview = mVi ews[i]; }}} root = new Viewrootimpl (view.getcontext (), display); 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; mparams[index] = wparams; }//do the last because it fires off messages to start doing things try {root.setview (view, wp arams, panelparentview); } catch (runtimeexception E) {//badtokenexception or invaliddisplayexception, clean up. Synchronized (mLock) {final int index = findviewlocked (view, false); If (index >= 0) {removeviewlocked (index, true); }} Throw e; } }</pre><br><br><p><p></p></p><p><p><span style="font-size:18px">Viewroot is a bridge between GUI management system and GUI rendering system, It is not a view type to be aware Of.<br>Its main function is as Follows:<br>1, to the Decorview distribution received user-initiated event events, such as keys, touch screen, trackball and other events;<br>2. Interact with Windowmanagerservice to finish drawing the entire activity Gui.<br></span></p></p><span style="font-size:18px"><span style="font-size:18px">View drawing Basic Flow</span></span><p><p><span style="font-size:18px">Here is the drawing flow of the Android system View: execute the following three methods in the view Class:<br>Measure (int, int): measure the size of the view<br>Layout (int, int, int, int): Sets the location of the child view<br>Draw (canvas): Draw the view content onto the canvas canvas<br></span></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px">The entire view Tree's drawing flow is expanded in the Performtraversals () function of the Viewroot.java class, and the execution of the function can be simply summarized to determine if the view size (measure) needs to be recalculated based on the state previously Set. Whether to reposition the view (layout), and whether to redraw (draw)<br></span></p></p><span style="font-size:18px"><span style="font-size:18px">mesarue () Measurement Process</span></span><p><p><span style="font-size:18px">Main function: calculate the actual size for the entire view tree, that is, set the actual height (mmeasuredheight) and width (mmeasurewidth), The actual width of each view control is determined by the parent view and its own VIEW.<br>The specific invocation is as Follows:</span></p></p><p><p><span style="font-size:18px"><span style="font-size:18px">Viewrootimpl the performtraversals method, call measurehierarchy, and then call Performmeasure</span></span></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_4_1587067" name="code" class="java"><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_4_1587067" name="code" class="java">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); } }</pre></pre>Viewroot the root object property Mview (whose type is generally viewgroup) calls the measure () method to calculate the size of the view tree, callback<br>The Onmeasure () method of the View/viewgroup object, which implements the following functions:<br>1, set the final size of this view, the implementation of this function by calling the Setmeasureddimension () method to set the actual high<span style="font-size:18px"><span style="font-size:18px">(mmeasuredheight)</span></span>and width (mmeasurewidth)<p><p></p></p><p><p><span style="font-size:18px">2. If the View object is a viewgroup type, you need to override the Onmeasure () method to traverse the measure () procedure on its child VIEW.<br>The measure () procedure for each child view is implemented by calling the Measurechildwithmargins () method in the parent class Viewgroup.java class, which simply invokes the measure () method of the View Object.<br>The entire measure invocation process is a tree-shaped recursive process<br>The measure () method two parameters are passed by the parent view, which represents the specification of the parent VIEW. He consists of two parts, a high 2-bit representation of mode, defined in the Measurespec class (view's inner class), with three types, measurespec.exactly for determining size, measurespec.at_most representing the maximum size, Measurespec.unspecified not Sure. A low 30-bit represents the size of the parent View. For the System window class, the Decorview object mode is generally measurespec.exactly, and the size corresponds to the screen width height respectively. For child view, the size is determined by the parent view and the child view Together.</span></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_5_7890332" name="code" class="java"><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_5_7890332" name="code" class="java"> protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {setmeasureddimension (getdefaultsize (getSuggestedMinim Umwidth (), widthmeasurespec), getdefaultsize (getsuggestedminimumheight (), heightmeasurespec)); }</pre></pre><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_6_5589038" name="code" class="java"><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_6_5589038" name="code" class="java"> Protected final void setmeasureddimension (int measuredwidth, int measuredheight) { Boolean optical = Islayoutmodeoptical (this); If (optical! = islayoutmodeoptical (mparent)) { Insets Insets = getopticalinsets (); int Opticalwidth = insets.left + insets.right; int opticalheight = insets.top + insets.bottom; Measuredwidth + = optical Opticalwidth :-opticalwidth; Measuredheight + = optical? opticalheight:-opticalheight; } Mmeasuredwidth = measuredwidth; Mmeasuredheight = measuredheight; Mprivateflags |= pflag_measured_dimension_set; }</pre></pre><p><p></p></p><span style="font-size:18px"><span style="font-size:18px">Layout Process</span></span><span style="font-size:18px"><span style="font-size:18px">main function: to place the view tree in the appropriate position based on the size of the child view and layout Parameters.<br></span></span><span style="font-size:18px"><span style="font-size:18px"><span style="font-size:18px">The specific invocation is as Follows:</span><br></span></span><span style="font-size:18px"><span style="font-size:18px"> <span style="font-size:18px">Viewrootimpl in the performtraversals method, call PerformLayout<br></span></span></span><span style="font-size:18px"><span style="font-size:18px"><span style="font-size:18px"></span></span></span><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_7_8827641" name="code" class="java"> private void performlayout (windowmanager.layoutparams lp, int desiredwindowwidth, int Desiredwindowheight) {mlayoutrequested = false; Mscrollmaychange = true; Minlayout = true; Final View host = mView; If (debug_orientation | | Debug_layout) {log.v (TAG, "laying out" + host + "to (" + host.getmeasuredwidth () + "," + Host.getmeasuredheight () + ")"); } trace.tracebegin (trace.trace_tag_view, "layout"); Try {host.layout (0, 0, host.getmeasuredwidth (), host.getmeasuredheight ()); Minlayout = false; int numviewsrequestinglayout = Mlayoutrequesters.size (); If (numviewsrequestinglayout > 0) {//requestlayout () was called during Layout. IF No Layout-request flags was set on the requesting, there is no problem. If Some requests is still pending, then we need to clear those flags and does A full request/measure/layout pass to handle this Situation. arraylist<view> validlayoutrequesters = getvalidlayoutrequesters (mlayoutrequesters, false); If (validlayoutrequesters! = Null) {//Set This flag to indicate the any further reque STS is happening during//the second pass, which may result in posting those requests to the next Frame instead mhandlinglayoutinlayoutrequest = true; Process Fresh layout requests, then measure and layout int numvalidrequests = Validlayoutrequesters. Size (); for (int i = 0; i < numvalidrequests; ++i) {final View view = Validlayoutrequesters.get (i); LOG.W ("view", "requestlayout () improperly called by" + view + "durin G layout:running Second layout pass "); View.requestlayout (); } measurehierarchy (host, lp, mview.getcontext (). getresources (), Desiredwindo wwidth, desiredwindowheight); Minlayout = true; Host.layout (0, 0, host.getmeasuredwidth (), host.getmeasuredheight ()); Mhandlinglayoutinlayoutrequest = false; Check the valid requests again, this time without checking/clearing the//layout flags, since Reque STS happening during the second pass get noop ' d validlayoutrequesters = getvalidlayoutrequesters (mlayou trequesters, true); If (validlayoutrequesters! = Null) {final arraylist<view> finalrequesters = Validlayoutreque sters; Post Second-pass requests to the next frame Getrunqueue (). post (new Runnable () { @Override public void Run () {int numvalidrequests = finalrequesters.size (); for (int i = 0; i < numvalidrequests; ++i) {final Vie W view = Finalrequesters.get (i); LOG.W ("view", "requestlayout () improperly called by" + view + "during second Layout pass:posting in Next frame "); View.requestlayout (); } } }); }}}} finally {trace.traceend (trace.trace_tag_view); } minlayout = false; }</pre>Host.layout () begins the layout of the view tree and then callbacks to the layouts () method in the View/viewgroup class. The specific process is as follows<br>1. The layout method sets the view View's axis in the parent view, which is Mleft,mtop,mleft,mbottom (called the setframe () function to implement), and then callbacks the OnLayout () method ( If the view is a viewgroup object, you need to implement the method to lay out each child view.<br>2, If the view is a viewgroup type, you need to traverse each child view chiildview, call the layout () method of the child view to set its coordinate Value.<span style="font-size:18px"><span style="font-size:18px"><span style="font-size:18px"></span></span></span><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_8_2622652" name="code" class="java"><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_8_2622652" name="code" class="java"> protected void onlayout (boolean changed, int left, int top, int. right, int Bottom) { }</pre></pre><pre code_snippet_id="1753786" snippet_file_name="blog_20160709_9_7846122" name="code" class="java"> public void layout (int l, int t, int r, int b) {int OLDL = mleft; int Oldt = mtop; int oldb = mbottom; int OLDR = mright; Boolean changed = Islayoutmodeoptical (mparent)? Setopticalframe (l, t, r, b): setframe (l, t, r, b); If (changed | | (mprivateflags & pflag_layout_required) = = Pflag_layout_required) {onlayout (changed, l, t, r, b); Mprivateflags &= ~pflag_layout_required; Listenerinfo Li = mlistenerinfo; If (li! = null && li.monlayoutchangelisteners! = Null) {arraylist<onlayoutchangelistener> li Stenerscopy = (arraylist<onlayoutchangelistener>) Li.mOnLayoutChangeListeners.clone (); int numlisteners = Listenerscopy.size (); for (int i = 0; i < numlisteners; ++i) {listenerscopy.get (i). onlayoutchange (this, l, t, r, b, oldl, oldt, oldr, oldb); } }} mprivateflags &= ~pflag_force_layout; }</pre><span style="font-size:18px"><span style="font-size:18px"><span style="font-size:18px"><span style="font-family:‘Microsoft YaHei‘; font-size:16px; line-height:26px">draw () Drawing process</span></span></span></span><span style="font-size:18px"><span style="font-size:18px"><span style="font-size:18px">in the Performtraversals method of viewrootimpl, The draw method of Mview is called</span></span></span><span style="font-size:18px"><span style="font-size:18px"> <span style="font-size:18px">mview.draw () start drawing, The Draw () method implements the following functions:<br>1. Draw the background of the view<br>2. Do some preparation for displaying the gradient box<br>3, Call the OnDraw () method to draw the view itself (each view needs to overload the method, ViewGroup do not need to implement the METHOD)<br>4. Call the Dispatchdraw () method to draw the child view (if The view type is not viewgroup, that is, it does not contain a child view, you do not need to overload the METHOD)<br>It is worth noting that the ViewGroup class has rewritten the functionality of Dispatchdraw (), and the application generally does not need to override the method, but can overload the parent function to implement the specific Function.<br>The Dispatchdraw () method iterates through each child view and calls Drawchild () to recall the draw () method of each child VIEW.<br>5. Draw the scroll bar<br></span></span></span><span style="font-size:18px"><span style="font-size:18px"><span style="font-size:18px">Refresh View</span></span></span><span style="font-size:18px"><span style="font-size:18px"><span style="font-size:18px">There are two ways to implement view updates in android, One is invalidate and the other is postinvalidate, where the former is used in the UI thread itself and the latter in a Non-ui thread.<br></span></span></span><span style="font-size:18px"><span style="font-size:18px"> <span style="font-size:18px">requestlayout () method: causes the call to the measure () procedure and the layout () Procedure.<br>Description: simply re-layout The view tree includes the measure () and layout () procedures, does not call the draw () procedure, but does not redraw<br>Any view includes the caller itself.<br></span></span></span><p><p></p></p><p><p><span style="font-size:18px">The function that normally causes the invalidate () operation is as Follows:<br>1. Call the Invalidate () method directly, request a redraw (), but only draw the caller itself.<br>2. setselection () method: request Redraw (), But only the caller itself is Drawn.<br>3. setvisibility () Method: when the view visual state transitions to the visible invisible, the invalidate () method is called indirectly and the view is Drawn.<br>4. setenabled () method: requests a redraw (), but does not redraw any views including the caller ITSELF.<br><br></span></p></p><p><p><span style="font-size:18px">The content reference source code, draws on the network some Analysis.</span></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px">Welcome to scan QR code, follow public account</span></p></p><p><p><br></p></p><p><p> View drawing flow analysis in Android </p> </p></span>
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