Graphic Android View Scrollto (), Scrollby (), Getscrollx (), getscrolly ()

Source: Internet
Author: User

The upper-left corner of the Android phone's screen is a coordinate system, while the y-axis direction is reversed with the y-axis of the Cartesian coordinate system. By providing an API such asGetLeft , GetTop, Getbottom, GetRight can get the relative position of the control in the parent. At the same time, you can also get the absolute position of the control in the screen, detailed usage can refer towhere to get view in Android app

When we write some custom sliding controls, we use some APIs such as Scrollto (), Scrollby (), Getscrollx (), getscrolly (). Because of the confusion of the meaning of the value returned by the function getscrollx (), getscrolly (), especially the positive and negative relationships, this article will use a few graphs to explain these functions to make it easier for everyone to remember.

Note: Calling view's Scrollto () and Scrollby () is used to slide the contents of the view instead of changing the location of a view. If you want to change the position of Mo view on the screen, you can use the following method.

Calling public void offsetleftandright (int offset) is used for the left and right move method or public void offsettopandbottom(int offset) to move up or down.

For example: BUTTON.OFFSETLEFTANDRIGNT (300) means that the button control is moved to the left by 300 pixels.


scrollTo (int x, int y) is the view in the content slide to the appropriate location and the reference coordinate system origin is the upper-left corner of the parent view.

The call to Scrollto (100, 0) indicates that the contents of the view are moved to the position x = 0, y =, as shown in. Notice that the yellow rectangular area of the figure represents a parent view, and the green dashed rectangle is the content in the parent view. In general, the size of the two are consistent, this article for the convenience of display, the dashed box is a little bit smaller. The position of the yellow area in the figure is always the same, and the position changes is the displayed content.


Similarly, the effect of scrollTo (0, 100) is as follows:


ScrollTo (100, 100) is as follows:


If the argument in the function is negative, the child view moves in the opposite direction.

Scrollby (int x, int y) is actually the wrapper on the Scrollto, moving is quite a position. The source code for ScrollTo (int x, int y) and Scrollby (int x, int y) are shown below.

[Java]View PlainCopyPrint?
  1. /**
  2. * Move 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 The amount of pixels to scroll by horizontally<pre name= "code" class= "java" >/**
  6. * Set The scrolled position of your view. This would cause a call to
  7. * {@link #onScrollChanged (int, int, int, int)} and the view would be
  8. * invalidated.
  9. * @param x position to scroll to
  10. * @param y the y position to scroll
  11.  */
  12. Public void scrollTo (int x, int y) {
  13. if (MSCROLLX! = x | | mscrolly! = y) {
  14. int oldx = mscrollx;
  15. int OldY = mscrolly;
  16. MSCROLLX = x;
  17. mscrolly = y;
  18. Invalidateparentcaches ();
  19. Onscrollchanged (MSCROLLX, mscrolly, OLDX, OldY);
  20. if (!awakenscrollbars ()) {
  21. Postinvalidateonanimation ();
  22. }
  23. }
  24. }
/** * Move The scrolled position of your view.     This would cause a call to * {@link #onScrollChanged (int, int, int, int)} and the view would be * invalidated. * @param x The amount of pixels to scroll by horizontally<pre name= "code" class= "java" >/** * Set the Scrolle D position of your view.     This would cause a call to * {@link #onScrollChanged (int, int, int, int)} and the view would be * invalidated. * @param x position to scroll to * @param y the y position to scroll to */public void ScrollTo (Int. x, in            T y) {if (Mscrollx! = x | | mscrolly! = y) {int oldx = MSCROLLX;            int OldY = mscrolly;            MSCROLLX = x;            mscrolly = y;            Invalidateparentcaches ();            Onscrollchanged (MSCROLLX, mscrolly, OLDX, OldY);            if (!awakenscrollbars ()) {postinvalidateonanimation (); }        }    }

[Java]View PlainCopyPrint?
    1. / * @param y the amount of pixels to scroll by vertically * /
/* @param y the amount of pixels to scroll by vertically */
[Java]View PlainCopyPrint?
    1. Public void Scrollby (int x, int y) {scrollTo (mscrollx + x, mscrolly + y);}
public void Scrollby (int x, int y) {scrollTo (mscrollx + x, mscrolly + y);}


Visible, MSCROLLX and mscrolly are variables that are specifically used in the view class to record the sliding position. These two functions eventually call the onscrollchanged () function, and interested parties can refer to their source code.

Understanding the use of Scrollto (int x, int y) and Scrollby (int x, int y) is not difficult to understand GETSCROLLX () and getscrolly (). The source code for these two functions is as follows:

[Java]View PlainCopyPrint?
  1. /**
  2. * Return The scrolled left position of this view. the left edge of
  3. * The displayed part of your view. Need to draw any pixels
  4. * Farther left, since those is outside of the frame of your view on
  5. * screen.
  6.  *
  7. * @return The left edge of the displayed part of your view, in pixels.
  8.  */
  9. Public Final int getscrollx () {
  10. return mscrollx;
  11. }
    /**     * Return the scrolled left position for this view. The the left edge of * The displayed part of     your view. You don't need to draw any pixels     * Farther left, since those is outside of the frame of your view on     * screen.< c5/>*     * @return The left edge of the displayed part of your view, in pixels.     *    /public final int getscrollx () {        return mscrollx;    }

[Java]View PlainCopyPrint?
  1. /**
  2. * Return the scrolled top position of this view. The top edge of
  3. * The displayed part of your view. Need to draw any pixels above
  4. * It, since those is outside of the frame of your view on screen.
  5.  *
  6. * @return The top edge of the displayed part of your view, in pixels.
  7.  */
  8. Public Final int getscrolly () {
  9. return mscrolly;
  10. }

Graphic Android View Scrollto (), Scrollby (), Getscrollx (), getscrolly ()

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.