博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
阅读量:7006 次
发布时间:2019-06-27

本文共 2787 字,大约阅读时间需要 9 分钟。

hot3.png

Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向想反。通过提供的api如getLeft , getTop, getBottom, getRight可以获得控件在parent中的相对位置。同时,也可以获得控件在屏幕中的绝对位置,详细用法可参考

当我们编写一些自定义的滑动控件时,会用到一些api如scrollTo(),scrollBy(),getScrollX(), getScrollY()。由于常常会对函数getScrollX(), getScrollY()返回的值的含义产生混淆,尤其是正负关系,因此本文将使用几幅图来对这些函数进行讲解以方便大家记忆。

注意:调用View的scrollTo()和scrollBy()是用于滑动View中的内容,而不是把某个View的位置进行改变。如果想改变莫个View在屏幕中的位置,可以使用如下的方法。

调用public void offsetLeftAndRight(int offset)用于左右移动方法或public void (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)源码如下所示.

 

[java]  

 

  1. /** 
  2.  * Move the scrolled position of your view. This will cause a call to 
  3.  * {
     #onScrollChanged(int, int, int, int)} and the view will be 
  4.  * invalidated. 
  5.  *  x the amount of pixels to scroll by horizontally<pre name="code" class="java">    /** 
  6.  * Set the scrolled position of your view. This will cause a call to 
  7.  * {
     #onScrollChanged(int, int, int, int)} and the view will be 
  8.  * invalidated. 
  9.  *  x the x position to scroll to 
  10.  *  y the y position to scroll to 
  11.  */  
  12. public void scrollTo(int x, int y) {  
  13.     if (mScrollX != x || mScrollY != y) {  
  14.         int oldX = mScrollX;  
  15.         int oldY = mScrollY;  
  16.         mScrollX = x;  
  17.         mScrollY = y;  
  18.         invalidateParentCaches();  
  19.         onScrollChanged(mScrollX, mScrollY, oldX, oldY);  
  20.         if (!awakenScrollBars()) {  
  21.             postInvalidateOnAnimation();  
  22.         }  
  23.     }  
  24. }  

 

 

[java]  

 

  1. /* @param y the amount of pixels to scroll by vertically */   

[java]  

 

  1. 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]  

 

  1. /** 
  2.  * Return the scrolled left position of this view. This is the left edge of 
  3.  * the displayed part of your view. You do not need to draw any pixels 
  4.  * farther left, since those are outside of the frame of your view on 
  5.  * screen. 
  6.  * 
  7.  * @return The left edge of the displayed part of your view, in pixels. 
  8.  */  
  9. public final int getScrollX() {  
  10.     return mScrollX;  
  11. }  

 

[java]  

 

  1. /** 
  2.  * Return the scrolled top position of this view. This is the top edge of 
  3.  * the displayed part of your view. You do not need to draw any pixels above 
  4.  * it, since those are outside of the frame of your view on screen. 
  5.  * 
  6.  * @return The top edge of the displayed part of your view, in pixels. 
  7.  */  
  8. public final int getScrollY() {  
  9.     return mScrollY;  
  10. }  

转载于:https://my.oschina.net/JiangTun/blog/727724

你可能感兴趣的文章
linux 添加静态路由
查看>>
PHP中file_exists()函数不能检测包含中文的文件名的解决办法
查看>>
How can I create a dump of SQL Server?
查看>>
排序(3)---------冒泡排序(C语言实现)
查看>>
利用React/anu编写一个弹出层
查看>>
windows下配置nginx+php环境
查看>>
[工具配置]使用requirejs模块化开发多页面一个入口js的使用方式
查看>>
Jenkins具体安装与构建部署使用教程
查看>>
【ES】学习9-聚合2
查看>>
Mindjet MindManager 思维导图软件-使用思维导图跟踪调用流程,绘制软件框架
查看>>
SQLServer判断指定列的默认值是否存在,并修改默认值
查看>>
贝塞尔曲线与CSS3动画、SVG和canvas的应用
查看>>
将NSTimer加入至RunLoop中的两种方法差别
查看>>
[ajax 学习笔记] ajax初试
查看>>
css中合理的使用nth-child实现布局
查看>>
每天一个JavaScript实例-操作元素定位元素
查看>>
架构-到底什么时候该使用MQ【转】
查看>>
split-brain 脑裂问题(Keepalived)
查看>>
清空,再来
查看>>
7.JAVA编程思想笔记隐藏实施过程
查看>>