8 使用GestureDetector进行手势识别
8-1 使用GestureDetector进行手势识别
手势篇
——GestureDetector
——GestureOverlayView
使用GestureDetector手势交互过程:
1.触屏一刹那,触发MotionEvent事件
2.被OntouchListener监听,在onTouch()中获得MotionEvent对象
3.GestureDetector转发MotionEvent对象至OnGestureListener
4.OnGestureListener获得该对象,根据该对象封装的信息作出合适的反馈
名词解释
1.MotionEvent:
a.用于封装手势、触摸笔、轨迹球等动作事件
b.内部封装用于记录横轴和纵轴坐标的属性X和Y
2.GestureDetector:识别各种手势
3.OnGestureListener:
a.手势交互的监听接口,其提供多个抽象方法
b.根据GestureDector的手势识别结果调用相对应的方法
GestureDetector详解:
1.触摸屏:按下。移动、抬起等(重载onTouch或者设置setOntouchListener)
2.GestureDetector工作原理:
a.当接收到用户触摸消息时,将消息交给GestureDetector加工
b.通过设置监听获得GestureDetector处理后的手势
3.GestureDetector提供两个监听器
a.OnGestureListener:处理单击类消息
b.OnDoubleTapListener:处理双击类消息
OnGestureListener接口:
单击:onDown(MotionEvent e)
抬起:onSingleTapUp(MotionEvent e)
短按:onShowPress(MotionEvent e)
长按:onLongPress(MotionEvent e)
滚动:onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)
滑动:onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)
OnDoubleTaoListener接口:
双击:onDoubleTap(MotionEvent e)
双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent e)
单击确认:onSingleTapConfirmed(MotionEvent e)(即很快的按下并抬起,但并不连续点击第二下)
SimpleOnGestureListener
1.集成SimpleOnGestureListener
2.重载感兴趣的手势
使用GestureDetector转发MotionEvent对象的方法:
调用GestureDetector的onTouchEvent(MotionEvent)方法就可以了。然后利用SimpleOnGestureListener这个辅助类,就可以完成手势动作的转发了。
GestureDetector
触摸事件MotionEvent--->被OnTouchListener监听到,在其ontouch方法中获得motionevent对象--->交给OnGestureListener获得对象--->做出反馈
手势-->手势类型鉴别--->对应方法
MotionEvent-->GestureDetector--->OnGestureListener
1. GestureDetector有三个监听器
GestureDetector.OnGestureListener、GestureDetector.OnDoubleTapListener、GestureDetector.SimpleOnGestureListener
前两个是接口,分别用来监听单机和双击的手势
GestureDetector.OnGestureListener的六个抽象方法,GestureDetector.OnDoubleTapListener有三个抽象方法(见reference)
GestureDetector.SimpleOnGestureListener是继承了前面两个接口的类,所以这9个抽象方法可以重写。
2. 使用
(1)在setOnTouchListener监听器中重写onTouch()方法
//先实例一个GestureDetector对象
mGestureDetector = new GestureDetector(MainActivity.this, new MyOnGestureListener());
//为控件设置OnTouchListener()监听器,重写onTouch()方法,将MotionEvent事件转发给OnGestureListener监听器
image.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event); //将MotionEvent转发给MyOnGestureListener监听器
return true; //必须要return true
}
});
(2)重写OnGestureListener监听器,重写必要的抽象方法
public class MyOnGestureListener extends SimpleOnGestureListener{
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
}
MainActivity.java
package com.example.test; import android.app.Activity; import android.gesture.Gesture; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { ImageView myImg; // 3.建立GestureDetector对象 GestureDetector myGestureDetector; class myGestureListener extends SimpleOnGestureListener { @Override // MotionEvent e1——刚开始事件 // MotionEvent e2——结束事件 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub if (e1.getX() - e2.getX() > 50) { Toast.makeText(MainActivity.this, "从右往左滑动", Toast.LENGTH_SHORT).show(); } else if (e2.getX() - e1.getX() > 50) { Toast.makeText(MainActivity.this, "从左往右滑动", Toast.LENGTH_SHORT).show(); } return super.onFling(e1, e2, velocityX, velocityY); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myImg = (ImageView) findViewById(R.id.img); // 4.实例化myGestureDetector myGestureDetector = new GestureDetector(new myGestureListener()); // 1.设置监听事件,写入匿名内部类 myImg.setOnTouchListener(new OnTouchListener() { // 2.可以捕获触屏屏幕发生的Event事件,ontouch方法中获得motionevent对象 @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub myGestureDetector.onTouchEvent(event); return true; } }); } // 首先产生MotionEvent event事件,被setOnTouchListener方法捕获, // 又被onTouchEvent方法转发给myGestureListener, // 而myGestureListener里有个方法为onFling,通过两个事件的判断得到实现。 }
<RelativeLayout xmlns: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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" /> </RelativeLayout>


如果是进行复杂的手势,像转圈什么的,该GestureDetector 就不满足了。

浙公网安备 33010602011771号