Android手势操作简单实例讲解
上一篇介绍的onTouch提供的事件还是相对较简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦,因为我们要根据用户触摸的轨迹去判断是什么手势。幸好AndroidSDK给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。
GestureDetector这个类对外提供了两个接口和一个外部类:
•接口:OnGestureListener,OnDoubleTapListener
•外部类:SimpleOnGestureListener
这个外部类,其实是两个接口中所有函数的集成,它包含了这两个接口里所有必须要实现的函数而且都已经重写,但所有方法体都是空的;不同点在于:该类是staticclass,程序员可以在外部继承这个类,重写里面的手势处理方法。
OnGestureListener有下面的几个动作:
•onDown(MotionEvente):用户按下屏幕就会触发。
•onShowPress(MotionEvente):如果按下的时间超过瞬间,而且在按下的时候没有松开或者是拖动的,那么onShowPress就会执行,具体这个瞬间是多久,我也不清楚…
•onLongPress(MotionEvente):长按触摸屏,超过一定时长,就会触发这个事件。
触发顺序:onDown->onShowPress->onLongPress
•onSingleTapUp(MotionEvente):一次单独的轻击抬起操作,也就是轻击一下屏幕,立刻抬起来,才会有这个触发,当然,如果除了Down以外还有其它操作,那就不再是Single操作了,所以也就不会触发这个事件。
触发顺序:
点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
•onFling(MotionEvente1,MotionEvente2,floatvelocityX,floatvelocityY):滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEventACTION_DOWN,多个ACTION_MOVE,1个ACTION_UP触发。
参数解释:
e1:第1个ACTION_DOWNMotionEvent
e2:最后一个ACTION_MOVEMotionEvent
velocityX:X轴上的移动速度,像素/秒
velocityY:Y轴上的移动速度,像素/秒
•onScroll(MotionEvente1,MotionEvente2,floatdistanceX,floatdistanceY):在屏幕上拖动事件。无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法在ACTION_MOVE动作发生时就会触发。
OnDoubleTapListener有下面的几个动作:
•onSingleTapConfirmed(MotionEvente):单击事件。用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。
触发顺序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
•onDoubleTap(MotionEvente):双击事件。
•onDoubleTapEvent(MotionEvente):双击间隔中发生的动作。指触发onDoubleTap以后,在双击之间发生的其它动作,包含down、up和move事件。
关于onSingleTapConfirmed和onSingleTapUp的区别:onSingleTapUp,只要手抬起就会执行,而对于onSingleTapConfirmed来说,如果双击的话,则onSingleTapConfirmed不会执行。
使用Gesture
•使用GestureDetector.OnGestureListener接口
要使用OnGestureListener接口,大致有几步要走:
1、创建OnGestureListener监听函数:
privateclassgestureListenerimplementsGestureDetector.OnGestureListener{
}
2、创建GestureDetector实例:
构造函数有下面三个,根据需要选择:
GestureDetectorgestureDetector=newGestureDetector(GestureDetector.OnGestureListenerlistener);
GestureDetectorgestureDetector=newGestureDetector(Contextcontext,GestureDetector.OnGestureListenerlistener);
GestureDetectorgestureDetector=newGestureDetector(Contextcontext,GestureDetector.SimpleOnGestureListenerlistener);
注:GestureDetector现在已经是deprecation状态,现在推荐GestureDetectorCompat
GestureDetectorCompatgestureDetectorCompat=newGestureDetectorCompat(Contextcontext,GestureDetector.OnGestureListenerlistener);
GestureDetectorCompatgestureDetectorCompat=newGestureDetectorCompat(Contextcontext,GestureDetector.OnGestureListenerlistener,Handlerhandler);
3、onTouch(Viewv,MotionEventevent)中拦截,在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector来分析是否有合适的callback函数来处理用户的手势
publicbooleanonTouch(Viewv,MotionEventevent){ returngestureDetectorCompat.onTouchEvent(event); }
4、控件绑定
TextViewtv=(TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
实现代码:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="50dip" android:background="#76EE00" android:text="GestureDetector"/> </RelativeLayout>
publicclassMainActivityextendsActivityimplementsOnTouchListener{ privateGestureDetectorCompatmGestureDetectorCompat; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetectorCompat=newGestureDetectorCompat(this,newgestureListener()); TextViewtv=(TextView)findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } publicbooleanonTouch(Viewv,MotionEventevent){ returnmGestureDetectorCompat.onTouchEvent(event); } privateclassgestureListenerimplementsGestureDetector.OnGestureListener{ publicbooleanonDown(MotionEvente){ showlog("onDown"); Toast.makeText(MainActivity.this,"onDown",Toast.LENGTH_SHORT).show(); returnfalse; } publicvoidonShowPress(MotionEvente){ showlog("onShowPress"); Toast.makeText(MainActivity.this,"onShowPress",Toast.LENGTH_SHORT).show(); } publicbooleanonSingleTapUp(MotionEvente){ showlog("onSingleTapUp"); Toast.makeText(MainActivity.this,"onSingleTapUp",Toast.LENGTH_SHORT).show(); returntrue; } publicbooleanonScroll(MotionEvente1,MotionEvente2, floatdistanceX,floatdistanceY){ showlog("onScroll:"+(e2.getX()-e1.getX())+""+distanceX); Toast.makeText(MainActivity.this,"onScroll",Toast.LENGTH_LONG).show(); returntrue; } publicvoidonLongPress(MotionEvente){ showlog("onLongPress"); Toast.makeText(MainActivity.this,"onLongPress",Toast.LENGTH_LONG).show(); } publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX, floatvelocityY){ showlog("onFling"); Toast.makeText(MainActivity.this,"onFling",Toast.LENGTH_LONG).show(); returntrue; } }; publicvoidshowlog(Stringinfo){ System.out.print("GestureDetector"+info); } }
•使用GestureDetector.OnDoubleTapListener接口
实现OnDoubleTapListener接口的前提是先实现OnGestureListener接口,其实除了第1步,2、3、4步和上面完全一样,不再赘述,下面看下第一步:
同时创建OnGestureListener和OnDoubleTapListener监听函数:
方法一:新建一个类同时派生自OnGestureListener和OnDoubleTapListener:
privateclassgestureListenerimplementsGestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{ }
方法二:使用GestureDetector.setOnDoubleTapListener();函数设置监听:
/构建GestureDetector实例 mGestureDetector=newGestureDetector(newgestureListener()); privateclassgestureListenerimplementsGestureDetector.OnGestureListener{ } //设置双击监听器 mGestureDetector.setOnDoubleTapListener(newdoubleTapListener()); privateclassdoubleTapListenerimplementsGestureDetector.OnDoubleTapListener{ }
注:大家可以看到无论在方法一还是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我们说过GestureDetectorCompat的构造函数,如下:
GestureDetectorCompatgestureDetectorCompat=newGestureDetectorCompat(Contextcontext,GestureDetector.OnGestureListenerlistener);
GestureDetectorCompatgestureDetectorCompat=newGestureDetectorCompat(Contextcontext,GestureDetector.OnGestureListenerlistener,Handlerhandler);
可以看到,它的两个构造函数参数都必须是OnGestureListener的实例。所以要想使用OnDoubleTapListener的几个函数,就必须先实现OnGestureListener。
实现代码:
publicclassMainActivityextendsActivityimplementsOnTouchListener{ privateGestureDetectorCompatmGestureDetectorCompat; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetectorCompat=newGestureDetectorCompat(this,newgestureListener()); TextViewtv=(TextView)findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } publicbooleanonTouch(Viewv,MotionEventevent){ returnmGestureDetectorCompat.onTouchEvent(event); } privateclassgestureListenerimplementsGestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{ publicbooleanonDown(MotionEvente){ showlog("onDown"); Toast.makeText(MainActivity.this,"onDown",Toast.LENGTH_SHORT).show(); returnfalse; } publicvoidonShowPress(MotionEvente){ showlog("onShowPress"); Toast.makeText(MainActivity.this,"onShowPress",Toast.LENGTH_SHORT).show(); } publicbooleanonSingleTapUp(MotionEvente){ showlog("onSingleTapUp"); Toast.makeText(MainActivity.this,"onSingleTapUp",Toast.LENGTH_SHORT).show(); returntrue; } publicbooleanonScroll(MotionEvente1,MotionEvente2, floatdistanceX,floatdistanceY){ showlog("onScroll:"+(e2.getX()-e1.getX())+""+distanceX); Toast.makeText(MainActivity.this,"onScroll",Toast.LENGTH_LONG).show(); returntrue; } publicvoidonLongPress(MotionEvente){ showlog("onLongPress"); Toast.makeText(MainActivity.this,"onLongPress",Toast.LENGTH_LONG).show(); } publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX, floatvelocityY){ showlog("onFling"); Toast.makeText(MainActivity.this,"onFling",Toast.LENGTH_LONG).show(); returntrue; } publicbooleanonSingleTapConfirmed(MotionEvente){ showlog("onSingleTapConfirmed"); Toast.makeText(MainActivity.this,"onSingleTapConfirmed",Toast.LENGTH_LONG).show(); returntrue; } publicbooleanonDoubleTap(MotionEvente){ showlog("onDoubleTap"); Toast.makeText(MainActivity.this,"onDoubleTap",Toast.LENGTH_LONG).show(); returntrue; } publicbooleanonDoubleTapEvent(MotionEvente){ showlog("onDoubleTapEvent"); Toast.makeText(MainActivity.this,"onDoubleTapEvent",Toast.LENGTH_LONG).show(); returntrue; } publicvoidshowlog(Stringinfo){ System.out.print("GestureDetector"+info); } }
•使用GestureDetector.SimpleOnGestureListener类
使用OnGestureListener和OnDoubleTapListener接口,这样需要重载接口所有的方法,适合监听所有的手势,如果我们只想监听某个手势或某几个手势呢,这时候就可以使用SimpleOnGestureListener类了。
它与前两个不同的是:
1、这是一个类,在它基础上新建类的话,要用extends派生而不是用implements继承!
2、OnGestureListener和OnDoubleTapListener接口里的函数都是强制必须重写的,即使用不到也要重写出来一个空函数但在SimpleOnGestureListener类的实例或派生类中不必如此,可以根据情况,用到哪个函数就重写哪个函数,因为SimpleOnGestureListener类本身已经实现了这两个接口的所有函数,只是里面全是空的而已。
publicclassMainActivityextendsActivityimplementsOnTouchListener{ privateGestureDetectormGestureDetector; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetector=newGestureDetector(newsimpleGestureListener()); TextViewtv=(TextView)findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } publicbooleanonTouch(Viewv,MotionEventevent){ returnmGestureDetector.onTouchEvent(event); } privateclasssimpleGestureListenerextendsGestureDetector.SimpleOnGestureListener{ /*****OnGestureListener的函数*****/ publicbooleanonDown(MotionEvente){ Log.i("MyGesture","onDown"); Toast.makeText(MainActivity.this,"onDown",Toast.LENGTH_SHORT).show(); returnfalse; } publicvoidonShowPress(MotionEvente){ Log.i("MyGesture","onShowPress"); Toast.makeText(MainActivity.this,"onShowPress",Toast.LENGTH_SHORT).show(); } publicbooleanonSingleTapUp(MotionEvente){ Log.i("MyGesture","onSingleTapUp"); Toast.makeText(MainActivity.this,"onSingleTapUp", Toast.LENGTH_SHORT).show(); returntrue; } /*****OnDoubleTapListener的函数*****/ publicbooleanonSingleTapConfirmed(MotionEvente){ Log.i("MyGesture","onSingleTapConfirmed"); Toast.makeText(MainActivity.this,"onSingleTapConfirmed",Toast.LENGTH_LONG).show(); returntrue; } publicbooleanonDoubleTap(MotionEvente){ Log.i("MyGesture","onDoubleTap"); Toast.makeText(MainActivity.this,"onDoubleTap",Toast.LENGTH_LONG).show(); returntrue; } } }
源码下载:http://xiazai.jb51.net/201609/yuanma/GestureDetector(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。