Look at the source side to refer to other people's blogs, etc., do a study note.
To understand the drawing of a view, you first need to know the structure of the View tree: (Refer to http://blog.csdn.net/qinjuning/article/details/7226787)
The drawing flow for the entire View tree is expanded in the Performtraversals () function of the Viewroot.java class, and its drawing process is as follows:
We understand the drawing process from the most basic idea: first determine the size of all the view, then determine the location of each view according to the layout, and then draw!
Here we focus on the draw method of the specific drawing, whose drawing process is:
View.draw (Canvas) method:
/*** Manually render this view (and all of it children) to the given Canvas. * The view must has already do a full layout before this function is * called. When implementing a view, implement * {@link#onDraw (Android.graphics.Canvas)} instead of overriding this method. * If You don't need to override this method, call the superclass version. * * @paramCanvas the canvas to which, the View is rendered. * * Automatically renders a view (including all its child view) based on a given Canvas. Layout must be completed before calling the method. When you customize the view, the * should go to implement the OnDraw (canvas) method instead of the draw (canvas) method. If you do need to replicate this method, remember to call the parent class method first. */ Public voidDraw (canvas canvas) {/ *Draw Traversal performs several drawing steps which must be executed*In the appropriate order:* * 1. Draw the backgroundifneed* 2. If necessary, save the canvas ' layers to prepare for fading * 3. Draw View ' s content * 4. Draw Children (Dispatchdraw)* 5. If necessary, draw the fading edges and restore layers* 6. Draw Decorations (scrollbars forinstance)*///Step 1, draw the background, if needed if(!Dirtyopaque) {drawbackground (canvas); } //Skip Step 2 & 5 if possible (common case) Final intViewFlags =Mviewflags; if(!verticaledges &&!)horizontaledges) { //Step 3, draw the content if(!dirtyopaque) OnDraw (canvas); //Step 4, draw the childrenDispatchdraw (canvas); //Step 6, Draw decorations (scrollbars)ondrawscrollbars (canvas); if(Moverlay! =NULL&&!Moverlay.isempty ()) {Moverlay.getoverlayview (). Dispatchdraw (canvas); } //we ' re done ... return; } //Step 2, save the canvas ' layers ... //Step 3, draw the content if(!dirtyopaque) OnDraw (canvas); //Step 4, draw the childrenDispatchdraw (canvas); //Step 5, draw the fade effect and restore layers//Step 6, Draw decorations (scrollbars)ondrawscrollbars (canvas); }
Viewgroup.dispatchdraw () Method:
Dispatchdraw (canvas canvas) {...if((Flags & flag_run_animation)! = 0 && cananimate ()) {//Animation for handling Childview Final BooleanBuildcache =!ishardwareaccelerated (); for(inti = 0; i < Childrencount; i++) { FinalView Child =Children[i]; if((Child.mviewflags & visibility_mask) = = VISIBLE) {//draw only the Visible state of the layout, so you can increase efficiency by delaying loading FinalLayoutparams params =Child.getlayoutparams (); Attachlayoutanimationparameters (Child, params, I, childrencount);//adding animations for layout changesBindlayoutanimation (child);//binding animations for child if(Cache) {child.setdrawingcacheenabled (true); if(Buildcache) {Child.builddrawingcache (true); } } } } FinalLayoutanimationcontroller Controller =Mlayoutanimationcontroller; if(Controller.willoverlap ()) {Mgroupflags|=flag_optimize_invalidate; } controller.start ();//animation to start the View} //Draw Childview for(inti = 0; i < Childrencount; i++) { intChildindex = Customorder?Getchilddrawingorder (Childrencount, i): i; FinalView Child = (Preorderedlist = =NULL) ?Children[childindex]: Preorderedlist.get (Childindex); if((Child.mviewflags & visibility_mask) = = VISIBLE | | child.getanimation ()! =NULL) { more|=drawchild (canvas, Child, drawingtime); } }...}protected BooleanDrawchild (canvas canvas, View child,Longdrawingtime) { returnChild.draw (Canvas, This, drawingtime);}/*** This method was called by Viewgroup.drawchild () to has each of the child view draw itself. * This draw () method is a implementation detail and is not intended to being overridden or * to being called from anywhere else other than Viewgroup.drawchild (). */ BooleanDraw (canvas canvas, viewgroup parent,Longdrawingtime) { ... }
Resources:
http://codekk.com/blogs/detail/54cfab086c4761e5001b253f
http://blog.csdn.net/qinjuning/article/details/7110211
http://blog.csdn.net/xyz_lmn/article/details/20385049
Learning Notes-----The Android view drawing process