Android手势识别类,GestureDetector,ScaleGestureDetector
Android程序使用中会有很多的手势,双击、长按、滑动、缩放等,我们可以通过手势识别类GestureDetector,ScaleGestureDetector进行识别。
1.GestureDetector
(1) 包含方法
onDown 按下触发。
onShowPress 按下但尚未松开或移动手指时调用。
onSingleTapUp 单点后抬起触发。
onScroll 滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。
onLongPress 长按触发。
onFling 迅速滑动后抬起手指时触发。motionEvent:按下事件;motionEvent1:抬起事件;vx:在x轴上的速度;vy:在y轴上的速度。
(2) 使用GestureDetector
可使用OnGestureListener或SimpleOnGestureListener作为参数;参数使用OnGestureListener需要实现全部方法;参数使用SimpleOnGestureListener只需要实现任意数量的方法。
//参数使用OnGestureListener需要实现全部方法
GestureDetector gestureDetector=new GestureDetector( context , new GestureDetector.OnGestureListener() {public boolean onDown(MotionEvent motionEvent) {//按下触发。return false;}public void onShowPress(MotionEvent motionEvent) {//按下但尚未松开或移动手指时调用。}public boolean onSingleTapUp(MotionEvent motionEvent) {//单点后抬起触发。return false;}public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float distanceX, float distanceY) {//滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。return false;}public void onLongPress(MotionEvent motionEvent) {//长按触发。}public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float vx, float vy) {//迅速滑动后抬起手指时触发。motionEvent:按下事件;motionEvent1:抬起事件;vx:在x轴上的速度;vy:在y轴上的速度。return false;}
});
//参数使用SimpleOnGestureListener只需要实现任意数量的方法
GestureDetector gestureDetector = new GestureDetector( context , new GestureDetector.SimpleOnGestureListener() {public boolean onScroll( MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {//滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。return false;}
});
2.ScaleGestureDetector
(1) 包含方法
onScale 缩放时调用。
onScaleBegin 缩放开始时调用。
onScaleEnd 缩放结束时调用。
(2) 使用ScaleGestureDetector
可使用OnScaleGestureListener或SimpleOnScaleGestureListener作为参数;参数使用OnScaleGestureListener需要实现全部方法;参数使用SimpleOnScaleGestureListener只需要实现任意数量的方法。
//参数使用OnScaleGestureListener需要实现全部方法
ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector( context , new ScaleGestureDetector.OnScaleGestureListener() {public boolean onScale( ScaleGestureDetector scaleGestureDetector) {//缩放时调用。return false;}public boolean onScaleBegin( ScaleGestureDetector scaleGestureDetector) {//缩放开始时调用。return false;}public void onScaleEnd( ScaleGestureDetector scaleGestureDetector) {//缩放结束时调用。}
});
//参数使用SimpleOnScaleGestureListener只需要实现任意数量的方法
ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector( context ,new ScaleGestureDetector.SimpleOnScaleGestureListener(){public boolean onScale( ScaleGestureDetector scaleGestureDetector) {//缩放时调用。return false;}
});