天天看点

Android的滑动分析以及各种实现

【引自mrxi的博客】一、滑动效果的产生

滑动一个view,本质区别就是移动一个view。改变当前view所在的坐标,原理和动画相似不断改变坐标位置实现。实现view的滑动就必须监听滑动的事件,并且根据事件传入的坐标,动态且不断改变view的坐标,从而实现view跟随用户触摸的滑动而滑动。

(1)、android的坐标系

android中将屏幕最左上角的顶点作为android坐标系的原点,从这个点向右是x轴正方向,从这个点向下是y轴正方向,如下图:

Android的滑动分析以及各种实现

系统提供了getlocationonscreen(int location[])这样的方法来获取android坐标系中点的位置,即该视图左上角在android坐标系中的坐标。在触控事件中使用getrawx()、getrawy()方法所获得的坐标同样是android坐标系中的坐标。

(2)、视图坐标系

android中除了上面所说的这种坐标系之外,还有一个视图坐标系,它描述了子视图在父视图中的位置关系。这两种坐标系并不矛盾也不复杂,他们的作用是相互相成的。与android坐标系类似,视图坐标系同样是以原点向右为x轴正方向,以原点向下为y轴正方向,只不过在视图坐标系中,原点不再是android坐标系中的屏幕最左上角,而是以父视图左上角为坐标原点,如下图:

Android的滑动分析以及各种实现

在触控事件中,通过getx()、gety()所获得的坐标系就是视图坐标系中的坐标。

(3)、触控事件——motionevent

触控事件motionevent在用户交互中,占着举足轻重的地位。首先看看motionevent封装的一些常用事件常量,定义了触控事件的不同类型。

//单点触摸按下动作 

public static final int action_down             = 0; 

//单点触摸离开动作 

public static final int action_up               = 1; 

//触摸点移动动作 

public static final int action_move             = 2; 

//触摸动作取消 

public static final int action_cancel           = 3; 

//触摸动作超出边界 

public static final int action_outside          = 4; 

//多点触摸按下动作 

public static final int action_pointer_down     = 5; 

//多点离开动作 

public static final int action_pointer_up       = 6;  

通常情况会在ontouchevent(motionevent event)方法中通过event.getaction()方法来获取触控事件的类型,并使用switch-case方法来进行筛选,这个代码的模式基本固定:

@override 

public boolean ontouchevent(motionevent event) { 

    //获取当前输入点的x、y坐标(视图坐标) 

    int x = (int) event.getx(); 

    int y = (int) event.gety(); 

    switch (event.getaction()) { 

        case motionevent.action_down: 

            //处理按下事件 

            break; 

        case motionevent.action_move: 

            //处理移动事件 

        case motionevent.action_up: 

            //处理离开事件 

    } 

    return true; 

}  

在不涉及多点操作的情况下,通常可以使用以上代码来完成触控事件的监听。

在android中系统提供了非常多的方法来获取坐标值、相对距离等。方法丰富固然好,下面对坐标系的api进行总结,如下图:

Android的滑动分析以及各种实现

这些方法可以分为如下两个类别:

view提供的获取坐标方法

gettop():获取到的是view自身的顶边到其父布局顶边的距离。

getleft():获取到的是view自身的左边到其父布局最左边的距离。

getright():获取到的是view自身的右边到其父布局左边的距离。

getbottom():获取到的是view自身的底边到其父布局顶边的距离。

motionevent提供的方法

getx():获取点击事件距离空间左边的距离,即视图坐标。

gety():获取点击事件距离控件顶边的距离,即视图坐标。

getrawx():获取点击事件距离整个屏幕左边的距离,即绝对坐标。

getrawy():获取点击事件距离整个屏幕顶边的距离,即绝对坐标。

二、实现滑动的七种方式

当了解android坐标系和触控事件后,我们再来看看如何使用系统提供的api来实现动态地修改一个view坐标,即实时滑动效果。而不管采用哪一种方式,其实现的思想基本是一致的,当触摸view时,系统记下当前触摸点坐标,当手指移动时,系统记下移动后的触摸点坐标,从而获取到相对于前一次坐标点的偏移量,并通过偏移量来修改view的坐标,这样不断重复,实现滑动过程。

通过一个实例看看android中该如何实现滑动效果,定义一个view,处于linearlayout中,实现一个简单布局:

<?xml version="1.0" encoding="utf-8"?> 

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="match_parent" 

android:layout_height="match_parent" 

android:orientation="vertical"> 

<com.xjf.drawview.dragview1 

android:layout_width="100dp" 

android:layout_height="100dp" /> 

</linearlayout>  

我们的目的就是让这个自定义的view随着手指在屏幕上的滑动而滑动。初始化时显示效果:

Android的滑动分析以及各种实现

(1)、layout方法

在view绘制时,会调用onlayout()方法来设置显示的位置。同样,可以通过修改view的left,top,right,bottom四个属性来控制view的坐标。与前面提供的模板代码一样,在每次回调ontouchevent的时候,我们都来获取一下触摸点的坐标,代码如下:

//获取当前输入点的x、y坐标(视图坐标) 

int x = (int) event.getx(); 

int y = (int) event.gety();  

接着,在action_down事件中记录触摸点的坐标,如下:

case motionevent.action_down: 

// 记录触摸点坐标 

lastx = x; 

lasty = y; 

break;  

最后,可以在action_move事件中计算偏移量,并将偏移量作用到layout方法中,在目前layout的left,top,right,bottom基础上,增加计算出来的偏移量,代码如下所示:

case motionevent.action_move: 

// 计算偏移量 

int offsetx = x - lastx; 

int offsety = y - lasty; 

// 在当前left、top、right、bottom的基础上加上偏移量 

layout(getleft() + offsetx, 

gettop() + offsety, 

getright() + offsetx, 

getbottom() + offsety); 

这样没错移动后,view都会调用layout方法来对自己重新布局,从而达到移动view的效果。

上面的代码中,使用的是getx()、gety()方法来获取坐标值,即通过视图坐标来获取偏移量。当然,同样可以使用getrawx()、getrawy()来获取坐标,并使用绝对坐标来计算偏移量,代码如下:

// 视图坐标方式 

int x = (int) event.getrawx(); 

int y = (int) event.getrawy(); 

switch (event.getaction()) { 

break; 

//重新设置初始化坐标 

return true; 

使用绝对坐标系,有一点非常需要注意的地方,就是在每次执行完action_move的逻辑后,一定要重新设置初始化坐标,这样才能准确地获取偏移量。

(2)、offsetleftandright()与offsettopandbottom()

这个方法相当于系统提供的一个对左右、上下移动的api的封装。当计算出偏移量后,只需要使用如下代码就可以完成view的重新布局,效果与使用layout方法一样,代码如下所示:

//同时对left和right进行偏移 

offsetleftandright(offsetx); 

//同时对top和bottom进行偏移 

offsettopandbottom(offsety);  

这里的offsetx、offsety与在layout方法中计算offset方法一样。

(3)、layoutparams

layoutparams保存了一个view的布局参数。因此可以在程序中,通过改变layoutparams来动态地修改一个布局的位置参数,从而达到改变view位置的效果。我们可以很方便在程序中使用getlayoutparams()来获取一个view的layoutparams。当然,计算偏移量的方法与在layout方法中计算offset也是一样。当获取到偏移量之后,就可以通过setlayoutparams来改变其layoutparams,代码如下:

linearlayout.layoutparams layoutparams = (linearlayout.layoutparams) getlayoutparams(); 

layoutparams.leftmargin = getleft() + offsetx; 

layoutparams.topmargin = gettop() + offsety; 

setlayoutparams(layoutparams);  

这里getlayoutparams()获取layoutparams时,需要根据view所在view父布局的类型来设置不同的类型,比如这里将view放在linearlayout中,那么就可以使用linearlayout.layoutparams。如果在relativelayout中,就要使用relativelayout.layoutparams。这一切的前提是你必须要有一个父布局,不然系统无法获取layoutparams。

在通过改变layoutparams来改变一个view的位置时,通常改变的是这个view的margin属性,所以除了使用布局的layoutparams之外,还可以使用viewgroup.marginlayoutparams来实现这一一个功能,代码:

viewgroup.marginlayoutparams layoutparams = (viewgroup.marginlayoutparams) getlayoutparams(); 

我们可以发现,使用viewgroup.marginlayoutparams更加的方便,不需要考虑父布局的类型,当然它们的本质都是一样。

(4)、scrollto与scrollby

在一个view中,系统提供了scrollto、scrollby两种方式来改变一个view的位置。这两个方法的区别非常好理解,与英文中to与by的区别类似,scrollto(x,y)表示移动到一个具体的坐标点(x,y),而scrollby(dx,dy)表示移动的增量为dx,dy。

与前面几种方式相同,在获取偏移量后使用scrollby来移动view,代码如下:

scrollby(offsetx, offsety);  

但是,当我们拖动view的时候,你会发现view并没有移动,其实方法没错,view确实移动了,只是移动的并不是我们想要的东西。scrollto、scrollby方法移动的是view的content,即让view的内容移动,如果在viewgroup中使用scrollto、scrollby方法,那么移动的将是所有子view,如果在view中使用,那么移动的将是view的内容,例如textview,content就是它的文本,imageview,content就是它的drawable对象。

通过以上的分析,现在知道为什么不能再view中使用这两个方法来拖动这个view了。那么我们就该view所在的viewgroup中来使用scrollby方法,移动它的子view,代码如下:

((view) getparent()).scrollby(offsetx, offsety); 

但是再次拖动view的时候,你会发现view虽然移动了,但却在乱动,并不是我们想要的跟随触摸点的移动而移动。这里先看一下视图移动,不妨这样想象一下手机屏幕是一个中空的盖板,盖板下面是一个巨大的画布,也就是我们想要显示的视图。当把这个盖板盖在画布上的某一处时,透过中间空的矩形,我们看见了手机屏幕上显示的视图,而画布上其他地方的视图,则被盖板盖住了无法看见。我们的视图与这个例子非常类似,我们没有看见视图,并不代表它就不存在,有可能只是在屏幕外面而已。当调用scrollby方法时,可以想象为外面的盖板在移动,这么说比较抽象。

下图一中间的矩形相当于屏幕,及可视区域。后面的content就相当于画布,代表视图。可以看到,只有视图的中间部分目前是可视的,其他部分都不可见。在可见区域中,我们设置了一个button,它的坐标为(20,10)。

下面使用scrollby方法,将盖板(屏幕、可视区域),在水平方向上向x轴正方向(向右)平移20,在竖直方向上向y轴正方向(下方)平移10,那么平移之后的可视区域如图二。

Android的滑动分析以及各种实现

图一

Android的滑动分析以及各种实现

图二、移动之后的可视区域

我们发现,虽然设置scrollby(20,10),偏移量均为x轴、y轴正方向上的正数,但是在屏幕的可视区域内,button却向x轴、y轴负方向上移动了。这就是因为参考系选择的不同,而产生的不同效果。

通过上面的分析可以发现,如果讲scrollby中的参数dx和dy设置为正数,那么content讲向坐标轴负方向移动,如果将scrollby中的参数dx和dy设置为负数,那么content将向坐标轴正方向移动,因此回到前面的例子,要实现跟随着手指移动而滑动的效果,就必须将偏移量改为负值,代码如下:

((view) getparent()).scrollby(-offsetx, -offsety);  

现在在运行一次发现和前面几种方式效果相同了,类似地使用绝对坐标时,也可以通过使用scrollto发方法来实现这一效果。

(5)、scroller

前面提到了scrollby、scrollto方法,就不得不再来说一说scroller类。scroller类与scrollby、scrollto方法十分相似。什么区别?先看例子,如果要完成这样一个效果;通过点击按钮,让一个viewgroup的子view向右移动100个像素。问题看起来很简单,只要在按钮的点击事件中使用前面的scrollby方法设置下偏移量就可以了吗?确实这样可以让一个子viewgroup中的子view平移,但是不管使用scrollby还是scrollto方法,子view的平移都是瞬间发生的,在事件执行的时候平移就已经完成了,这样的效果会让人感觉非常突然,google建议使用自然的过度动画来实现移动效果。因此scroller类就这样诞生了,通过scroller类可以实现平滑移动的效果,而不是瞬间就完成移动。

scroller类的实现原理,其实它与前面使用的scrollto和scrollby方法来实现子view跟随手指移动的原理基本类似,虽然scrollby芳芳法是让子view瞬间从某点移动到另一个点,但是由于在action_move事件中不断获取手指移动的微小的偏移量,这样就将一段距离划分成了n个非常小的偏移量。虽然每个偏移量里面,通过scrollby方法进行了瞬间移动,但是在整体上却可以获得一个平滑移动的效果。这个原理与动画的实现原理也是基本类似的,它们都是利用了人眼的视觉暂留特性。

下面我们使用scroller类实现平滑移动,在这个实例中,同样让子view跟随手指的滑动而滑动,但是在手指离开屏蔽时,让子view平滑的移动到初始化位置,即屏幕左上角。使用scroller类需要如下三个步骤:

初始化scroller

首先通过它的构造方法来创建一个scroller对象,代码如下所示:

// 初始化scroller 

mscroller = new scroller(context);  

重写computerscroller方法,实现模拟滑动

下面我们需要重写computerscroller()芳芳法,它是使用scroller类的核心,系统在绘制view的时候会在draw()方法中调用该方法。这个方法实际就是使用的scrollto方法。再结合scroller对象,帮助获取到当前滚动值。我们可以通过不断地瞬间移动一个小的距离来实现整体上的平滑移动效果。代码如下:

public void computescroll() { 

    super.computescroll(); 

    // 判断scroller是否执行完毕 

    if (mscroller.computescrolloffset()) { 

        ((view) getparent()).scrollto( 

                mscroller.getcurrx(), 

                mscroller.getcurry()); 

        // 通过重绘来不断调用computescroll 

        invalidate(); 

scroller类提供了computescrolloffset()方法来判断是否完成了整个滑动,同时也提供了getcurrx()、getcurry()方法来获得当前的滑动坐标。在上面的代码中,唯一需要注意的是invalidate()方法,因为只能在computescroller()方法中获取模拟过程中的scrollx和scrolly坐标。但computescroll()方法是不会自动调用的,只能通过invalidate()->draw()->computescroll()来间接调用compuetscroll()方法,所以需要在compuetscroll()方法中调用invalidate()方法,实现循环获取scrollx和scrolly的目的。而当模拟过程结束后,scroller.compuetscrolloffset()方法会返回false,而中断循环,完成平滑移动过程。

startscroll开启模拟过程

我们在需要使用平滑移动的事件中,使用scroller类的startscroll()方法来开启平滑移动过程。startscroll()方法具有两个重载方法。

public void startscroll(int startx, int starty, int dx, int dy)  

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

可以看到它们的区别就是一个具有指定的支持时长,而另一个没有。很好理解,与在动画中设置duration和使用默认的显示时长是一个道理。其他四个坐标,则与他们的命名含义相同,就是起始坐标与偏移量。在获取坐标时,通常可以使用getscrollx()和getscrolly()方法来获取父视图中content所滑动到的点的坐标,需要注意的是这个值的正负,它与在scrollby、scrollto中讲解的情况是一样的。

根据以上三步,就可以使用scroller类实现平滑移动,在构造方法中初始化scroller对象,重写view的computerscroll()方法,最后监听手指离开屏蔽的事件,并在该事件中调用startscroll()方法完成平滑移动。监听手指离开屏幕的事件,只需要在ontouchevent中增加一个action_up监听选项即可,代码如下所示:

case motionevent.action_up: 

    // 手指离开时,执行滑动过程 

    view viewgroup = ((view) getparent()); 

    mscroller.startscroll( 

            viewgroup.getscrollx(), 

            viewgroup.getscrolly(), 

            -viewgroup.getscrollx(), 

            -viewgroup.getscrolly()); 

    invalidate(); 

    break;  

在startscroll()方法中我们获取子view移动的距离-getscrollx()、getscrolly(),并将偏移量设置为其相反数,从而将子view滑动到原位置。这里的invalidate()方法是用来通知view进行重绘,调用computescroll()的模拟过程。当然,也可以给startscroll()方法增加一个duration的参数来设置滑动的持续时长。

(6)、属性动画

属性动画请参见我的另一篇:android全套动画使用技巧

(7)、viewdraghelper

google在其support库中为我们提供了drawerlayout和slidingpanelayout两个布局来帮助开发者实现侧边栏滑动的效果。这两个新的布局方便我们创建自己的滑动布局界面,在这两个强大布局背后有一个功能强大的类——viewdraghelper。通过viewdraghelper,基本可以实现各种不同的滑动、拖放需求,因此这个方法也是各种滑动解决方案中的终结绝招。

下面演示一个使用viewdraghelper创建一个qq侧边栏滑动的布局,如图:

Android的滑动分析以及各种实现

图三

Android的滑动分析以及各种实现

图四

初始化viewdraghelper

首先需要初始化viewdraghelper,viewdraghelper通常定义在一个viewgroup的内部,通过静态工厂方法进行初始化,代码如下:

mviewdraghelper = viewdraghelper.create(this, callback); 

第一个参数监听的view,通常需要一个viewgroup,即parentview;第二个参数是一个callback回调,这个回调就是整个viewdraghelper的逻辑核心。

拦截事件

重写拦截事件,将事件传递给viewdraghelper进行处理;

public boolean onintercepttouchevent(motionevent ev) { 

return mviewdraghelper.shouldintercepttouchevent(ev); 

//将触摸事件传递给viewdraghelper,此操作必不可少 

mviewdraghelper.processtouchevent(event); 

处理computescroll()

使用viewdraghelper同样需要重写computescroll()方法,因为viewdraghelper内部也是通过scroller来实现平滑移动的。

    if (mviewdraghelper.continuesettling(true)) { 

        viewcompat.postinvalidateonanimation(this); 

处理回调callback

创建一个viewdraghelper.callback

private viewdraghelper.callback getcallback = new viewdraghelper.callback() { 

    @override 

    public boolean trycaptureview(view child, int pointerid) { 

        return false; 

};  

as自动重写trycaptureview()方法,通过这个方法可以指定在创建viewdraghelper时,参数parentview中的哪一个子vieww可以被移动,例如我们在这个实例中自定义一个viewgroup,里面定义了两个子view——menu view和mainview,如下代码:

// 何时开始检测触摸事件 

public boolean trycaptureview(view child, int pointerid) { 

    //如果当前触摸的child是mmainview时开始检测 

    return mmainview == child; 

具体垂直滑动方法clampviewpositionvertical()和水平滑动方法clampviewpositionhorizontal()。实现滑动这个两个方法必须写,默认返回值是0,即不发生滑动,当然如果只重写clampviewpositionvertical()或clampviewpositionhorizontal()中的一个,那么就只会实现该方向上的滑动效果。

// 处理垂直滑动 

public int clampviewpositionvertical(view child, int top, int dy) { 

    return top; 

// 处理水平滑动 

public int clampviewpositionhorizontal(view child, int left, int dx) { 

    return left; 

clampviewpositionvertical(view child, int top, int dy)中的参数top,代表在垂直方向上child移动的距离,dy则表示比较前一次的增量。clampviewpositionhorizontal(view child, int left, int dx)也是类似的含义,通常情况下只需要返回top和left即可,但需要更加精确地计算padding等属性的时候,就需要对left进行一些处理,并返回合适大小的值。

通过重写上面的三个方法,就可以实现基本的滑动效果。当用手拖动mainview的时候,它就可有跟随手指的滑动而滑动了,代码:

private viewdraghelper.callback callback =  new viewdraghelper.callback() { 

            // 何时开始检测触摸事件 

            @override 

            public boolean trycaptureview(view child, int pointerid) { 

                //如果当前触摸的child是mmainview时开始检测 

                return mmainview == child; 

            } 

            // 处理垂直滑动 

            public int clampviewpositionvertical(view child, int top, int dy) { 

                return 0; 

            // 处理水平滑动 

            public int clampviewpositionhorizontal(view child, int left, int dx) { 

                return left; 

        };  

在前面的scroller中讲解时实现一个效果——手指离开屏幕后,view滑动回到初始位置。现在使用viewdraghelper实现,在viewdraghelper.callback中,系统提供了这样的方法——onviewreleased(),通过重写这个方法,可以非常简单地实现当手指离开屏幕后实现的操作。这个方法内部是使用scroller类实现的,这也是前面重写computescroll()方法的原因。

public void onviewreleased(view releasedchild, float xvel, float yvel) { 

    super.onviewreleased(releasedchild, xvel, yvel); 

    //手指抬起后缓慢移动到指定位置 

    if (mmainview.getleft() < 500) { 

        //关闭菜单 

        //等同于scroll的startscroll方法 

        mviewdraghelper.smoothslideviewto(mmainview, 0, 0); 

        viewcompat.postinvalidateonanimation(dragviewgroup.this); 

    } else { 

        //打开菜单 

        mviewdraghelper.smoothslideviewto(mmainview,300,0); 

设置让mainview移动后左边距小于500像素的时候,就使用smoothslideviewto()方法来讲mainview还原到初始状态,即坐标(0,0),左边距大于500则将mainview移动到(300,0)坐标,即显示mainview。

//viewdraghelper 

mviewdraghelper.smoothslideviewto(mmainview, 0, 0); 

viewcompat.postinvalidateonanimation(dragviewgroup.this);   

//scroller 

mscroller.startscroll(x,y,dx,dy); 

invalidate();  

滑动的时候,在自定义viewgroup的onfinishinflate()方法中,按照顺序将子view分别定义成menuview和mainview,并在onsizechanged方法中获得view的宽度。如果需要根据view的宽度来处理滑动后的效果,就可以使用这个值判断。

/*** 

 * 加载完布局文件后调用 

 */ 

protected void onfinishinflate() { 

    super.onfinishinflate(); 

    mmenuview = getchildat(0); 

    mmainview = getchildat(1); 

protected void onsizechanged(int w, int h, int oldw, int oldh) { 

    super.onsizechanged(w, h, oldw, oldh); 

    mwidth = mmenuview.getmeasuredwidth(); 

最后,整个通过viewdraghelper实现qq侧滑功能代码:

package com.xjf.drawview; 

import android.content.context; 

import android.support.v4.view.viewcompat; 

import android.support.v4.widget.viewdraghelper; 

import android.util.attributeset; 

import android.view.motionevent; 

import android.view.view; 

import android.widget.framelayout; 

public class dragviewgroup extends framelayout { 

    private viewdraghelper mviewdraghelper; 

    private view mmenuview, mmainview; 

    private int mwidth; 

    public dragviewgroup(context context) { 

        super(context); 

        initview(); 

    public dragviewgroup(context context, attributeset attrs) { 

        super(context, attrs); 

    public dragviewgroup(context context, 

                         attributeset attrs, int defstyleattr) { 

        super(context, attrs, defstyleattr); 

    /*** 

     * 加载完布局文件后调用 

     */ 

    protected void onfinishinflate() { 

        super.onfinishinflate(); 

        mmenuview = getchildat(0); 

        mmainview = getchildat(1); 

    protected void onsizechanged(int w, int h, int oldw, int oldh) { 

        super.onsizechanged(w, h, oldw, oldh); 

        mwidth = mmenuview.getmeasuredwidth(); 

    public boolean onintercepttouchevent(motionevent ev) { 

        return mviewdraghelper.shouldintercepttouchevent(ev); 

    public boolean ontouchevent(motionevent event) { 

        //将触摸事件传递给viewdraghelper,此操作必不可少 

        mviewdraghelper.processtouchevent(event); 

        return true; 

    private void initview() { 

        mviewdraghelper = viewdraghelper.create(this, callback); 

    private viewdraghelper.callback callback = 

            new viewdraghelper.callback() { 

                // 何时开始检测触摸事件 

                @override 

                public boolean trycaptureview(view child, int pointerid) { 

                    //如果当前触摸的child是mmainview时开始检测 

                    return mmainview == child; 

                } 

                // 触摸到view后回调 

                public void onviewcaptured(view capturedchild, 

                                           int activepointerid) { 

                    super.onviewcaptured(capturedchild, activepointerid); 

                // 当拖拽状态改变,比如idle,dragging 

                public void onviewdragstatechanged(int state) { 

                    super.onviewdragstatechanged(state); 

                // 当位置改变的时候调用,常用与滑动时更改scale等 

                public void onviewpositionchanged(view changedview, 

                                                  int left, int top, int dx, int dy) { 

                    super.onviewpositionchanged(changedview, left, top, dx, dy); 

                // 处理垂直滑动 

                public int clampviewpositionvertical(view child, int top, int dy) { 

                    return 0; 

                // 处理水平滑动 

                public int clampviewpositionhorizontal(view child, int left, int dx) { 

                    return left; 

                // 拖动结束后调用 

                public void onviewreleased(view releasedchild, float xvel, float yvel) { 

                    super.onviewreleased(releasedchild, xvel, yvel); 

                    //手指抬起后缓慢移动到指定位置 

                    if (mmainview.getleft() < 500) { 

                        //关闭菜单 

                        //相当于scroller的startscroll方法 

                        mviewdraghelper.smoothslideviewto(mmainview, 0, 0); 

                        viewcompat.postinvalidateonanimation(dragviewgroup.this); 

                    } else { 

                        //打开菜单 

                        mviewdraghelper.smoothslideviewto(mmainview, 300, 0); 

                    } 

            }; 

    public void computescroll() { 

        if (mviewdraghelper.continuesettling(true)) { 

            viewcompat.postinvalidateonanimation(this); 

        } 

除此之外,viewdraghelper很多强大的功能还没得到展示,在viewdraghelper.callback中,系统定义了大量的监听事件来帮助我们处理各种事件,如下:

onviewcaptured()这个事件在用户触摸到view后回调

onviewdragstatechanged()这个事件在拖拽状态改变时回调,比如idle,dragging等状态

state_idle:view当前没有被拖拽也没执行动画,只是安静地待在原地

state_dragging:view当前正在被拖动,由于用户输入或模拟用户输入导致view位置正在改变

state_settling:view当前正被安顿到指定位置,由fling手势或预定义的非交互动作触发

onviewpositionchanged()//view在拖动过程坐标发生变化时会调用此方法,包括两个时间段:手动拖动和自动滚动。

三、开源代码库

最后再分享一个自己积攒很久的代码库,只有你想不到,没有用不到的,欢迎star

<a href="https://github.com/xijiufu">https://github.com/xijiufu</a>

由于github服务器在美国,有时访问很慢,还提供了开源中国地址库,2个仓库代码均同步更新:

<a href="http://git.oschina.net/xijiufu">http://git.oschina.net/xijiufu </a>

作者:mrxi

来源:51cto

继续阅读