当前位置: 移动技术网 > 移动技术>移动开发>Android > 深入理解Android中Scroller的滚动原理

深入理解Android中Scroller的滚动原理

2019年07月24日  | 移动技术网移动技术  | 我要评论
view的平滑滚动效果 什么是实现view的平滑滚动效果呢,举个简单的例子,一个view从在我们指定的时间内从一个位置滚动到另外一个位置,我们利用scroller类可以实

view的平滑滚动效果

什么是实现view的平滑滚动效果呢,举个简单的例子,一个view从在我们指定的时间内从一个位置滚动到另外一个位置,我们利用scroller类可以实现匀速滚动,可以先加速后减速,可以先减速后加速等等效果,而不是瞬间的移动的效果,所以scroller可以帮我们实现很多滑动的效果。

首先我们先来看一下scroller的用法,基本可概括为“三部曲”:

1、创建一个scroller对象,一般在view的构造器中创建:

public scrollviewgroup(context context) {
  this(context, null);
}

public scrollviewgroup(context context, attributeset attrs) {
  this(context, attrs, 0);
}

public scrollviewgroup(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  mscroller = new scroller(context);
}

2、重写view的computescroll()方法,下面的代码基本是不会变化的:

@override
public void computescroll() {
  super.computescroll();
  if (mscroller.computescrolloffset()) {
    scrollto(mscroller.getcurrx(), mscroller.getcurry());
    postinvalidate();
  }
}

3、调用startscroll()方法,startx和starty为开始滚动的坐标点,dx和dy为对应的偏移量:

mscroller.startscroll (int startx, int starty, int dx, int dy);
invalidate();

上面的三步就是scroller的基本用法了。

那接下来的任务就是解析scroller的滚动原理了。

而在这之前,我们还有一件事要办,那就是搞清楚scrollto()scrollby()的原理。scrollto()scrollby()的区别我这里就不重复叙述了,不懂的可以自行google或百度。

下面贴出scrollto()的源码:

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

设置好mscrollxmscrolly之后,调用了onscrollchanged(mscrollx, mscrolly, oldx, oldy);  ,view就会被重新绘制。这样就达到了滑动的效果。

下面我们再来看看scrollby()  :

public void scrollby(int x, int y) {
  scrollto(mscrollx + x, mscrolly + y);
}

这样简短的代码相信大家都懂了,原来scrollby()内部是调用了scrollto()的。但是scrollto() / scrollby()的滚动都是瞬间完成的,怎么样才能实现平滑滚动呢。

不知道大家有没有这样一种想法:如果我们把要滚动的偏移量分成若干份小的偏移量,当然这份量要大。然后用scrollto() / scrollby()每次都滚动小份的偏移量。在一定的时间内,不就成了平滑滚动了吗?没错,scroller正是借助这一原理来实现平滑滚动的。

下面我们就来看看源码吧!

根据“三部曲”中第一部,先来看看scroller的构造器:

public scroller(context context, interpolator interpolator, boolean flywheel) {
  mfinished = true;
  if (interpolator == null) {
    minterpolator = new viscousfluidinterpolator();
  } else {
    minterpolator = interpolator;
  }
  mppi = context.getresources().getdisplaymetrics().density * 160.0f;
  mdeceleration = computedeceleration(viewconfiguration.getscrollfriction());
  mflywheel = flywheel;

  mphysicalcoeff = computedeceleration(0.84f); // look and feel tuning
}

在构造器中做的主要就是指定了插补器,如果没有指定插补器,那么就用默认的viscousfluidinterpolator

我们再来看看scroller的startscroll()

public void startscroll(int startx, int starty, int dx, int dy, int duration) {
  mmode = scroll_mode;
  mfinished = false;
  mduration = duration;
  mstarttime = animationutils.currentanimationtimemillis();
  mstartx = startx;
  mstarty = starty;
  mfinalx = startx + dx;
  mfinaly = starty + dy;
  mdeltax = dx;
  mdeltay = dy;
  mdurationreciprocal = 1.0f / (float) mduration;
}

我们发现,在startscroll()里面并没有开始滚动,而是设置了一堆变量的初始值,那么到底是什么让view开始滚动的?我们应该把目标集中在startscroll()的下一句invalidate();身上。我们可以这样理解:首先在startscroll()设置好了一堆初始值,之后调用了invalidate();让view重新绘制,这里又有一个很重要的点,在draw()中会调用computescroll()这个方法!

源码太长了,在这里就不贴出来了。想看的童鞋在view类里面搜boolean draw(canvas canvas, viewgroup parent, long drawingtime)这个方法就能看到了。通过viewgroup.drawchild()方法就会调用子view的draw()方法。而在view类里面的computescroll()是一个空的方法,需要我们去实现:

/**
 * called by a parent to request that a child update its values for mscrollx
 * and mscrolly if necessary. this will typically be done if the child is
 * animating a scroll using a {@link android.widget.scroller scroller}
 * object.
 */
public void computescroll() {
}

而在上面“三部曲”的第二部中,我们就已经实现了computescroll()  。首先判断了computescrolloffset() ,我们来看看相关源码:

/**
 * call this when you want to know the new location. if it returns true,
 * the animation is not yet finished.
 */ 
public boolean computescrolloffset() {
  if (mfinished) {
    return false;
  }

  int timepassed = (int)(animationutils.currentanimationtimemillis() - mstarttime);

  if (timepassed < mduration) {
    switch (mmode) {
    case scroll_mode:
      final float x = minterpolator.getinterpolation(timepassed * mdurationreciprocal);
      mcurrx = mstartx + math.round(x * mdeltax);
      mcurry = mstarty + math.round(x * mdeltay);
      break;
    case fling_mode:
      final float t = (float) timepassed / mduration;
      final int index = (int) (nb_samples * t);
      float distancecoef = 1.f;
      float velocitycoef = 0.f;
      if (index < nb_samples) {
        final float t_inf = (float) index / nb_samples;
        final float t_sup = (float) (index + 1) / nb_samples;
        final float d_inf = spline_position[index];
        final float d_sup = spline_position[index + 1];
        velocitycoef = (d_sup - d_inf) / (t_sup - t_inf);
        distancecoef = d_inf + (t - t_inf) * velocitycoef;
      }

      mcurrvelocity = velocitycoef * mdistance / mduration * 1000.0f;
      
      mcurrx = mstartx + math.round(distancecoef * (mfinalx - mstartx));
      // pin to mminx <= mcurrx <= mmaxx
      mcurrx = math.min(mcurrx, mmaxx);
      mcurrx = math.max(mcurrx, mminx);
      
      mcurry = mstarty + math.round(distancecoef * (mfinaly - mstarty));
      // pin to mminy <= mcurry <= mmaxy
      mcurry = math.min(mcurry, mmaxy);
      mcurry = math.max(mcurry, mminy);

      if (mcurrx == mfinalx && mcurry == mfinaly) {
        mfinished = true;
      }

      break;
    }
  }
  else {
    mcurrx = mfinalx;
    mcurry = mfinaly;
    mfinished = true;
  }
  return true;
}

这个方法的返回值有讲究,若返回true则说明scroller的滑动没有结束;若返回false说明scroller的滑动结束了。再来看看内部的代码:先是计算出了已经滑动的时间,若已经滑动的时间小于总滑动的时间,则说明滑动没有结束;不然就说明滑动结束了,设置标记mfinished = true;  。而在滑动未结束里面又分为了两个mode,不过这两个mode都干了差不多的事,大致就是根据刚才的时间timepassed和插补器来计算出该时间点滚动的距离mcurrxmcurry。也就是上面“三部曲”中第二部的mscroller.getcurrx()  , mscroller.getcurry()的值。

然后在第二部曲中调用scrollto()方法滚动到指定点(即上面的mcurrx, mcurry)。之后又调用了postinvalidate(); ,让view重绘并重新调用computescroll()以此循环下去,一直到view滚动到指定位置为止,至此scroller滚动结束。

其实scroller的原理还是比较通俗易懂的。我们再来理清一下思路,以一张图的形式来终结今天的scroller解析:

总结

好了,本文介绍android中scroller的滚动原理的内容到这就结束了,如果有什么问题可以在下面留言。希望本文的内容对大家开发android能有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网