Android ScrollView Scrolling mechanism
We all know that by View#scrollto (x, y) You can implement the view scrolling effect, and if you add the Scroller class again, you can roll to the effect. But how does this work? This problem involves the drawing mechanism of view. Let's first look at the basic flow of view drawing
(Image from the online more common view drawing flowchart)
A brief description of the three phases:
1. Measure: Pre-estimate the space occupied by each View of the viewtree .
OK viewtree each view width Span style= "font-family: the song Body;" >, height , left top right , bottom
3. Draw: Use One of the Surface.lockcanvas (Dirty) objects in Rootviewimpl as the canvas, and all the View on the Viewtree is drawing on this Canvas ,
It is worth noting that canvas through GetHeight () and GetWidth () is the true size of the entire screen. including the notification bar (although the print out of the viewtree can not see, but through the top property, left a little space to the notification bar), title bar, Content, the bottom of virtual keys and so on.
Let's take a look at Mscrollx/mscrolly's comments in the code:
mscrollx/mscrolly relative to this View the content (text, pictures, sub - View ) Vertical / the horizontal pixel offset. such as:
After setting mscrollx/mscrolly , you can scroll to the specified " content " and mscrollx/ Mscrolly is The offset from the content , the content origin is (0, 0 ).
And how does this content size and offset occur? In ViewGroup, there is an API Drawchild (), this function mainly completes the sub-view of the space size and offset, see the following description
Protected Boolean drawchild (canvas canvas, View child, Long Drawingtime) {Boolean more = false;//Gets the space size of the sub View final int cl = child.mleft;final int ct = child.mtop;final int cr = child.mright;final int cb = child.mbottom;//Notify Sub view to determine whether to complete scrolling, this is the pass The scroller code implements the key point of scrolling child.computescroll ();//Gets the latest offset final int sx = child.mscrollx;final int sy = child.mscrolly;// Create a restore point final int restoreto = Canvas.save ();//offset, through this API, to achieve the scroll to the content offset, first the content of the origin of the offset to the negative area canvas.translate (CL-SX, CT- SY);//shear, because there is a translate operation, all the cut out space is the parent view given the visible area//So if the child view fills the canvas content beyond the given space, it will not show up Canvas.cliprect (SX, SY, SX + (CR-CL), Sy + (CB-CT));//Allow child view to draw, note that the child view does not have to process the scroll property, can achieve content offset child.draw (canvas);//Restore Canvas.restoretocount ( Restoreto); return more;}
It is worth noting that the ListView is not implemented using this mechanism, but instead uses the replacement childview to achieve the sliding effect.
Android ScrollView scrolling mechanism