zoukankan      html  css  js  c++  java
  • [学习总结]1、View的scrollTo 和 scrollBy 方法使用说明和区别

    参考资料:http://blog.csdn.net/vipzjyno1/article/details/24577023 非常感谢这个兄弟!

    先查看这2个方法的源码:

    scrollTo:

     1  /**
     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     }

     scrollBy:

     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
     6      * @param y the amount of pixels to scroll by vertically
     7      */
     8     public void scrollBy(int x, int y) {
     9         scrollTo(mScrollX + x, mScrollY + y);
    10     }

    scrollTo是移动到这个点,scrollBy是相对view当前的位置去偏移位置,

    scrollTo(int x,int y):

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

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

    例如:

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

    scrollBy(int x,int y):

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

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

    例如:

    scrollBy(-100,-100)相对于view的当前位置去偏移-100,100,第一调用也就是0,0点 第2次调用当前位置就是-100,100。

    demo:

    http://download.csdn.net/detail/vipzjyno1/7260923

  • 相关阅读:
    HashMap源码分析
    ArrayList、LinkedList和Vector源码分析
    java序列化
    Python Web自动化测试入门与实战,从入门到入行
    Chrome 自带截图工具
    【转】chrome DEvTools 使用,进行定位元素
    偷懒大法好,用 selenium 做 web 端自动化测试
    Python代码覆盖率分析工具Coverage
    Jmeter分布式压力测试
    通过dockerfile制作镜像
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/3718198.html
Copyright © 2011-2022 走看看