专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 移动开发

GestureDetector种的用法

发布时间:2010-05-30 05:29:43 文章来源:www.iduyao.cn 采编人员:星星草
GestureDetector类的用法

GestureDetector类定义了许多触摸事件。包括 
   1.boolean  onDoubleTap(MotionEvent e)解释:双击的第二下Touch down时触发 
   2.boolean  onDoubleTapEvent(MotionEvent e)解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。 
   3.boolean  onDown(MotionEvent e)解释:Touch down时触发 
   4.boolean  onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)解释:Touch了滑动一点距离后,up时触发。 
   5.void  onLongPress(MotionEvent e)解释:Touch了不移动一直Touch down时触发 
   6.boolean  onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)解释:Touch了滑动时触发。 
   7.void  onShowPress(MotionEvent e)解释:Touch了还没有滑动时触发(与onDown,onLongPress)比较onDown只要Touch down一定立刻触发。而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。所以Touchdown后一直不 滑动,onDown->onShowPress->onLongPress这个顺序触发。 
   8.boolean  onSingleTapConfirmed(MotionEvent e) 
   9.boolean  onSingleTapUp(MotionEvent e)解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。 
   点击一下非常快的(不滑动)Touchup:onDown->onSingleTapUp->onSingleTapConfirmed 
   点击一下稍微慢点的(不滑 动)Touchup:onDown->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 onCreate(Bundle savedInstanceState) { 
04.    super.onCreate(savedInstanceState); 
05.    mGestureDetector = new GestureDetector(this, new LearnGestureListener()); 
06.} 
07.@Override
08.public boolean onTouchEvent(MotionEvent event) { 
09.    if (mGestureDetector.onTouchEvent(event)) 
10.        return true; 
11.    else
12.        return false; 
13.} 
14.class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{ 
15.    @Override
16.    public boolean onSingleTapUp(MotionEvent ev) { 
17.        Log.d("onSingleTapUp",ev.toString()); 
18.        return true; 
19.    } 
20.    @Override
21.    public void onShowPress(MotionEvent ev) { 
22.        Log.d("onShowPress",ev.toString()); 
23.    } 
24.    @Override
25.    public void onLongPress(MotionEvent ev) { 
26.        Log.d("onLongPress",ev.toString()); 
27.    } 
28.    @Override
29.    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
30.        Log.d("onScroll",e1.toString()); 
31.        return true; 
32.    } 
33.    @Override
34.    public boolean onDown(MotionEvent ev) { 
35.        Log.d("onDownd",ev.toString()); 
36.        return true; 
37.    } 
38.    @Override
39.    public boolean onFling(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());
利用onTouchEvent方法作为入口检测,通过传递MotionEvent参数来监听操作手势。
1.mGestureDetector.onTouchEvent(event)
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: