scrollTo与scrollBy

来源:互联网 发布:国防强固必须优化 编辑:程序博客网 时间:2024/05/09 17:50

scrollTo与scrollBy


mScrollX

屏幕的左侧边缘跟内容的左侧边缘的距离

getScrollX()

可以得到mScrollX的值

mScrollY

屏幕的上侧边缘跟内容的上侧边缘的距离

getScrollY()

可以得到mScrollY的值


scrollTo(int x,int y)

将内容移动到x,y位置

经过代码测试发现,移动后mScrollX和mScrollY的值分别跟x和y的值相等,也就是说可以“理解”scrollTo方法,scrollTo方法是通过将x,y分别赋值给mScrollX跟mScorllY来使内容进行移动的。

实验代码

当x > 0 ,内容向左侧移动
当x < 0 ,内容向右侧移动
当y > 0 ,内容向上侧移动
当y < 0 ,内容向下侧移动

 /**     * 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;            //回调onScrollChanged方法            onScrollChanged(mScrollX, mScrollY, oldX, oldY);            if (!awakenScrollBars()) {                invalidate();  //一般都引起重绘            }        }    }

scrollBy(int x,int y)

将内容移动x,y

当x > 0 ,内容向左侧移动
当x < 0 ,内容向右侧移动
当y > 0 ,内容向上侧移动
当y < 0 ,内容向下侧移动

 /**     * 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     * @param y the amount of pixels to scroll by vertically     */    // 看出原因了吧 。。 mScrollX 与 mScrollY 代表我们当前偏移的位置 , 在当前位置继续偏移(x ,y)个单位    public void scrollBy(int x, int y) {        scrollTo(mScrollX + x, mScrollY + y);    }
0 0
原创粉丝点击