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

  • 相关阅读:
    苹果一体机发射Wi-Fi
    iphone 屏蔽系统自动更新,消除设置上的小红点
    data parameter is nil 异常处理
    copy与mutableCopy的区别总结
    java axis2 webservice
    mysql 远程 ip访问
    mysql 存储过程小问题
    mysql游标错误
    is not writable or has an invalid setter method错误的解决
    Struts2中关于"There is no Action mapped for namespace / and action name"的总结
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/3718198.html
Copyright © 2011-2022 走看看