欢迎来到天天文库
浏览记录
ID:37920358
大小:21.00 KB
页数:4页
时间:2019-06-02
《GestureDetector手势识别类》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、对于Android设备来说最主要的操作方式为触控,作为一个Android开发者来说深入了解GestureDetector手势识别是很有必要的,Android123今天主要介绍下该类相关的方法,以及简单的手势识别。通常我们构造GestureDetector类时设置一个GestureDetector.OnGestureListener对象来实时监控用户的操作,OnGestureListener对象提供一些常见手势的重写方法。@OverridepublicbooleanonDown(MotionEvente){/
2、/按下事件returntrue;}@OverridepublicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,floatvelocityY){//快速滚动returntrue;}@OverridepublicvoidonLongPress(MotionEvente){//长按}@OverridepublicbooleanonScroll(MotionEvente1,MotionEvente2,floatdistanceX,floatdis
3、tanceY){//常规滚动returntrue;}@OverridepublicvoidonShowPress(MotionEvente){//Android开发网提示为了产生理解异议,SDK原文为TheuserhasperformedadownMotionEventandnotperformedamoveorupyet.Thiseventiscommonlyusedtoprovidevisualfeedbacktotheusertoletthemknowthattheiractionhasbeenrec
4、ognizedi.e.highlightanelement}@OverridepublicbooleanonSingleTapUp(MotionEvente){//一次按下弹起returntrue;}首先上次Android123写的较为简单,如果网友不太明白可以看看更具体些的内容:booleanonDoubleTap(MotionEvente)解释:双击的第二下Touchdown时触发booleanonDoubleTapEvent(MotionEvente)解释:双击的第二下Touchdown和up都会触发
5、,可用e.getAction()区分。booleanonDown(MotionEvente)解释:Touchdown时触发booleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,floatvelocityY)解释:Touch了滑动一点距离后,up时触发。voidonLongPress(MotionEvente)解释:Touch了不移动一直Touchdown时触发booleanonScroll(MotionEvente1,MotionEvente2,
6、floatdistanceX,floatdistanceY)解释:Touch了滑动时触发。voidonShowPress(MotionEvente)解释:Touch了还没有滑动时触发(与onDown,onLongPress比较onDown只要Touchdown一定立刻触发。而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。booleanonSingleT
7、apConfirmed(MotionEvente)booleanonSingleTapUp(MotionEvente)解释:上面这两个函数都是在touchdown后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。点击一下非常快的(不滑动)Touchup:onDown->onSingleTapUp->onSingleTapConfirmed点击一下稍微慢点的(不滑动)Touchup:onDown->onShowPress->onSingleTapUp->onSi
8、ngleTapConfirmed有了这么多的响应方式,我们能更加方便的对用户的触摸操作进行响应,对各种动作都有所对应。那么这个类如何使用呢,其实非常简单,在view的新建一个GestureDetector的对象。构造函数里gestureDetector=newGestureDetector(newSelfGestureDetectorListener());然后在View的onTouchEvent里以下这样用,就
此文档下载收益归作者所有