圖解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()

Android系統手機屏幕的左上角爲座標系,同時y軸方向與笛卡爾座標系的y軸方向想反。通過提供的api如getLeft , getTopgetBottomgetRight可以獲得控件在parent中的相對位置。同時,也可以獲得控件在屏幕中的絕對位置,詳細用法可參考android應用程序中獲取view的位置

當我們編寫一些自定義的滑動控件時,會用到一些api如scrollTo(),scrollBy(),getScrollX(), getScrollY()。由於常常會對函數getScrollX(), getScrollY()返回的值的含義產生混淆,尤其是正負關係,因此本文將使用幾幅圖來對這些函數進行講解以方便大家記憶。

注意:調用View的scrollTo()和scrollBy()是用於滑動View中的內容,而不是把某個View的位置進行改變。如果想改變莫個View在屏幕中的位置,可以使用如下的方法。

調用public void offsetLeftAndRight(int offset)用於左右移動方法或public void offsetTopAndBottom(int offset)用於上下移動。

                 如:button.offsetLeftAndRignt(300)表示將button控件向左移動300個像素。


scrollTo(int x, int y) 是將View中內容滑動到相應的位置,參考的座標系原點爲parent View的左上角。

       調用scrollTo(100, 0)表示將View中的內容移動到x = 100, y = 0的位置,如下圖所示。注意,圖中黃色矩形區域表示的是一個parent View,綠色虛線矩形爲parent view中的內容。一般情況下兩者的大小一致,本文爲了顯示方便,將虛線框畫小了一點。圖中的黃色區域的位置始終不變,發生位置變化的是顯示的內容。


 同理,scrollTo(0, 100)的效果如下圖所示:


scrollTo(100, 100)的效果圖如下:


若函數中參數爲負值,則子View的移動方向將相反。

scrollBy(int x, int y)其實是對scrollTo的包裝,移動的是相當位置。 scrollTo(int x, int y)的源碼和scrollBy(int x, int y)源碼如下所示.

    /**
     * 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); }



可見,mScrollX和mScrollY是View類中專門用於記錄滑動位置的變量。這兩個函數最終調用onScrollChanged()函數,感興趣者可以參考他們的源代碼。

理解了scrollTo(int x, int y)和scrollBy(int x, int y)的用法,就不難理解getScrollX() 和getScrollY()。這兩個函數的源碼如下所示:

    /**
     * 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;
    }


 


發佈了49 篇原創文章 · 獲贊 21 · 訪問量 81萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章