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. Docker Swarm Mode无法增加管理节点

    这两天用Docker Swarm Mode,加入新的管理节点会报以下错误(在/var/log/messages文件中可以看到): Handler for POST /v1.37/swarm/join ...

  2. fiddler怎么修改服务器返回参数并发送

    在进行使用fiddler中提供了较多的功能进行使用的,那么进行就可以对当前的网页中的暂停之后,在进行使用fiddler中把拦截起的后,在进行修改的服务器参数的之后,在把参数修改完成之后,把进行发送出去 ...

  3. Html Agility Pack - API

    Html Agility Pack - APIParserSelectorsManipulationTraversingWriterUtilitiesAttributes HTML Parser HT ...

  4. NE555

    Turn-off time less than 2μsMax. operating frequency greater than 500kHzTemperature stability of 0.00 ...

  5. Ubuntu16.04 Arduino UNO R3开发板

    使用的是国内改过的版本, usb芯片换成了ch341, 晶振不是原版的16MHz而是12MHz, 杜邦线孔布局和原版一致. 设备连接 在Ubuntu16.04下不需要驱动, 能直接认出ch341设备 ...

  6. ios中开始页面做法

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  7. Linux-使用ctags、vim查看数据类型所在头文件

    安装 ctags yum -y install ctags 生成 tags 文件 cd /usr/include ctags -R . 在 vim 中配置 tags #修改 vim 配置文件.或者使用 ...

  8. SSD基本工作原理

    SSD主要由SSD控制器,FLASH存储阵列,板上DRAM(可选),以及跟HOST接口(诸如SATA,SAS, PCIe等)组成. SSD主控通过若干个通道(channel)并行操作多块FLASH颗粒 ...

  9. Xcode7 运行iOS10以上系统(10.1、10.2、10.3)解决Could not find Developer Disk Image

    由于历史原因,需要在Xcode7上真机运行下app,无奈手机系统已是10.3了,一运行, 就提示:Could not find Developer Disk Image 解决办法: 1.找到xcode ...

  10. JSONObject.parseObject(jsonStr);和JSONObject.fromObject(jsonStr);

    虽然都是返回JSONObject,但是 JSONObject.parseObject(jsonStr);这个方法需要导入import com.alibaba.fastjson.JSONObject; ...