涉及到滑动,就涉及到VIEW,大家都知道,Android的UI界面都是由一个一个的View以及View的派生类组成,View作为基类,而常用的布局里面的各种布局就是它派生出来的ViewGroup的子类,ViewGroup作为各个组件的容器搭建了整体的UI。以下是android UI的结构示示意图:

查看源码

/**

* Implement this to do your drawing.

*

* @param canvas the canvas on which the background will be drawn

*/

protected void onDraw(Canvas canvas) {

}

可以发现,View的实现一般是通过绘制onDraw方法进行,如果你要改变它的界面可以重写onDraw,达到你的效果,在源码中,你找不到    

public void addView(View child) {

addView(child, -1);

}

这个方法,因为它不知道,只有在它的派生类例如ViewGroup中会有这个方式去添加子布局。

而ViewGroup作为一个组件容器,它可以包含任何组件,可是你必须重写他的onLayout() 方法和 onMeasure()来设置容器布局的位置和绘制它的大小才能正常显示。

首先 ,我们必须明白在Android View视图是没有边界的,Canvas是没有边界的,只不过我们通过绘制特定的View时对 Canvas对象进行了一定的操作,例如 : translate(平移)、clipRect(剪切)等,以便达到我们的对该Canvas对象绘制的要求 ,我们可以将这种无边界的视图称为“视图坐标”-----它不受物理屏幕限制。通常我们所理解的一个Layout布局文件只是该视图的显示区域,超过了这个显示区域将不能显示到父视图的区域中 ,对应的,我们可以将这种有边界的视图称为“布局坐标”------ 父视图给子视图分配的布局(layout)大小。而且, 一个视图的在屏幕的起始坐标位于视图坐标起始处,如下图所示。


其实是相对于父类视图的左上角坐标为原点(0,0),而不是整体ViewGroup的左上角为原点。

由于布局坐标只能显示特定的一块内容,所以我们只有移动布局坐标的坐标原点就可以将视图坐标的任何位置显示出来。

(注:例如cocos2D的布局就和android的布局坐标原点坐标不一样,是左下角会原点,所以会有所差异。)

 

这里就大致提下View和ViewGroup,(网上很多大神都对这块进行了分析,这里只是做了少量摘抄记录)目的是为了引出今天的主角scrollTo 和 scrollBy。

查看下源码可以发现:

  1. <span style="font-family:SimSun;font-size:14px;">  /**
  2. * The offset, in pixels, by which the content of this view is scrolled
  3. * horizontally.
  4. * {@hide}
  5. */
  6. @ViewDebug.ExportedProperty(category = "scrolling")
  7. protected int mScrollX;
  8. /**
  9. * The offset, in pixels, by which the content of this view is scrolled
  10. * vertically.
  11. * {@hide}
  12. */
  13. @ViewDebug.ExportedProperty(category = "scrolling")
  14. protected int mScrollY;
  15. /**
  16. * Return the scrolled left position of this view. This is the left edge of
  17. * the displayed part of your view. You do not need to draw any pixels
  18. * farther left, since those are outside of the frame of your view on
  19. * screen.
  20. *
  21. * @return The left edge of the displayed part of your view, in pixels.
  22. */
  23. public final int getScrollX() {
  24. return mScrollX;
  25. }
  26. /**
  27. * Return the scrolled top position of this view. This is the top edge of
  28. * the displayed part of your view. You do not need to draw any pixels above
  29. * it, since those are outside of the frame of your view on screen.
  30. *
  31. * @return The top edge of the displayed part of your view, in pixels.
  32. */
  33. public final int getScrollY() {
  34. return mScrollY;
  35. }</span>

mScrollX:表示离视图起始位置的x水平方向的偏移量

mScrollY:表示离视图起始位置的y垂直方向的偏移量

分别通过getScrollX() 和getScrollY()方法获得。

注意:mScrollX和mScrollY指的并不是坐标,而是偏移量。

  1. <span style="font-family:SimSun;font-size:14px;"> /**
  2. * Set 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 x position to scroll to
  6. * @param y the y position to scroll to
  7. */
  8. public void scrollTo(int x, int y) {
  9. if (mScrollX != x || mScrollY != y) {
  10. int oldX = mScrollX;
  11. int oldY = mScrollY;
  12. mScrollX = x;
  13. mScrollY = y;
  14. invalidateParentCaches();
  15. onScrollChanged(mScrollX, mScrollY, oldX, oldY);
  16. if (!awakenScrollBars()) {
  17. postInvalidateOnAnimation();
  18. }
  19. }
  20. }
  21. /**
  22. * Move the scrolled position of your view. This will cause a call to
  23. * {@link #onScrollChanged(int, int, int, int)} and the view will be
  24. * invalidated.
  25. * @param x the amount of pixels to scroll by horizontally
  26. * @param y the amount of pixels to scroll by vertically
  27. */
  28. public void scrollBy(int x, int y) {
  29. scrollTo(mScrollX + x, mScrollY + y);
  30. }</span>

从以上的代码可以看出,scrollTo 和 scrollBy区别,其实2者的效果是一样的。

 

scrollTo(int x,int y):

如果偏移位置发生了改变,就会给mScrollX和mScrollY赋新值,改变当前位置。

注意:x,y代表的不是坐标点,而是偏移量。

例如:

我要移动view到坐标点(100,100),那么我的偏移量就是(0,,0)  - (100,100) = (-100 ,-100)  ,我就要执行view.scrollTo(-100,-100),达到这个效果。

scrollBy(int x,int y):

从源码中看出,它实际上是调用了scrollTo(mScrollX + x, mScrollY + y);

mScrollX + x和mScrollY + y,即表示在原先偏移的基础上在发生偏移,通俗的说就是相对我们当前位置偏移。

根据父类VIEW里面移动,如果移动到了超出的地方,就不会显示。

查看上文中的示意图你就会知道大概。

 

下面通过一个小例子了解下这2个方法之间的的使用,

效果图如下:

scrollTo 和 scrollBy的更多相关文章

  1. android 布局之滑动探究 scrollTo 和 scrollBy 方法使用说明

    涉及到滑动,就涉及到VIEW,大家都知道,Android的UI界面都是由一个一个的View以及View的派生类组成,View作为基类,而常用的布局里面的各种布局就是它派生出来的ViewGroup的子类 ...

  2. 【Android 界面效果29】研究一下Android滑屏的功能的原理,及scrollTo和scrollBy两个方法

    Android中的滑屏功能的原理是很值得我们去研究的,在知道这两个原理之前,有必要先说说View的两个重要方法,它们就是scrollTo 和scrollBy. Android View视图是没有边界的 ...

  3. Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明

    今天给大家介绍下Android中滑屏功能的一个基本实现过程以及原理初探,最后给大家重点讲解View视图中scrollTo 与 scrollBy这两个函数的区别 . 首先 ,我们必须明白在Android ...

  4. 使用开源的PullToRefreshScrollView scrollTo和scrollby遇到的问题

    在项目中使用了开源的com.handmark.pulltorefresh.library 下拉刷新组件,当中使用了PullToRefreshScrollView ,须要调用scrollTo或者scro ...

  5. 原生JS scroll()、scrollTo()、scrollBy()

    scroll()  此方法接收两个参数,依次为X坐标和Y坐标:设置滚动条的偏移位置 scrollTo() 此方法和scroll()作用一样,都是设置滚动条的偏移位置. scrollBy() 此法发同样 ...

  6. android中scrollTo和scrollBy的理解

    protected   int  mScrollX;    //该视图内容相当于视图起始坐标的偏移量   , X轴 方向 protected   int  mScrollY;    //该视图内容相当 ...

  7. Android中的ScrollTo和ScrollBy解析

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

  8. [学习总结]1、View的scrollTo 和 scrollBy 方法使用说明和区别

    参考资料:http://blog.csdn.net/vipzjyno1/article/details/24577023 非常感谢这个兄弟! 先查看这2个方法的源码: scrollTo: 1 /** ...

  9. 关于平移的 scrollTo和scrollBy的区别

    这几天在项目中要求一部分布局实现整体偏移的效果 在网上查了下我使用来ScrollBy(x,y)方法 他的意思是将view实现整体偏移 而ScollTo(x,y)则是将原点偏移到相应指定的位置即   移 ...

随机推荐

  1. zju3430

    题意:给出多个加密的模式串,和多个待匹配的串,问每个串里出现了多少种模式串.加密方法是把每3bytes加密成按6bits一个对应成4个字符,对应方法题里给了. 分析:除了解密之外,基本是个赤裸裸的AC ...

  2. java web 学习 --第六天(Java三级考试)

    第五天学习在这:http://www.cnblogs.com/tobecrazy/p/3458592.html session对象 当某个用户首次访问web应用系统时,jsp会自动创建出一个sessi ...

  3. 24. javacript高级程序设计-最佳实践

    1. 最佳实践 l 来自其他语言的代码约定可以用于决定何时进行注释,以及如何进行缩进,不过JavaScript需要针对其松散类型的性质创造一些特殊的约定 l javascript应该定义行为,html ...

  4. Greedy:Linear world(POJ 2674)

      Linear world 题目大意:一些人生活在线性世界中,到达线性世界两端就会消失,两个人的前进方向有两个,相遇会改变各自相遇方向,求最后一个人掉下的人的名字和时间. 其实这一题就是弹性碰撞的模 ...

  5. UIButton修改文字大小问题

    一.问题描述 通过UIButton对象font属性设置文字大小,却发现该属性在2.0.3.0就已经被废弃,ios不建议使用. 图1-1:点出UIButton对象的font属性提示被废弃 图1-2:UI ...

  6. Mysql 基础1

    Mysql int 整型float 小数double 小数varchar(20) 字符串bit 布尔型数据datetime 日期时间类型text 长文本 money 存货币image 存二进制数据 数 ...

  7. 轻轻送送为你的App加点特效

    前言 今天突然在一个应用中看到一个转场动画,蛮有意思的 退出动画 进入动画 ActivityOptionsCompat options = ActivityOptionsCompat.makeScal ...

  8. CCF I'm Stuck!

    问题描述 试题编号: 201312-5 试题名称: I'm stuck! 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个R行C列的地图,地图的每一个方格可能是'#', ...

  9. 【jquery】字符ascii码转换函数

    js 字符ascii码转换函数 字符转ascii码:用charCodeAt();ascii码砖字符:用fromCharCode(); 看一个小例子 <script> str="A ...

  10. 手动关闭searchDisplayControlelr

    两行代码搞定 [searchBar endEditing:YES]; [searchDisplayControllersetActive:NO];