一、概述
1、手势交互过程(原理)
(1)触屏一刹那,触发MotionEvent事件
(2)被OnTounchListener监听,在onTouch()中获得MotionEvent对象
(3)GestureDetector转发MotionEvent对象至OnGestureListener.
2、MotionEvent:
(1)用于封装手势、触摸笔、规则球等动作事件
(2)内部封装用于记录横轴和纵轴坐标的属性X和Y
3、GestureDetector:
(1)识别各种手势
(2)触摸屏:按下、移动、抬起等
<1>重载onTouch或者设置setOnTouchListener
(3)工作原理:
<1>当接收到用户触摸消息时,将消息交给GestureDetector加工
<2>通过设置监听器获得GestureDetector处理后的手势
4、监听器:
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)
OnDoubleTapListener:处理双击类消息
双击:onDoubleTap(MotionEvent e)
双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent e)
单击确认:onSingleTapConfirmed(MotionEvent e)
5、GestureOverlayView:
一种用于手势输入的透明覆盖层,可覆盖在其他控件的上方,也可包含其他控件。存在3个监听器接口:
GestureOverlayView.OnGestureListener 手势监听器
GestureOverlayView.OnGesturePerformedListener 手势执行监听器
GestureOverlayView.OnGesturingListener 手势执行中监听器
一些常见的XML属性设置:
android:eventsInterceptionEnabled 当手势已经被识别出来时,是否拦截该手势动作
android:fadeDuration 当用户画完手势效果淡出的时间
android:fadeEnabled 用户画完之后手势是否自动淡出
android:gestureColor=”#000000” 手势的颜色
android:gestureStrokeType=”multiple” 笔画的类型
android:gestureStrokeWidth=”2dp” 笔画的粗细
使用步骤:
(1)使用Gesture Builder 应用程序生成手势文件
(2)导入生成的手势文件
(3)匹配识别手势
二、简单手势GestureDetector的使用
package com.sqb.demo8;
import com.sqb.app_im3.R;
import android.app.Activity;
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 Activity8Gesture extends Activity{
private ImageView img;
private GestureDetector myGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout8gesture);
img = (ImageView) findViewById(R.id.iv8_1);
//myGestureDetector = new GestureDetector(new myGestureListener());//过时了
myGestureDetector = new GestureDetector(this,new myGestureListener());
img.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
myGestureDetector.onTouchEvent(event);
return true;
}
});
}
class myGestureListener extends SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX()-e2.getX() >50){
Toast.makeText(Activity8Gesture.this, "从右向左滑", Toast.LENGTH_SHORT).show();
}else if(e2.getX()- e1.getX() >50){
Toast.makeText(Activity8Gesture.this, "从左向右滑", Toast.LENGTH_SHORT).show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
}
三、复杂手势GestureOverlayView的使用
1、在Eclipse创建内置的simple demo
(1)File-->new-->Other-->Android-->Android Sample Project-->Build Target 2.3.3也行-->GestureBuilder,然后Eclipse里面就多了一个GestureBuilder项目了,把项目运行到虚拟机上。
2、创建手势文件并导入到新的项目中:
(1)打开模拟器,打开刚才从Eclipse编译安装的Gesture Builder软件
(2)点击Add gesture新建手势
(3)将手势识别库从sdcard卡中导出,在res文件下新建raw文件,将导出的gesture文件粘贴到raw文件下
3、编写代码:
(1)Activity类:
package com.sqb.demo8;
import java.util.ArrayList;
import com.sqb.app_im3.R;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.Toast;
public class Activity8GestureOverlayView extends Activity{
private GestureOverlayView gov;
private GestureLibrary gl;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout8gestureoverlayview);
//从资源文件中将手势库加载进来
gl = GestureLibraries.fromRawResource(this, R.raw.gestures);
gl.load();
//从xml中取出GestureOverlayView控件
gov = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
//为GestureOverlayView控件添加监听
gov.addOnGesturePerformedListener(new OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
//识别手势,返回一个类型为Prediction的列表
ArrayList list = gl.recognize(gesture);
Prediction pre = list.get(0);
//如果匹配度大于1,表示可以识别,否则提示无法识别
if(pre.score > 1){
//判断名字是否与手势库的名字相同
if(pre.name.equals("haha")){
Toast.makeText(Activity8GestureOverlayView.this, "Recognize the haha", Toast.LENGTH_SHORT).show();
}else if(pre.name.equals("xin")){
Toast.makeText(Activity8GestureOverlayView.this, "Recognize the xin", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(Activity8GestureOverlayView.this, "Can't Recognize", Toast.LENGTH_SHORT).show();
}
}
});
}
}
参考资料:
GestureOverlayView手势识别:http://blog.csdn.net/zlqqhs/article/details/8541507