https://blog.csdn.net/bigconvience/article/details/26697645

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

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

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

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

  1.  
    /**
  2.  
    * Move the scrolled position of your view. This will cause a call to
  3.  
    * {@link #onScrollChanged(int, int, int, int)} and the view will be
  4.  
    * invalidated.
  5.  
    * @param 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.  
    * {@link #onScrollChanged(int, int, int, int)} and the view will be
  8.  
    * invalidated.
  9.  
    * @param x the x position to scroll to
  10.  
    * @param 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.  
    }
/* @param y the amount of pixels to scroll by vertically */ 
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()。这两个函数的源码如下所示:

  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.  
    }
    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.  
      }

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

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

    Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向想反.提供了 getLeft(), getTop(), getBottom(), getRight() 这些API来获取 ...

  2. 关于View的ScrollTo, getScrollX 和 getScrollY

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

  3. Android View 的事件体系

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

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

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

  5. getX,getY,getScrollX,getScrollY,ScrollTo(),ScrollBy()辨析

    前言:前两天看了自定义控件,其中有一些东西我觉得有必要深入理解一下 以下图为例: getX(),getY()返回的是触摸点A相对于view的位置 getRaw(),getRawY()返回的是触摸点B相 ...

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

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

  7. Android中的ScrollTo和ScrollBy解析

    关于Android中的ScrollBy和ScrollTo方法相信大家并不陌生,这两个方法是在View中实现的.所以在各个继承了View的类都可以使用改方法. 在View中对这两个方法的源码编写是这样的 ...

  8. 图解Android - Android GUI 系统 (2) - 窗口管理 (View, Canvas, Window Manager)

    Android 的窗口管理系统 (View, Canvas, WindowManager) 在图解Android - Zygote 和 System Server 启动分析一 文里,我们已经知道And ...

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

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

随机推荐

  1. Struts2中 radio标签的详细使用方法

    首先在页面中引入struts标签库: <%@ taglib prefix="s" uri="/struts-tags"%> 在JSP页面中创建单选按 ...

  2. Supervised Hashing with Kernels, KSH

    Notation 该论文中应用到较多符号,为避免混淆,在此进行解释: n:原始数据集的大小 l:实验中用于监督学习的数据集大小(矩阵S行/列的大小) m:辅助数据集,用于得到基于核的哈希函数 r:比特 ...

  3. 课堂Beta发布

    项目组名:奋斗吧兄弟 小组成员:黄兴,李俞寰,栾骄阳,王东涵,杜桥 今天6个小组在课上进行了Bate发布,以下是我的一些看法: 飞天小女警的礼物挑选系统: 由于是第一个Bate发布的项目,所以我印象较 ...

  4. node之body-parser的使用

    bodyparser 用来解析post的请求取代了 原生的 req.on 的方式 但是只能取到ajax 和表单的数据 ,取不到上传的文件类型. let express = require('expre ...

  5. Serial,Parallel,CMS,G1四大GC收集器特点小结

    1.Serial收集器一个单线程的收集器,在进行垃圾收集时候,必须暂停其他所有的工作线程直到它收集结束.特点:CPU利用率最高,停顿时间即用户等待时间比较长.适用场景:小型应用通过JVM参数-XX:+ ...

  6. Windows 使用 StarWind 创建的 Oracle RAC环境 异常关机之后的处理过程

    创建好了 虚拟机之后发现 偶尔会出现 蓝屏重启的现象, 这个时候 需要进行 异常处理 确定虚拟机已经开机之后 1. 打开iscsi的连接设备, 确认 iscsi的正常连接到虚拟机的 存储设备 注意 r ...

  7. GS 服务器端开启webservice 远程调试的方法

    1. 修改 安装目录下 web.config的文件. 一般目录为: C:\Program Files\GenerSoft\bscw_local\web.config 为了保证安全想把文件备份一下. 2 ...

  8. Java字节流与字符流

    九.字节流与字符流 9.1 IO的分类 <段落>根据数据的流向分为:输入流和输出流. 输入流 :把数据从其他设备上读取到内存中的流. 输出流 :把数据从内存 中写出到其他设备上的流. 数据 ...

  9. 51Nod 1175 区间中第K大的数 (可持久化线段树+离散)

    1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题   一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...

  10. ssm框架配置过程

    1.pom.xml配置 1.1<build>标签中配置<plugins>和<resources>,即插件和资源文件 1.2 <properties>标签 ...