scrollTo与scrollBy

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值