若函数中参数为负值,则子View的移动方向将相反。
scrollBy(int x, int y)其实是对scrollTo的包装,移动的是相当位置。 scrollTo(int x, int y)的源码和scrollBy(int x, int y)源码如下所示.
[java] view plain copy
-
/**
-
* 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
/**
-
* 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();
-
}
-
}
-
}
[java] view plain copy
- /* @param y the amount of pixels to scroll by vertically */
[java] view plain copy
- 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()。这两个函数的源码如下所示:
[java] view plain copy
-
/**
-
* 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;
-
}
[java] view plain copy
-
/**
-
* Return the scrolled top position of this view. This is the top edge of
先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以扫码领取!!!!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可免费领取!
zmLTPvu-1711216444252)]
[外链图片转存中…(img-Syekstad-1711216444252)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可免费领取!