Android 之手势识别篇-GestureDetector

来源:互联网 发布:omnirom源码编译 编辑:程序博客网 时间:2024/05/23 12:56

Android 之手势识别篇-GestureDetector

         根据用户的手势做出相应的处理是Android中一个较为重要的课题之一了。所以:

1、  如果你想监听用户的手势(例如滑动手势),就需要去实现GestureDetector.OnGestureListener接口;

2、  如果你想监听点击、双击事件的手势,就要实现GestureDetector.OnDoubleTapListener接口;

3、  如果你想监听点击、双击、滑动事件的手势,就要去继承GestureDetector. SimpleOnGestureListener这个类。

 

SimpleOnGestureListener是继承于GestureDetect并且实现了OnGestureListener和OnDoubleTapListener接口的。

以下是不同事件所触发的方法,其中OnDoubleTap、OnDoubleTapEvent和onSingleTapConfirmed是属于OnDoubleTapListener的方法。剩下的6个方法是属于OnGestureListener接口的。

 

boolean                               

onDoubleTap(MotionEvent e)

Notified when a double-tap occurs.

当用户双击屏幕时,就会触发这个方法

boolean  

onDoubleTapEvent(MotionEvent e)

Notified when an event within a double-tap gesture occurs, including the down, move, and up events.

当确定有双击事件发生时,在双击事件中的第二次点击中(按下,移动,起来),就会触发该方法

双击事件触发顺序:OnDown--OnSingleTapUp--OnDoubleTap--OnDoubleTapEvent--OnDown--OnDoubleTapEvent

意思就是:第一次按下--第一次起来--触发双击事件--双击事件按下--第二次按下--第二次起来

boolean  

onSingleTapConfirmed(MotionEvent e)

Notified when a single-tap occurs.

SingleTapUp方法不同的是,只用当Gesture当确定这一次点击不是双击前的第一次点击,才会调用该方法

boolean  

onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)

Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent.

当用户在屏幕上进行滑动,到手离开屏幕后,才会触发这个方法,e1为起始点,e2为结束点,velocityX为水平方向的速度,velocityY为垂直方向的速度(当需要从手势结束后才执行的,就使用这个方法)

void  

onLongPress(MotionEvent e)

Notified when a long press occurs with the initial on down MotionEvent that trigged it.

当用户长按屏幕时,就会触发这个方法

boolean  

onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)

Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent.

当用户在屏幕进行滑动时,就会连续触发这个方法,e1e2是滑动过程中的两个点,distanceXdistanceY分别是水平滑动距离和垂直滑动距离(当需要根据手势,实时进行变化的就使用这个方法,例如看地图的时候,地图会根据手势实时变化)

void  

onShowPress(MotionEvent e)

The user has performed a down MotionEvent and not performed a move or up yet.

当用户按下屏幕时,但是还没有移动或者是起来的时候,就会触发这个方法,每次只会触发一次

boolean  

onSingleTapUp(MotionEvent e)

Notified when a tap occurs with the up MotionEvent that triggered it.

当用户点击屏幕,并且起来后,才会触发这个方法

 

实现监控手势的编码方法主要是:

private GestureDetector gesture = new GestureDetector(MainActivity.this,myGestureListener);

这里的myGestureListener是实现了OnGestureListener接口的类的对象。然后在该类中重写了所有OnGestureListener接口的方法(根据需求进行重写)。

 

然后在Activity里面重写OnTouchEvent()方法,如下:

@Override

public boolean onTouchEvent(MotionEventevent) {

    gesture.onTouchEvent(event);

    returnsuper.onTouchEvent(event);

}

 

还有一点的是,当这些方法可以有一个boolean类型的返回值时:

当返回值为true时,就代表这个方法把当前这个MotionEvent给消费了,不会继续传递到后面的其他方法中,但是不会影响这个监听器里面的其他方法,因为触发一个OnTouchEvent事件,才会触发一个监听器里面的方法,所以不会被当前这个MotionEvent是否被消费所影响。

当返回值为false时,就不会把MotionEvent消费了,默认为false。


列出事件触发情况:

点击:OnDown---OnSingleTapUp---OnSingleTapConfirmed

半长按:OnDown---OnShowPress---OnSingleTapUp

长按:OnDown---OnShowPress---OnLongPress

滑动:OnDown---OnScroll(多次触发)---OnFling

双击:OnDown---OnSingleTapUp---OnDoubleTap---OnDoubleTapEvent---OnDown---OnDoubleTapEvent

双击滑动(在第二次点击的时候进行滑动):

OnDown---OnSingleTapUp---OnDoubleTap---OnDoubleTapEvent---OnDown---OnDoubleTapEvent(多次触发,如果滑动的时间足够长,也会触发OnShowPress和OnLongPress方法)

 

参考资料:

http://www.cnblogs.com/mengdd/p/3394345.html

http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html

0 0