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

來源:互聯網
上載者:User

標籤:理解   編寫   jsb   nts   ace   ons   lin   cache   use   

Android系統手機螢幕的左上方為座標系,同一時候y軸方向與笛卡爾座標系的y軸方向想反。通過提供的api如getLeft , getTop, getBottom, getRight能夠獲得控制項在parent中的相對位置。同一時候。也能夠獲得控制項在螢幕中的絕對位置,具體使用方法可參考android應用程式中擷取view的位置

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

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

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


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


 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.