Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向想反。提供了

getLeft(),
getTop(),
getBottom(),
getRight()

这些API来获取控件在Parent中的相对位置

同时也提供了

getLocalVisibleRect()
getGlobalVisibleRect()
getLocationOnScreen()
getLocationInWindow()

这些API来获取控件在屏幕中的绝对位置。详情可参考:android应用程序中获取view的位置

如果要将View中的内容滚动到相应到指定位置,可以使用这些API

scrollTo()
scrollBy()

如果要改变整个View在屏幕中的位置,可以使用下列API:

offsetLeftAndRight(int offset) // 用于左右移动
offsetTopAndBottom(int offset) // 用于上下移动

下面简要总结一下scrollTo(),scrollBy(),getScrollX(), getScrollY()这些方法

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的移动方向将相反。

关于scrollTo()方法中参数值为正,却向左移动,参数值为负,却向右移动(这地方确实很怪)的一些理解:

scrollTo()方法本身滚动的是View的内容,View本身位置不变。可以将该View想象成一个带滚动条的窗体,我们以滚动条作为参照物

当水平滚动条向右移动时,原本窗体显示的内容向左移动,比方说水平滚动条向右移动了100的距离,同样的窗体显示的内容就向左移动了100的距离,这个时候也就是scrollTo(100, 0);

当滚动条向下移动时,原本窗体显示的内容应向上移动,比方说垂直滚动条向下移动了100的距离,同样的窗体显示的内容就向上移动了100的距离,这个时候也就是scrollTo(0, 100);

这也就解释了为什么scrollTo()方法中参数大于0,View向左移动,参数小于0,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();
}
}
}
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;
}

View:Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()的理解的更多相关文章

  1. 图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()

    https://blog.csdn.net/bigconvience/article/details/26697645 Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向 ...

  2. 关于View的ScrollTo, getScrollX 和 getScrollY

    下载地址:源代码 当利用 Scroller 去滑动屏幕或者扩展 ScrollView 的时候,总是会用到 getScrollX 和 getScrollY 去获取当前View 滑动到的位置,那么getS ...

  3. Android View 的事件体系

    android 系统虽然提供了很多基本的控件,如Button.TextView等,但是很多时候系统提供的view不能满足我们的需求,此时就需要我们根据自己的需求进行自定义控件.这些控件都是继承自Vie ...

  4. Android -- View移动的六种方法

    layout() 如果你将滑动后的目标位置的坐标传递给layout(),这样子就会把view的位置给重新布置了一下,在视觉上就是view的一个滑动的效果. public class DragView ...

  5. Android View视图系统分析和Scroller和OverScroller分析

    Android  View视图系统分析和Scroller和OverScroller分析 View  视图分析         首先,我们知道.在Android中全部的视图资源(无论是Layout还是V ...

  6. Android View的滑动

    Android View的滑动 文章目录 Android View的滑动 一.实现移动 1.1 layout() 1.2 设置位置偏移量 1.3 改变布局参数 1.4 动画 1.5 ScrollTo以 ...

  7. 浅谈Android View滑动和弹性滑动

    引言 View的滑动这一块在实际开发中是非常重要的,无论是优秀的用户体验还是自定义控件都是需要对这一块了解的,我们今天来谈一下View的滑动. View的滑动 View滑动功能主要可以使用3种方式来实 ...

  8. Android scrollTo() scrollBy() Scroller解说及应用

    版本号:1.0  日期:2014.6.17  2014.6.18 版权:© 2014 kince 转载注明出处   scrollTo() .scrollBy()及 Scroller在视图滑动中常常使用 ...

  9. android view的多种移动方式(测试集合)

    前言 由于最近在写一个涉及移动方面的自定义View,在做移动的时候用到了类似offsetTopAndBottom .setTranslationY.scrollTo.scrollBy等方法,对于他们的 ...

随机推荐

  1. vsftp 虚拟用户测试

    1.创建用于进行FTP验证的帐号密码数据库文件,单数行为账户名,偶数行为密码.[root@rhel1 vsftpd]# vi /etc/vsftpd/vuser.listuser1123456user ...

  2. TL Cop

    第一讲 研发技术型人才的特点 一.为何要重视技术与研发技术管理 1.中国技术型企业近十年的发展路线图以及经验教训 2.技术还是营销:中国企业的痛 3.名家论技术与研发技术管理 二.研发技术人才的特点 ...

  3. 微信小程序支付(java后端)

      第一步 进入小程序,下单,请求下单支付,调用小程序登录API来获取Openid(https://mp.weixin.qq.com/debug/w ... .html#wxloginobject), ...

  4. 微信小程序解决方案合集

    微信小程序解决方案合集:http://www.wxapp-union.com/special/solution.html 跳坑系列:http://www.wxapp-union.com/forum.p ...

  5. android应用Theme(二)

    另外一种实现android应用Theme的方式是通过apk来实现的. 以下是一个demo. 1.首先必须新建一个apk.类似的插件,然后在该apk的AndroidManifest.xml文件的appl ...

  6. 一些有用的git命令清单

    以下是一些我常用的git命令清单 如果以下的命令不清晰细节,请看git的文档. 设置个人信息 git config --global user.name "John Doe" gi ...

  7. XML制作RSS源

    什么是RSS源?看到这片文章的人相信都知道.自己博客首页不就是一个吗? 好吧,先来一个简单点的.直接就是死代码:详细如何使用就看RSS使用标准吧! <?xml version = "1 ...

  8. 共享单车微信小程序

    微信小程序bike单车,前台使用小程序地图控件+weui+小程序相关组件和API,后台使用SpringBoot+JPA,用户及单车信息保存进mongodb,短信平台的配置信息和临时生成的验证码存放进r ...

  9. webview中事件的用法

    封装 MBProgressHud ==================================== #import "MBProgressHUD.h" @interface ...

  10. 【LeetCode】Missing Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...