当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等。
一般情况下,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View
Android
GestureDetector这个类对外提供了两个接口:OnGestureListener,OnDoubleTapListener,还有一个内部类SimpleOnGestureListener。
GestureDetector.OnDoubleTapListener接口:用来通知DoubleTap事件,类似于鼠标的双击事件。
1,onDoubleTap(MotionEvent e):在双击的第二下,Touch down时触发
2,onDoubleTapEvent(MotionEvent e):通知DoubleTap手势中的事件,包含down、up和move事件(这里指的是在双击之间发生的事件,例如在同一个地方双击会产生DoubleTap手势,而在DoubleTap手势里面还会发生down和up事件,这两个事件由该函数通知);双击的第二下Touch down和up都会触发,可用e.getAction()区分。
3,onSingleTapConfirmed(MotionEvent e):用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。这个方法不同于onSingleTapUp,他是在GestureDetector确信用户在第一次触摸屏幕后,没有紧跟着第二次触摸屏幕,也就是不是“双击”的时候触发
GestureDetector.OnGestureListener接口:用来通知普通的手势事件,该接口有如下六个回调函数:
(onScroll),又没有长按(onLongPress),然后Touchup时触发。
onDown->onSingleTapUp->onSingleTapConfirmed
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
事件;Touch了还没有滑动时触发(与onDown,onLongPress)比较onDown只要Touch down一定立刻触发。而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。所以Touchdown后一直不滑动
按照onDown->onShowPress->onLongPress这个顺序触发。
势事件;Touch了滑动一点距离后,在ACTION_UP时才会触发
参数:e1 第1个ACTION_DOWN MotionEvent 并且只有一个;e2 最后一个ACTION_MOVE MotionEvent ;velocityX X轴上的移动速度,像素/秒
6.
拖动事件。无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法在ACTION_MOVE动作发生时就会触发
onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling
onDown------》onScroll----》onScroll------》onFiling
SimpleOnGestureListener类是GestureDetector提供给我们的一个更方便的响应不同手势的类,这个类实现了上述两个接口(但是所有的方法体都是空的),该类是static class,也就是说它实际上是一个外部类。程序员可以在外部继承这个类,重写里面的手势处理方法。
方法步骤
第一种示例:
1,通过GestureDetector的构造方法可以将SimpleOnGestureListener对象传递进去,这样GestureDetector能处理不同的手势了。
public GestureDetector
(Context context, GestureDetector.OnGestureListener listener)
2,在OnTouchListener的onTouch方法中
private OnTouchListener gestureTouchListener = new OnTouchListener() {
第二种示例:
使用方法
private GestureDetector mGestureDetector;
mGestureListener = new BookOnGestureListener();
构造出来mGestureDetector = new GestureDetector(mGestureListener);
class BookOnGestureListener implements OnGestureListener {
同时要public boolean onTouchEvent(MotionEvent event) {
第三种示例代码
代码:
01.private GestureDetector mGestureDetector;
02.@Override
03.public void onCreate(Bundle savedInstanceState) {
04.
05.
06.}
07.@Override
08.public boolean onTouchEvent(MotionEvent event) {
09.
10.
11.
12.
13.}
14.class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{
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.}
1,在当前类中创建一个GestureDetector实例。
private GestureDetector mGestureDetector;
2,创建一个Listener来实时监听当前面板操作手势。
class LearnGestureListener extends GestureDetector.SimpleOnGestureListener
3,在初始化时,将Listener实例关联当前的GestureDetector实例。
mGestureDetector = new GestureDetector(this, new LearnGestureListener());
4,利用onTouchEvent方法作为入口检测,通过传递MotionEvent参数来监听操作手势。
1.mGestureDetector.onTouchEvent(event)
第四种示例代码
private GestureDetector mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
…
}
基本的内容就是创建一个GestureDetector的对象,传入listener对象,在自己接收到的onTouchEvent中将event传给GestureDetector进行分析,listener会回调给我们相应的动作。其中GestureDetector.SimpleOnGestureListener(Framework帮我们简化了)是实现了上面提到的OnGestureListener和OnDoubleTapListener两个接口的类,我们只需要继承它并重写其中我们关心的回调即可。
最后,再提一下双击和三击的识别过程:在第一次单击down时
大家好,关于android双击事件 我相信大家都知道 API中是有个方法的,但是必须在Activity中在能使用。
对于到底用不用android 双击事件API各有各的看法。
在Activity中使用API
优点:省时省力,别人写的东西,直接用就行了,不担心有BUG
缺点:代码写在activity中 总感觉有些乱。
自己写个onDoubleClick方法
优点:自己写的东西,好控制,灵活性强,想放哪放哪。
缺点:需要消耗点时间,检查BUG之类的。
好了,最近我也遇到了需要处理双击事件的问题,我是这样处理的
我用了android的API 但是我也很好整理。
上代码吧,只有2个类 一个当然是Activity 另一个就是OnDoubleClick事件处理类
1 import android.app.Activity;
2 import android.os.Bundle;
3 import android.view.GestureDetector;
4 import android.view.MotionEvent;
5 import android.view.View.OnTouchListener;
6
7 /**
8 *
9 * <p>class instruction:内部界面 核心Activity</p>
10 * create in 2011-12-2 @author Mercury
11 */
12 public class GameActivity extends Activity {
13 GameActivityHelper helper;
14 private GestureDetector gd;
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 helper=new GameActivityHelper(this);
19 helper.init();
20 setContentView(helper.getGameView());
21 gd=new GestureDetector(this,new OnDoubleClick());
22 }
23 @Override
24 public boolean onTouchEvent(MotionEvent event) {
25 return gd.onTouchEvent(event);
26 }
27 }
1 import android.view.GestureDetector;
2 import android.view.MotionEvent;
3
4 public class OnDoubleClick extends GestureDetector.SimpleOnGestureListener{
5 @Override
6 public boolean onDoubleTap(MotionEvent e) {
7 //TODO
8 return false;
9 }
10 // @Override
11 // public boolean onDoubleTapEvent(MotionEvent e) {
12 // return super.onDoubleTapEvent(e);
13 // }
14 }
代码也还算整洁。 API中对于双击有两种处理,
一种是双击后执行一次:onDoubleTap
一种是双击后执行两次:onDoubleTapEvent
这里我只需要双击后执行一次就行了,那里注释掉了
还有需要特别注意的是在自己的Activity中重写的父类Activity的方法
@Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
}
这个方法。我在网上看到有很多人在Activity中 实现implements OnTouchListener 后会要求你重写onTouch方法来实现双击。
这完全是误导人的做法,本人亲自试过,双击是无效的,普通的点击还是有效 我是在SDK1.6版本中试的
这是个细节希望大家多注意下。
这里的onTouchEvent 是Activity中的方法,而不是实现某个接口而来的,这就是为什么只能在Activity中处理双击事件的原因,
如果你需要用androidAPI处理双击事件就必须重写Activity中的onTouchEvent 方法 否则与GestureDetector中有关的方法都无效
onTouch 只要实现了OnTouchListener接口的View或者Activity都必须重写的方法,他是不能实现GestureDetector的一些功能的