Android Layout Slide Explore ScrollTo and Scrollby methods using instructions

Source: Internet
Author: User

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:

View Source

[Java] view Plaincopy 
    1. /** 
    2.  * implement this to do your  drawing. 
    3.  * 
    4.  */  
    5. protected void ondraw ( Canvas canvas)  {  

Can be found that the implementation of theview 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 Plaincopy 
    1. Public void AddView (View child) {
    2. AddView (Child,-1);
    3. }

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.

First, 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, beyond which the display area will not be displayed in the parent view of the area, corresponding, we can call this bounded view of the layout coordinates------the parent view to the child view assigned layout (layout) size. Also, the starting coordinates of a view on the screen are at the beginning of the view coordinates, as shown in.


In fact, the upper-left corner coordinates of the parent view are the origin (0,0), not the upper-left corner of the overall viewgroup as the Origin point.

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.) )

Here's a rough overview of the view and ViewGroup, (on the internet many great gods have analyzed this piece, here just did a few excerpt record) purpose is to elicit today's protagonist Scrollto and Scrollby.

View the following source code can be found:

[Java] view Plaincopy 
  1. <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" > /**
  2. * The offset, in pixels, by which the content of this view is scrolled
  3. * Horizontally.
  4. * {@hide}
  5. */
  6. @ViewDebug. Exportedproperty (category = "scrolling")
  7. protected int mscrollx;
  8. /** 
  9. * The offset, in pixels, by which the content of this view is scrolled
  10. * Vertically.
  11. * {@hide}
  12. */
  13. @ViewDebug. Exportedproperty (category = "scrolling")
  14. protected int mscrolly;
  15. /** 
  16. * Return the scrolled left position of this view. The left edge of
  17. * The displayed part of your view. Need to draw any pixels
  18. * Farther left, since those is outside of the frame of your view on
  19. * screen.
  20. *
  21. * @return the left edge of the displayed part of your view, in pixels.
  22. */
  23. public Final int getscrollx () {
  24. return MSCROLLX;
  25. }
  26. /** 
  27. * Return the scrolled top position of this view. The top edge of
  28. * The displayed part of your view. Need to draw any pixels above
  29. * It, since those is outside of the frame of your view on screen.
  30. *
  31. * @return The top edge of the displayed part of your view, in pixels.
  32. */
  33. public Final int getscrolly () {
  34. return mscrolly;
  35. }</span>


MSCROLLX: Represents the offset from the x horizontal direction from the start of the view

Mscrolly: Represents the offset from the vertical y direction of the view starting position

Obtained by Getscrollx () and getscrolly () method respectively.

Note:Mscrollx and mscrolly refer not to coordinates, but to offsets.

[Java] view Plaincopy 
  1. <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" > /**
  2. * Set the scrolled position of your view. This would cause a call to
  3. * {@link #onScrollChanged (int, int, int, int)} and the view would be
  4. * invalidated.
  5. * @param x position to scroll to
  6. * @param y the y position to scroll
  7. */
  8. public void ScrollTo (int x, int y) {
  9. if (mscrollx! = x | | mscrolly! = y) {
  10. int oldx = MSCROLLX;
  11. int oldY = mscrolly;
  12. MSCROLLX = x;
  13. mscrolly = y;
  14. Invalidateparentcaches ();
  15. Onscrollchanged (MSCROLLX, mscrolly, OLDX, OldY);
  16. if (!awakenscrollbars ()) {
  17. Postinvalidateonanimation ();
  18. }
  19. }
  20. }
  21. /** 
  22. * Move the scrolled position of your view. This would cause a call to
  23. * {@link #onScrollChanged (int, int, int, int)} and the view would be
  24. * invalidated.
  25. * @param x The amount of pixels to scroll by horizontally
  26. * @param y the amount of pixels to scroll by vertically
  27. */
  28. public void Scrollby (int x, int y) {
  29. ScrollTo (mscrollx + x, mscrolly + y);
  30. }</span>


From the above code can be seen,scrollTo and Scrollby difference, in fact, 2 of the effect is the same.

ScrollTo (int x,int y):

if the offset position changes, MSCROLLX and mscrolly are assigned a new value, change the current position.

Note: x, Y is not a sitting punctuation, but an offset.

For example:

I 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.

Scrollby (int x,int y):

As seen from the source, it actually calls the ScrollTo (mscrollx + x, mscrolly + y);

MSCROLLX + x and mscrolly + y, which means that offsets occur on the basis of the original offset, which is popularly said to be relative to our current position offset.

Moved from the parent view, it will not be displayed if it is moved to an outside location.

Look at the above and you'll know probably.

Below is a small example of the use of these 2 methods,

As follows:

The core code is the 2 methods described in this article, and the code is listed here.

Provides a simple demo:

After a rough look at these 2 methods, it is even more helpful to drag the classes behind the scroller and implement a few drag effects.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.