Graphic Android View (scrollTo), scrollBy (), getScrollX (), getScrollY ()

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Android mobile phone system in the upper left corner of the screen for the coordinate system, at the same time, the direction of Y axis and Y axis coordinate system to the anti Descartes. By providing a API such as getLeft, getTop, getBottom, relative position of the getRight can gain control in parent. At the same time, the absolute position can also gain control on the screen in detail, can obtain the View reference in the Android application position

When we write sliding controls some custom, will use some API such as scrollTo(),scrollBy(),getScrollX(), getScrollY(). Because often the function getScrollX (), getScrollY () returns the value of the meaning of confusion, especially the relation, so this paper will use the pictures to explain these functions to facilitate memory.

Note: call View () and scrollBy (scrollTo) is used for sliding the contents of View, instead of a View position change. If you want to change the Mo View position on the screen, you can use the following method.

Call the public void offsetLeftAndRight (int offset) to move around or public void offsetTopAndBottom(int offset)For moving up and down.

Such as: button.offsetLeftAndRignt (300) said the button control to move to the left 300 pixels.


scrollTo(int x, int y) Is the ViewContentSliding to the corresponding location, coordinate origin of reference for the upper left corner of the parent View.

Call scrollTo (100, 0) that will move the contents of View to x = 100, y = 0, as shown below. Note, the yellow rectangle representation in the graph is a parent View, green dotted rectangle is parent the contents of the view. Generally the same size, in order to show the convenience, the dashed box painting a small point. The yellow areas in the images position remains unchanged, change position is displayed content.


Similarly, scrollTo (0, 100) effect as shown below:


scrollTo(100, 100)Effect diagram as follows:


If the function parameter is negative, then View will be the opposite direction.

scrollBy(int x, int y)In fact, the scrollTo packaging, mobile is a position. ScrollTo (int x, int y) source code and scrollBy (int x, int y) source code as shown below.

    /**
     * Move the scrolled position of your view. This will cause a call to
     * {@link #onScrollChanged(int, int, int, int)} and the view will be
     * invalidated.
     * @param x the amount of pixels to scroll by horizontally<pre name="code" class="java">    /**
     * Set the scrolled position of your view. This will cause a call to
     * {@link #onScrollChanged(int, int, int, int)} and the view will be
     * invalidated.
     * @param x the x position to scroll to
     * @param y the y position to scroll to
     */
    public void scrollTo(int x, int 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();
            }
        }
    }

/* @param y the amount of pixels to scroll by vertically */ 
public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y); }


Visible, mScrollX and mScrollY is the View class for recording the position of the sliding variable. The two function eventually call onScrollChanged () function, interested can refer to their source code.

Understanding of scrollTo (int x, int y) and scrollBy (int x, int y) usage, it is not difficult to understand the getScrollX () and getScrollY (). The two functions of the source are shown as follows.:

    /**
     * Return the scrolled left position of this view. This is the left edge of
     * the displayed part of your view. You do not need to draw any pixels
     * farther left, since those are outside of the frame of your view on
     * screen.
     *
     * @return The left edge of the displayed part of your view, in pixels.
     */
    public final int getScrollX() {
        return mScrollX;
    }

    /**
     * Return the scrolled top position of this view. This is the top edge of
     * the displayed part of your view. You do not need to draw any pixels above
     * it, since those are outside of the frame of your view on screen.
     *
     * @return The top edge of the displayed part of your view, in pixels.
     */
    public final int getScrollY() {
        return mScrollY;
    }



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Alger at July 02, 2014 - 2:32 PM