Android Layout Slide Explore ScrollTo and Scrollby methods using instructions

Source: Internet
Author: User

1 When it comes to sliding, it involves the view, and we all know that the Android UI is made up of a view and a derived class of view, view as the base class, and the layout of the common layout is the subclass of the viewgroup that it derives from. ViewGroup the overall UI as a container for each component. The following is an example of the Android UI structure:2 3 4 View Source5 [Java] View plain copy6 View code slices to my Code slice7 8     /** 9 * Implement this to do your drawing.Ten      *  One      * @paramCanvas the canvas on which the background 'll be drawn A      */   -     protected voidOnDraw (canvas canvas) { -     }   the  - can be found that the implementation of the view is generally done by drawing the OnDraw method, if you want to change its interface can be rewritten OnDraw to achieve your effect, in the source code, you cannot find -  - [Java] View plain copy + View code slices to my Code slice -  +      Public voidAddView (View child) { AAddView (Child,-1);  at         }   -  - this method, because it does not know, only in its derived class such as ViewGroup will have this way to add a sub-layout.  -  - ViewGroup, as a component container, can contain any component, but you must override his OnLayout () method and Onmeasure () to set the position of the container layout and draw its size to display properly.  -  inFirst, we have to understand that there is no boundary in Android view, there is no boundary for canvas, but we do some work on the canvas object by drawing a specific view, for example: translate (PAN), clipRect (cut), etc. To achieve our requirements for drawing the canvas object, we can refer to this borderless view as "view coordinates"-----It is not limited by the physical screen. Usually we understand that a layout file is only the display area of the view, more than the display area will not be displayed in the parent view of the area, corresponding to this, we can call this bounded view "layout coordinates"------the layout size that the parent view assigns to the child view. Also, the starting coordinates of a view on the screen are at the beginning of the view coordinates, as shown in.  -  to  +is actually the origin point relative to the upper-left coordinate of the parent Class View (0,0), instead of the overall viewgroup, the upper left corner is the origin point.  -  the because layout coordinates can only display a specific piece of content, we can display any location of the view coordinates by moving the coordinate origin of the layout coordinates.  *  $ (Note: For example, the layout of cocos2d is not the same as the coordinates of the original coordinates of the android layout coordinate, it is the origin in the lower left corner, so there are some differences.) )Panax Notoginseng  -  the here is the general view and ViewGroup, (many great gods on the internet to analyze this piece, here only to do a few excerpts of the record) is to elicit the protagonist of today Scrollto and Scrollby.  +  A  the View the following source code can be found: +  - [Java] View plain copy $ View code slices to my Code slice $  -<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >/**  - * The offset, in pixels, by which the content of this view is scrolled the * horizontally. - * {@hide}Wuyi          */   the@ViewDebug. Exportedproperty (category = "scrolling")   -         protected intMscrollx;  Wu         /**  - * The offset, in pixels, by which the content of this view is scrolled About * vertically. $ * {@hide} -          */   -@ViewDebug. Exportedproperty (category = "scrolling")   -         protected intmscrolly;  A         /**  + * Return The scrolled left position of this view. the left edge of the * The displayed part of your view. Need to draw any pixels - * Farther left, since those is outside of the frame of your view on $ * screen. the          *  the          * @returnthe left edge of the displayed part of your view, in pixels. the          */   the          Public Final intGetscrollx () { -             returnMscrollx;  in         }   the        the         /**  About * Return the scrolled top position of this view. The top edge of the * The displayed part of your view. Need to draw any pixels above the * It, since those is outside of the frame of your view on screen. the          *  +          * @returnthe top edge of the displayed part of your view, in pixels. -          */   the          Public Final intgetscrolly () {Bayi             returnmscrolly;  the}</span> the  -  - MSCROLLX: Represents the offset from the x horizontal direction from the start of the view the  the mscrolly: Represents the offset from the vertical y direction of the view starting position the  the obtained by GETSCROLLX () and getscrolly () method respectively.  -  the Note: Mscrollx and mscrolly refer not to coordinates, but to offsets.  the  the 94 [Java] View plain copy the View code slices to my Code slice the  the<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >/** 98 * Set The scrolled position of your view. This would cause a call to About          * {@link#onScrollChanged (int, int, int, int)} and the view would be - * invalidated.101          * @paramx The x position to scroll to102          * @paramy the y position to scroll103          */  104          Public voidScrollTo (intXinty) { the             if(MSCROLLX! = x | | mscrolly! =y) {106                 intOLDX =Mscrollx; 107                 intOldY =mscrolly; 108MSCROLLX =x; 109mscrolly =y;  the invalidateparentcaches (); 111 onscrollchanged (MSCROLLX, mscrolly, OLDX, OldY);  the                 if(!Awakenscrollbars ()) {  113 postinvalidateonanimation ();  the                 }   the             }   the         }  117       118         /** 119 * Move The scrolled position of your view. This would cause a call to -          * {@link#onScrollChanged (int, int, int, int)} and the view would be121 * invalidated.122          * @paramx The amount of pixels to scroll by horizontally123          * @paramy the amount of pixels to scroll by vertically124          */   the          Public voidScrollby (intXinty) {126ScrollTo (mscrollx + x, mscrolly +y); 127}</span> - 129  the from the above code can be seen, scrollTo and Scrollby difference, in fact, 2 of the effect is the same. 131  the 133ScrollTo (intXinty):134 135 if the offset position changes, the MSCROLLX and mscrolly are assigned new values, changing the current position. 136 137 Note: x, Y is not a sitting punctuation, but an offset. 138 139 For example: $ 141I want to move the view to the coordinate point (100,100), then my offset is (0,,0)-(100,100) = (-100,-100), I will execute View.scrollto ( -100,-100) to achieve this effect. 142 143 144Scrollby (intXinty):145 146As seen from the source, it is actually called Scrollto (mscrollx + x, mscrolly +y);147 148MSCROLLX + x and mscrolly +Y, which means that there is an offset on the basis of the original offset, which is popularly said to be relative to our current position offset. 149  Max moved from the parent view, it will not be displayed if it is moved to an outside location. 151 look at the above and you'll know probably.  the 153 154 Below is a small example of the use of these 2 methods,155 156As follows:

Android Layout Slide Explore ScrollTo and Scrollby methods using instructions

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.