详解Android应用开发中Scroller类的屏幕滑动功能运用

时间:2021-09-15 14:20:48

今天给大家介绍下android中滑屏功能的一个基本实现过程以及原理初探,最后给大家重点讲解view视图中scrollto 与scrollby这两个函数的区别 。
 
首先 ,我们必须明白在android view视图是没有边界的,canvas是没有边界的,只不过我们通过绘制特定的view时对canvas对象进行了一定的操作,例如 : translate(平移)、cliprect(剪切)等,以便达到我们的对该canvas对象绘制的要求 ,我们可以将这种无边界的视图称为“视图坐标”-----它不受物理屏幕限制。通常我们所理解的一个layout布局文件只是该视图的显示区域,超过了这个显示区域将不能显示到父视图的区域中 ,对应的,我们可以将这种有边界的视图称为“布局坐标”父视图给子视图分配的布局(layout)大小。而且, 一个视图的在屏幕的起始坐标位于视图坐标起始处,如下图所示。
 
这么来说吧 ,世界本是无边*的,可是我们的眼睛我们的心约束了我们所看到的“世界” 。
 
如下所示:

详解Android应用开发中Scroller类的屏幕滑动功能运用

黑色框框表示该子视图的布局坐标, 褐色框框表示该子视图的视图坐标--该坐标是无限的,超过了父视图给子视图规定的区域后,不再显示该超出内容。
 
那么下面的问题就是:如何将我们的视图的任意坐标能显示到该视图的中心坐标上呢? 由于该布局位置是只能显示特定的一块视图内容 ,因此我们需要通过scrollto()或者scrollby()方法将我们期望的视图“滚动”至布局坐标上。
 
      在view.java中提供了了如下两个变量以及相应的属性方法去读取滚动值 ,如下: view.java类中  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
   * the offset, in pixels, by which the content of this view is scrolled
   * horizontally.
   * {@hide}
   */
  protected int mscrollx;  //该视图内容相当于视图起始坐标的偏移量  , x轴 方向
  /**
   * the offset, in pixels, by which the content of this view is scrolled
   * vertically.
   * {@hide}
   */
  protected int mscrolly;  //该视图内容相当于视图起始坐标的偏移量  , y轴方向
 
  /**
   * return the scrolled left position of this view. this is the left edge of
   * the displayed part of your view. you do not need to draw any pixels
   * farther left, since those are outside of the frame of your view on
   * screen.
   *
   * @return the left edge of the displayed part of your view, in pixels.
   */
  public final int getscrollx() {
    return mscrollx;
  }
 
  /**
   * return the scrolled top position of this view. this is the top edge of
   * the displayed part of your view. you do not need to draw any pixels above
   * it, since those are outside of the frame of your view on screen.
   *
   * @return the top edge of the displayed part of your view, in pixels.
   */
  public final int getscrolly() {
    return mscrolly;
  }

 
注意,所谓的“by which the content of this view is scrolled”表示该偏移量只针对于该view中ondraw()方法里的具体内容实现,而不针对绘制背景图片等 。具体原因可参考<android中view绘制流程以及invalidate()等相关方法分析>
 
提示:下文中提到的当前视图内容是在绘制在布局坐标处的内容。
 

?
1
public void scrollto(int x, int y)

说明:在当前视图内容偏移至(x , y)坐标处,即显示(可视)区域位于(x , y)坐标处。
方法原型为: view.java类中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 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;
    //回调onscrollchanged方法
    onscrollchanged(mscrollx, mscrolly, oldx, oldy);
    if (!awakenscrollbars()) {
      invalidate(); //一般都引起重绘
    }
  }
}
 
   public void scrollby(int x, int y) 

说明:在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位。方法原型为: view.java类中

?
1
2
3
4
5
6
7
8
9
10
11
/**
  * 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
  */
 // 看出原因了吧 。。 mscrollx 与 mscrolly 代表我们当前偏移的位置 , 在当前位置继续偏移(x ,y)个单位
 public void scrollby(int x, int y) {
   scrollto(mscrollx + x, mscrolly + y);
 }

        
            
第一个小demo非常简单 ,大家重点理解与掌握scrollto() 与 scrollby()函数的用法和区别。
第二个小demo则有了launcher的模样,能够左右切换屏幕 。实现功能如下: 采用了一个自定义viewgroup,该viewgroup对象包含了3个linearlayout子视图,并且以一定的布局坐标(由layout()方法指定)显示在viewgroup上。 接下来,即可调用该viewgroup对象的scrollto或者scrollby()方法切换指定视图内容了,即切换屏幕。 呵呵 ,挺好玩的吧 。

详解Android应用开发中Scroller类的屏幕滑动功能运用

自定义viewgroup如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//自定义viewgroup , 包含了三个linearlayout控件,存放在不同的布局位置,通过scrollby或者scrollto方法切换
public class multiviewgroup extends viewgroup {
 
  private context mcontext;
 
  private static string tag = "multiviewgroup";
 
  public multiviewgroup(context context) {
    super(context);
    mcontext = context;
    init();
  }
 
  public multiviewgroup(context context, attributeset attrs) {
    super(context, attrs);
    mcontext = context;
    init();
  }
 
  private void init() {
    // 初始化3个 linearlayout控件
    linearlayout onell = new linearlayout(mcontext);
    onell.setbackgroundcolor(color.red);
    addview(onell);
     
    linearlayout twoll = new linearlayout(mcontext);
    twoll.setbackgroundcolor(color.yellow);
    addview(twoll);
     
    linearlayout threell = new linearlayout(mcontext);
    threell.setbackgroundcolor(color.blue);
    addview(threell);
  }
 
  // measure过程
  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
 
    log.i(tag, "--- start onmeasure --");
 
    // 设置该viewgroup的大小
    int width = measurespec.getsize(widthmeasurespec);
    int height = measurespec.getsize(heightmeasurespec);
    setmeasureddimension(width, height);
 
    int childcount = getchildcount();
    log.i(tag, "--- onmeasure childcount is -->" + childcount);
    for (int i = 0; i < childcount; i++) {
      view child = getchildat(i);
      // 设置每个子视图的大小 , 即全屏
      child.measure(multiscreenactivity.screenwidth, multiscreenactivity.scrrenheight);
    }
  }
 
  // layout过程
  @override
  protected void onlayout(boolean changed, int l, int t, int r, int b) {
    // todo auto-generated method stub
    log.i(tag, "--- start onlayout --");
    int startleft = 0; // 每个子视图的起始布局坐标
    int starttop = 10; // 间距设置为10px 相当于 android:margintop= "10px"
    int childcount = getchildcount();
    log.i(tag, "--- onlayout childcount is -->" + childcount);
    for (int i = 0; i < childcount; i++) {
      view child = getchildat(i);
      child.layout(startleft, starttop, 
          startleft + multiscreenactivity.screenwidth, 
          starttop + multiscreenactivity.scrrenheight);
      startleft = startleft + multiscreenactivity.screenwidth ; //校准每个子view的起始布局位置
      //三个子视图的在屏幕中的分布如下 [0 , 320] / [320,640] / [640,960]
    }
  }
 
}

关于scrollto()和scrollby()以及偏移坐标的设置/取值问题

scrollto()和scrollby()这两个方法的主要作用是将view/viewgroup移至指定的坐标中,并且将偏移量保存起来。另外:
mscrollx 代表x轴方向的偏移坐标,mscrolly 代表y轴方向的偏移坐标

关于偏移量的设置我们可以参看下源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.qin.customviewgroup;
 
public class view {
  ....
  protected int mscrollx;  //该视图内容相当于视图起始坐标的偏移量  , x轴 方向  
  protected int mscrolly;  //该视图内容相当于视图起始坐标的偏移量  , y轴方向
  //返回值
  public final int getscrollx() {
    return mscrollx;
  }
  public final int getscrolly() {
    return mscrolly;
  }
  public void scrollto(int x, int y) {
    //偏移位置发生了改变
    if (mscrollx != x || mscrolly != y) {
      int oldx = mscrollx;
      int oldy = mscrolly;
      mscrollx = x; //赋新值,保存当前便宜量
      mscrolly = y;
      //回调onscrollchanged方法
      onscrollchanged(mscrollx, mscrolly, oldx, oldy);
      if (!awakenscrollbars()) {
        invalidate(); //一般都引起重绘
      }
    }
  }
  // 看出原因了吧 。。 mscrollx 与 mscrolly 代表我们当前偏移的位置 , 在当前位置继续偏移(x ,y)个单位
  public void scrollby(int x, int y) {
    scrollto(mscrollx + x, mscrolly + y);
  }
  //...
}

     于是,在任何时刻我们都可以获取该view/viewgroup的偏移位置了,即调用getscrollx()方法和getscrolly()方法

scroller类的介绍
在初次看launcher滑屏的时候,我就对scroller类的学习感到非常蛋疼,完全失去了继续研究的欲望。如今,没得办法,得重新看launcher模块,基本上将launcher大部分类以及功能给掌握了。当然,也花了一天时间来学习launcher里的滑屏实现,基本上业是拨开云雾见真知了。
    
我们知道想把一个view偏移至指定坐标(x,y)处,利用scrollto()方法直接调用就ok了,但我们不能忽视的是,该方法本身来的的副作用:非常迅速的将view/viewgroup偏移至目标点,而没有对这个偏移过程有任何控制,对用户而言可能是不太友好的。于是,基于这种偏移控制,scroller类被设计出来了,该类的主要作用是为偏移过程制定一定的控制流程(后面我们会知道的更多),从而使偏移更流畅,更完美。
  
可能上面说的比较悬乎,道理也没有讲透。下面我就根据特定情景帮助大家分析下:

情景: 从上海如何到武汉?
普通的人可能会想,so easy : 飞机、轮船、11路公交车...
文艺的人可能会想,  小 case : 时空忍术(火影的招数)、翻个筋斗(孙大圣的招数)...

不管怎么样,我们想出来的套路可能有两种:
1、有个时间控制过程才能抵达(缓慢的前进)、对应于scroller的作用
假设做火车,这个过程可能包括: 火车速率,花费周期等;
2、瞬间抵达(超神太快了,都眩晕了,用户体验不太好)、对应于scrollto()的作用
 
模拟scroller类的实现功能:

假设从上海做动车到武汉需要10个小时,行进距离为1000km ,火车速率200/h 。采用第一种时间控制方法到达武汉的整个配合过程可能如下:我们每隔一段时间(例如1小时),计算火车应该行进的距离,然后调用scrollto()方法,行进至该处。10小时过完后,我们也就达到了目的地了。相信大家心里应该有个感觉了。我们就分析下源码里去看看scroller类的相关方法.
其源代码(部分)如下:
路径位于 \frameworks\base\core\java\android\widget\scroller.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class scroller {
 
  private int mstartx;  //起始坐标点 , x轴方向
  private int mstarty;  //起始坐标点 , y轴方向
  private int mcurrx;   //当前坐标点 x轴, 即调用startscroll函数后,经过一定时间所达到的值
  private int mcurry;   //当前坐标点 y轴, 即调用startscroll函数后,经过一定时间所达到的值
   
  private float mdeltax; //应该继续滑动的距离, x轴方向
  private float mdeltay; //应该继续滑动的距离, y轴方向
  private boolean mfinished; //是否已经完成本次滑动操作, 如果完成则为 true
 
  //构造函数
  public scroller(context context) {
    this(context, null);
  }
  public final boolean isfinished() {
    return mfinished;
  }
  //强制结束本次滑屏操作
  public final void forcefinished(boolean finished) {
    mfinished = finished;
  }
  public final int getcurrx() {
    return mcurrx;
  }
   /* call this when you want to know the new location. if it returns true,
   * the animation is not yet finished. loc will be altered to provide the
   * new location. */
  //根据当前已经消逝的时间计算当前的坐标点,保存在mcurrx和mcurry值中
  public boolean computescrolloffset() {
    if (mfinished) { //已经完成了本次动画控制,直接返回为false
      return false;
    }
    int timepassed = (int)(animationutils.currentanimationtimemillis() - mstarttime);
    if (timepassed < mduration) {
      switch (mmode) {
      case scroll_mode:
        float x = (float)timepassed * mdurationreciprocal;
        ...
        mcurrx = mstartx + math.round(x * mdeltax);
        mcurry = mstarty + math.round(x * mdeltay);
        break;
      ...
    }
    else {
      mcurrx = mfinalx;
      mcurry = mfinaly;
      mfinished = true;
    }
    return true;
  }
  //开始一个动画控制,由(startx , starty)在duration时间内前进(dx,dy)个单位,即到达坐标为(startx+dx , starty+dy)出
  public void startscroll(int startx, int starty, int dx, int dy, int duration) {
    mfinished = false;
    mduration = duration;
    mstarttime = animationutils.currentanimationtimemillis();
    mstartx = startx;    mstarty = starty;
    mfinalx = startx + dx; mfinaly = starty + dy;
    mdeltax = dx;      mdeltay = dy;
    ...
  }
}

其中比较重要的两个方法为:

?
1
public void startscroll(int startx, int starty, int dx, int dy, int duration)

函数功能说明:根据当前已经消逝的时间计算当前的坐标点,保存在mcurrx和mcurry值中

?
1
public void startscroll(int startx, int starty, int dx, int dy, int duration)

函数功能说明:开始一个动画控制,由(startx , starty)在duration时间内前进(dx,dy)个单位,到达坐标为 (startx+dx , starty+dy)处。

ps : 强烈建议大家看看该类的源码,便于后续理解。

computescroll()方法介绍

为了易于控制滑屏控制,android框架提供了 computescroll()方法去控制这个流程。在绘制view时,会在draw()过程调用该方法。因此, 再配合使用scroller实例,我们就可以获得当前应该的偏移坐标,手动使view/viewgroup偏移至该处。

?
1
2
3
4
5
6
7
8
9
10
   computescroll()方法原型如下,该方法位于viewgroup.java类中  
/**
   * 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.
   */由父视图调用用来请求子视图根据偏移值 mscrollx,mscrolly重新绘制
  public void computescroll() { //空方法 ,自定义viewgroup必须实现方法体
     
  }

为了实现偏移控制,一般自定义view/viewgroup都需要重载该方法 。其调用过程位于view绘制流程draw()过程中,如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@override protected void dispatchdraw(canvas canvas){
  ...
   
  for (int i = 0; i < count; i++) {
    final view child = children[getchilddrawingorder(count, i)];
    if ((child.mviewflags & visibility_mask) == visible || child.getanimation() != null) {
      more |= drawchild(canvas, child, drawingtime);
    }
  }
}
protected boolean drawchild(canvas canvas, view child, long drawingtime) {
  ...
  child.computescroll();
  ...
}

demo说明:
我们简单的复用了之前写的一个自定义viewgroup,与以前一次有区别的是,我们没有调用scrollto()方法去进行瞬间偏移。 本次做法如下:
第一、调用scroller实例去产生一个偏移控制(对应于startscroll()方法)
第二、手动调用invalid()方法去重新绘制,剩下的就是在 computescroll()里根据当前已经逝去的时间,获取当前应该偏移的坐标(由scroller实例对应的computescrolloffset()计算而得),
第三、当前应该偏移的坐标,调用scrollby()方法去缓慢移动至该坐标处。

详解Android应用开发中Scroller类的屏幕滑动功能运用详解Android应用开发中Scroller类的屏幕滑动功能运用

附:由于滑动截屏很难,只是简单的截取了两个个静态图片,触摸的话可以实现左右滑动切屏了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//自定义viewgroup , 包含了三个linearlayout控件,存放在不同的布局位置,通过scrollby或者scrollto方法切换
public class multiviewgroup extends viewgroup {
  ...
  //startscroll开始移至下一屏
  public void startmove(){
    curscreen ++ ;
    log.i(tag, "----startmove---- curscreen " + curscreen);
     
    //使用动画控制偏移过程 , 3s内到位
    mscroller.startscroll((curscreen-1) * getwidth(), 0, getwidth(), 0,3000);
    //其实点击按钮的时候,系统会自动重新绘制view,我们还是手动加上吧。
    invalidate();
    //使用scrollto一步到位
    //scrollto(curscreen * multiscreenactivity.screenwidth, 0);
  }
  // 由父视图调用用来请求子视图根据偏移值 mscrollx,mscrolly重新绘制
  @override
  public void computescroll() {  
    // todo auto-generated method stub
    log.e(tag, "computescroll");
    // 如果返回true,表示动画还没有结束
    // 因为前面startscroll,所以只有在startscroll完成时 才会为false
    if (mscroller.computescrolloffset()) {
      log.e(tag, mscroller.getcurrx() + "======" + mscroller.getcurry());
      // 产生了动画效果,根据当前值 每次滚动一点
      scrollto(mscroller.getcurrx(), mscroller.getcurry());
       
      log.e(tag, "### getleft is " + getleft() + " ### getright is " + getright());
      //此时同样也需要刷新view ,否则效果可能有误差
      postinvalidate();
    }
    else
      log.i(tag, "have done the scoller -----");
  }
  //马上停止移动,如果已经超过了下一屏的一半,我们强制滑到下一个屏幕
  public void stopmove(){
     
    log.v(tag, "----stopmove ----");
     
    if(mscroller != null){
      //如果动画还没结束,我们就按下了结束的按钮,那我们就结束该动画,即马上滑动指定位置
      if(!mscroller.isfinished()){
         
        int scrollcurx= mscroller.getcurrx() ;
        //判断是否超过下一屏的中间位置,如果达到就抵达下一屏,否则保持在原屏幕
        // 这样的一个简单公式意思是:假设当前滑屏偏移值即 scrollcurx 加上每个屏幕一半的宽度,除以每个屏幕的宽度就是
        // 我们目标屏所在位置了。 假如每个屏幕宽度为320dip, 我们滑到了500dip处,很显然我们应该到达第二屏,索引值为1
        //即(500 + 320/2)/320 = 1
        int descscreen = ( scrollcurx + getwidth() / 2) / getwidth() ;
         
        log.i(tag, "-mscroller.is not finished scrollcurx +" + scrollcurx);
        log.i(tag, "-mscroller.is not finished descscreen +" + descscreen);
        mscroller.abortanimation();
 
        //停止了动画,我们马上滑倒目标位置
        scrollto(descscreen *getwidth() , 0);
        curscreen = descscreen ; //纠正目标屏位置
      }
      else
        log.i(tag, "----ok mscroller.is finished ---- ");
    }  
  }
  ...
}

如何实现触摸滑屏?

其实网上有很多关于launcher实现滑屏的博文,基本上也把道理阐释的比较明白了 。我这儿也是基于自己的理解,将一些
重要方面的知识点给补充下,希望能帮助大家理解。想要实现滑屏操作,值得考虑的事情包括如下几个方面:

其中:onintercepttouchevent()主要功能是控制触摸事件的分发,例如是子视图的点击事件还是滑动事件。其他所有处理过程均在ontouchevent()方法里实现了。
1、屏幕的滑动要根据手指的移动而移动  ---- 主要实现在ontouchevent()方法中
2、当手指松开时,可能我们并没有完全滑动至某个屏幕上,这是我们需要手动判断当前偏移至去计算目标屏(当前屏或者前后屏),并且优雅的偏移到目标屏(当然是用scroller实例咯)。
3、调用computescroll ()去实现缓慢移动过程。
 
知识点介绍:             
velocitytracker类
功能:  根据触摸位置计算每像素的移动速率。
常用方法有:    
public void addmovement (motionevent ev)
功能:添加触摸对象motionevent , 用于计算触摸速率。  
public void computecurrentvelocity (int units)
功能:以每像素units单位考核移动速率。额,其实我也不太懂,赋予值1000即可。
参照源码 该units的意思如下:
参数 units : the units you would like the velocity in.  a value of 1
provides pixels per millisecond, 1000 provides pixels per second, etc.
public float getxvelocity ()
功能:获得x轴方向的移动速率。
viewconfiguration类
功能: 获得一些关于timeouts(时间)、sizes(大小)、distances(距离)的标准常量值 。
常用方法:
public int getscalededgeslop()
说明:获得一个触摸移动的最小像素值。也就是说,只有超过了这个值,才代表我们该滑屏处理了。
public static int getlongpresstimeout()
说明:获得一个执行长按事件监听(onlongclicklistener)的值。也就是说,对某个view按下触摸时,只有超过了这个时间值在,才表示我们该对该view回调长按事件了;否则,小于这个时间点松开手指,只执行onclick监听
 
 
我能写下来的也就这么多了,更多的东西参考代码注释吧。 在掌握了上面我罗列的知识后(重点scrollto、scroller类),其他方面的知识都是关于点与点之间的计算了以及触摸事件的分发了。这方面感觉也没啥可写的。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//自定义viewgroup , 包含了三个linearlayout控件,存放在不同的布局位置,通过scrollby或者scrollto方法切换
public class multiviewgroup extends viewgroup {
 
  private static string tag = "multiviewgroup";
   
  private int curscreen = 0 ; //当前屏幕
  private scroller mscroller = null ; //scroller对象实例
   
  public multiviewgroup(context context) {
    super(context);
    mcontext = context;
    init();
  }
  public multiviewgroup(context context, attributeset attrs) {
    super(context, attrs);
    mcontext = context;
    init();
  }
  //初始化
  private void init() {  
    ...
    //初始化scroller实例
    mscroller = new scroller(mcontext);
    // 初始化3个 linearlayout控件
    ...
    //初始化一个最小滑动距离
    mtouchslop = viewconfiguration.get(getcontext()).getscaledtouchslop();
  }
  // 由父视图调用用来请求子视图根据偏移值 mscrollx,mscrolly重新绘制
  @override
  public void computescroll() {  
    // todo auto-generated method stub
    log.e(tag, "computescroll");
    // 如果返回true,表示动画还没有结束
    // 因为前面startscroll,所以只有在startscroll完成时 才会为false
    if (mscroller.computescrolloffset()) {
      log.e(tag, mscroller.getcurrx() + "======" + mscroller.getcurry());
      // 产生了动画效果,根据当前值 每次滚动一点
      scrollto(mscroller.getcurrx(), mscroller.getcurry());
       
      log.e(tag, "### getleft is " + getleft() + " ### getright is " + getright());
      //此时同样也需要刷新view ,否则效果可能有误差
      postinvalidate();
    }
    else
      log.i(tag, "have done the scoller -----");
  }
  //两种状态: 是否处于滑屏状态
  private static final int touch_state_rest = 0; //什么都没做的状态
  private static final int touch_state_scrolling = 1; //开始滑屏的状态
  private int mtouchstate = touch_state_rest; //默认是什么都没做的状态
  //-------------------------- 
  //处理触摸事件 ~
  public static int snap_velocity = 600 ; //最小的滑动速率
  private int mtouchslop = 0 ;       //最小滑动距离,超过了,才认为开始滑动
  private float mlastionmotionx = 0 ;    //记住上次触摸屏的位置
  //处理触摸的速率
  private velocitytracker mvelocitytracker = null ;
   
  // 这个感觉没什么作用 不管true还是false 都是会执行ontouchevent的 因为子view里面ontouchevent返回false了
  @override
  public boolean onintercepttouchevent(motionevent ev) {
    // todo auto-generated method stub
    log.e(tag, "onintercepttouchevent-slop:" + mtouchslop);
 
    final int action = ev.getaction();
    //表示已经开始滑动了,不需要走该action_move方法了(第一次时可能调用)。
    //该方法主要用于用户快速松开手指,又快速按下的行为。此时认为是出于滑屏状态的。
    if ((action == motionevent.action_move) && (mtouchstate != touch_state_rest)) {
      return true;
    }
     
    final float x = ev.getx();
    final float y = ev.gety();
 
    switch (action) {
    case motionevent.action_move:
      log.e(tag, "onintercepttouchevent move");
      final int xdiff = (int) math.abs(mlastionmotionx - x);
      //超过了最小滑动距离,就可以认为开始滑动了
      if (xdiff > mtouchslop) {
        mtouchstate = touch_state_scrolling;
      }
      break;
 
    case motionevent.action_down:
      log.e(tag, "onintercepttouchevent down");
      mlastionmotionx = x;
      mlastmotiony = y;
      log.e(tag, mscroller.isfinished() + "");
      mtouchstate = mscroller.isfinished() ? touch_state_rest : touch_state_scrolling;
 
      break;
 
    case motionevent.action_cancel:
    case motionevent.action_up:
      log.e(tag, "onintercepttouchevent up or cancel");
      mtouchstate = touch_state_rest;
      break;
    }
    log.e(tag, mtouchstate + "====" + touch_state_rest);
    return mtouchstate != touch_state_rest;
  }
  public boolean ontouchevent(motionevent event){
 
    super.ontouchevent(event);
     
    log.i(tag, "--- ontouchevent--> " );
 
    // todo auto-generated method stub
    log.e(tag, "ontouchevent start");
    //获得velocitytracker对象,并且添加滑动对象
    if (mvelocitytracker == null) {
      mvelocitytracker = velocitytracker.obtain();
    }
    mvelocitytracker.addmovement(event);
     
    //触摸点
    float x = event.getx();
    float y = event.gety();
    switch(event.getaction()){
    case motionevent.action_down:
      //如果屏幕的动画还没结束,你就按下了,我们就结束上一次动画,即开始这次新action_down的动画
      if(mscroller != null){
        if(!mscroller.isfinished()){
          mscroller.abortanimation(); 
        }
      }
      mlastionmotionx = x ; //记住开始落下的屏幕点
      break ;
    case motionevent.action_move:
      int detax = (int)(mlastionmotionx - x ); //每次滑动屏幕,屏幕应该移动的距离
      scrollby(detax, 0);//开始缓慢滑屏咯。 detax > 0 向右滑动 , detax < 0 向左滑动 ,
       
      log.e(tag, "--- motionevent.action_move--> detax is " + detax );
      mlastionmotionx = x ;
      break ;
    case motionevent.action_up:
       
      final velocitytracker velocitytracker = mvelocitytracker ;
      velocitytracker.computecurrentvelocity(1000);
      //计算速率
      int velocityx = (int) velocitytracker.getxvelocity() ; 
      log.e(tag , "---velocityx---" + velocityx);
       
      //滑动速率达到了一个标准(快速向右滑屏,返回上一个屏幕) 马上进行切屏处理
      if (velocityx > snap_velocity && curscreen > 0) {
        // fling enough to move left
        log.e(tag, "snap left");
        snaptoscreen(curscreen - 1);
      }
      //快速向左滑屏,返回下一个屏幕)
      else if(velocityx < -snap_velocity && curscreen < (getchildcount()-1)){
        log.e(tag, "snap right");
        snaptoscreen(curscreen + 1);
      }
      //以上为快速移动的 ,强制切换屏幕
      else{
        //我们是缓慢移动的,因此先判断是保留在本屏幕还是到下一屏幕
        snaptodestination();
      }
      //回收velocitytracker对象
      if (mvelocitytracker != null) {
        mvelocitytracker.recycle();
        mvelocitytracker = null;
      }
      //修正mtouchstate值
      mtouchstate = touch_state_rest ;
       
      break;
    case motionevent.action_cancel:
      mtouchstate = touch_state_rest ;
      break;
    }
     
    return true ;
  }
  ////我们是缓慢移动的,因此需要根据偏移值判断目标屏是哪个?
  private void snaptodestination(){
    //当前的偏移位置
    int scrollx = getscrollx() ;
    int scrolly = getscrolly() ;
     
    log.e(tag, "### ontouchevent snaptodestination ### scrollx is " + scrollx);
    //判断是否超过下一屏的中间位置,如果达到就抵达下一屏,否则保持在原屏幕  
    //直接使用这个公式判断是哪一个屏幕 前后或者自己
    //判断是否超过下一屏的中间位置,如果达到就抵达下一屏,否则保持在原屏幕
    // 这样的一个简单公式意思是:假设当前滑屏偏移值即 scrollcurx 加上每个屏幕一半的宽度,除以每个屏幕的宽度就是
    // 我们目标屏所在位置了。 假如每个屏幕宽度为320dip, 我们滑到了500dip处,很显然我们应该到达第二屏
    int destscreen = (getscrollx() + multiscreenactivity.screenwidth / 2 ) / multiscreenactivity.screenwidth ;
     
    log.e(tag, "### ontouchevent action_up### dx destscreen " + destscreen);
     
    snaptoscreen(destscreen);
  }
  //真正的实现跳转屏幕的方法
  private void snaptoscreen(int whichscreen){ 
    //简单的移到目标屏幕,可能是当前屏或者下一屏幕
    //直接跳转过去,不太友好
    //scrollto(mlastscreen * multiscreenactivity.screenwidth, 0);
    //为了友好性,我们在增加一个动画效果
    //需要再次滑动的距离 屏或者下一屏幕的继续滑动距离
     
    curscreen = whichscreen ;
    //防止屏幕越界,即超过屏幕数
    if(curscreen > getchildcount() - 1)
      curscreen = getchildcount() - 1 ;
    //为了达到下一屏幕或者当前屏幕,我们需要继续滑动的距离.根据dx值,可能想左滑动,也可能像又滑动
    int dx = curscreen * getwidth() - getscrollx() ;
     
    log.e(tag, "### ontouchevent action_up### dx is " + dx);
     
    mscroller.startscroll(getscrollx(), 0, dx, 0,math.abs(dx) * 2);
     
    //由于触摸事件不会重新绘制view,所以此时需要手动刷新view 否则没效果
    invalidate();
  }
  //开始滑动至下一屏
  public void startmove(){
    ...   
  }
  //理解停止滑动
  public void stopmove(){
    ...
  }
  // measure过程
  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    ...
  }
  // layout过程
  @override
  protected void onlayout(boolean changed, int l, int t, int r, int b) {
    ...
  }
}