GestureDetector类的用法

来源:互联网 发布:男生挎包 知乎 编辑:程序博客网 时间:2024/05/19 02:28

GestureDetector类定义了许多触摸事件。包括
   1.boolean  on
DoubleTap(MotionEvent e)解释:双击的第二下Touch down时触发
   2.boolean  on
DoubleTapEvent(MotionEvent e)解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
   3.boolean  on
Down(MotionEvent e)解释:Touch down时触发
   4.boolean  on
Fling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)解释:Touch了滑动一点距离后,up时触发。
   5.void  on
LongPress(MotionEvent e)解释:Touch了不移动一直Touch down时触发
   6.boolean  on
Scroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)解释:Touch了滑动时触发。
   7.void  on
ShowPress(MotionEvent e)解释:Touch了还没有滑动时触发(与onDown,onLongPress)比较onDown只要Touch down一定立刻触发。而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。所以Touchdown后一直不 滑动,onDown->onShowPress->onLongPress这个顺序触发。
   8.boolean  on
SingleTapConfirmed(MotionEvent e)
   9.boolean  on
SingleTapUp(MotionEvent e)解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
   点击一下非常快的(不滑动)Touchup:on
Down->onSingleTapUp->onSingleTapConfirmed
   点击一下稍微慢点的(不滑 动)Touchup:on
Down->onShowPress->onSingleTapUp->onSingleTapConfirmed



GestureDetector探测当前用户各种不同的操作手势,通过GestureDetector.OnGestureListener callback来获取当前被触发的操作手势(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling)。


使用方法:


01.private GestureDetector mGestureDetector;
02.@Override
03.public void on
Create(Bundle savedInstanceState) {
04.    super.on
Create(savedInstanceState);
05.    mGestureDetector = new GestureDetector(this, new LearnGestureListener());
06.}
07.@Override
08.public boolean on
TouchEvent(MotionEvent event) {
09.    if (mGestureDetector.on
TouchEvent(event))
10.        return true;
11.    else
12.        return false;
13.}
14.class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{
15.    @Override
16.    public boolean on
SingleTapUp(MotionEvent ev) {
17.        Log.d("on
SingleTapUp",ev.toString());
18.        return true;
19.    }
20.    @Override
21.    public void on
ShowPress(MotionEvent ev) {
22.        Log.d("on
ShowPress",ev.toString());
23.    }
24.    @Override
25.    public void on
LongPress(MotionEvent ev) {
26.        Log.d("on
LongPress",ev.toString());
27.    }
28.    @Override
29.    public boolean on
Scroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
30.        Log.d("on
Scroll",e1.toString());
31.        return true;
32.    }
33.    @Override
34.    public boolean on
Down(MotionEvent ev) {
35.        Log.d("on
Downd",ev.toString());
36.        return true;
37.    }
38.    @Override
39.    public boolean on
Fling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
40.        Log.d("d",e1.toString());
41.        Log.d("e2",e2.toString());
42.        return true;
43.    }
44.}
说明:
在当前类中创建一个GestureDetector实例。
1.private GestureDetector mGestureDetector;
创建一个Listener来实时监听当前面板操作手势。
1.class LearnGestureListener extends GestureDetector.SimpleOnGestureListener
在初始化时,将Listener实例关联当前的GestureDetector实例。
1.mGestureDetector = new GestureDetector(this, new LearnGestureListener());
利用on
TouchEvent方法作为入口检测,通过传递MotionEvent参数来监听操作手势。
1.mGestureDetector.on
TouchEvent(event)

原创粉丝点击