View:Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()的理解
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()的理解的更多相关文章
- 图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
https://blog.csdn.net/bigconvience/article/details/26697645 Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向 ...
- 关于View的ScrollTo, getScrollX 和 getScrollY
下载地址:源代码 当利用 Scroller 去滑动屏幕或者扩展 ScrollView 的时候,总是会用到 getScrollX 和 getScrollY 去获取当前View 滑动到的位置,那么getS ...
- Android View 的事件体系
android 系统虽然提供了很多基本的控件,如Button.TextView等,但是很多时候系统提供的view不能满足我们的需求,此时就需要我们根据自己的需求进行自定义控件.这些控件都是继承自Vie ...
- Android -- View移动的六种方法
layout() 如果你将滑动后的目标位置的坐标传递给layout(),这样子就会把view的位置给重新布置了一下,在视觉上就是view的一个滑动的效果. public class DragView ...
- Android View视图系统分析和Scroller和OverScroller分析
Android View视图系统分析和Scroller和OverScroller分析 View 视图分析 首先,我们知道.在Android中全部的视图资源(无论是Layout还是V ...
- Android View的滑动
Android View的滑动 文章目录 Android View的滑动 一.实现移动 1.1 layout() 1.2 设置位置偏移量 1.3 改变布局参数 1.4 动画 1.5 ScrollTo以 ...
- 浅谈Android View滑动和弹性滑动
引言 View的滑动这一块在实际开发中是非常重要的,无论是优秀的用户体验还是自定义控件都是需要对这一块了解的,我们今天来谈一下View的滑动. View的滑动 View滑动功能主要可以使用3种方式来实 ...
- Android scrollTo() scrollBy() Scroller解说及应用
版本号:1.0 日期:2014.6.17 2014.6.18 版权:© 2014 kince 转载注明出处 scrollTo() .scrollBy()及 Scroller在视图滑动中常常使用 ...
- android view的多种移动方式(测试集合)
前言 由于最近在写一个涉及移动方面的自定义View,在做移动的时候用到了类似offsetTopAndBottom .setTranslationY.scrollTo.scrollBy等方法,对于他们的 ...
随机推荐
- 转 PHP5+APACHE2.2配置
初学php,配置起来老出问题,找了篇不错的帖子,一试就通过了,所以就顺带着转了过来. 不过在我安装phpMyAdmin的时候还是发现这篇文章的一个问题,就是php.ini如果放在system32下,启 ...
- Aggressive cows
总时间限制: 1000ms 内存限制: 65536kB 描述 Farmer John has built a new long barn, with N (2 <= N <= 100,00 ...
- 自制小工具含源码——SPTC上海交通卡余额查询
说明 需求 开发 其他
- (原)ubuntnu中anaconda的g++提示crtbeginS.o:unrecognized relocation
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6950263.html 自从使用anaconda后,方便是方便了,也遇到了很多蛋疼的问题. 这次使用an ...
- Android开发环境——调试器 DDMS相关内容汇总
Android开发环境将分为SDK相关内容.Eclipse ADT相关内容.模拟器AVD相关内容.调试器DDMS相关内容.日志LogCat相关内容.连接驱动ADB相关内容.内存泄露检测工具MAT ...
- magento 得到树形结构的分类列表
<?php ?> <?php class Lehui_AllCategoryList_Block_List extends Mage_Core_Block_Template { ...
- 【Servlet】web.xml中welcome-file-list的作用
今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...
- [转]在Linux CentOS 6.6上安装Python 2.7.9
在Linux CentOS 6.6上安装Python 2.7.9 查看python安装版本 python -V yum中最新的也是Python 2.6.6,所以只能下载Python 2.7.9的源代码 ...
- 【Algorithm】自顶向下的归并排序
一. 算法描述 自顶向下的归并排序:采用分治法进行自顶向下的程序设计方式,分治法的核心思想就是分解.求解.合并. 先将长度为N的无序序列分割平均分割为两段 然后分别对前半段进行归并排序.后半段进行归并 ...
- mongoDB 32位 安装包地址
https://www.mongodb.org/dl/win32/i386 http://downloads.mongodb.org/win32/mongodb-win32-i386-3.2.4-si ...