android scroller

关于这个类网上的讲的大多没有把难点说出来下面我就剖析下这个类

  1:首先scrollTo;这里的两个参数是滑动到的意思;这两个参数是目的。

    /**
     * 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();
            }
        }
    }

2:scrollBy这两个入参数是过程。就是位移。

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

       重要理解思想:一个View都是因为限定了其长宽高的时候,显示的时候屏幕就像是拿着望远镜看事物一样的

   一、不使用Scroller单纯的view 滚动就会出现被自己父类限定的位置遮挡。

           当然也通常在让要滚动的view match_parent 这样view 调用scrollTo和scrollBy的时候不会遮挡;实现一个直接滚动到某处的效果;就像ScrollView      scrollTo           那样的效果;

二、Scroller 是一个提供滑动的类;这个类是个工具;他帮忙计算过程。

           startScroll(int startX, int startY, int dx, int dy, int duration);

          //postInvalidate();

          实现的是从前两个是执行此方法;此viewGroup 就会到滚到这里的初始值;//只是设置个值,并没有直接执行操作

         此viewGroup要滚动的位移;//这里也是只是设置个值并没有执行

       问题来啦;怎么滚动;

     @Override
public void computeScroll() {

      // 如果返回true,表示动画还没有结束
// 因为前面startScroll,所以只有在startScroll完成时 才会为false

                            Scroller.computeScrollOffset()

  // 重写这个完成滚动这个方法;这个方法虽然叫完成滚动但是他却会在滚动的时候被多次调用;是一个过程量;所有的滚动操作就是在这里面完成的

    //   在这个时候scroller 获取当前的位置getCurrX 和getcurry;

     //让子类view 执行scrollTo 的时候就能实现子类的滚动

    //viewGroup 执行scrollTo  就会把所有这个viewGroup 的子类进行滚动;viewGroup 不执行scrollTo   就没有效果此效果。

    //postInvalidate();

       }

    

    代码地址:http://download.csdn.net/detail/u012629497/8340883





         


      












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值