识别各种手势
对于上面的两种接口,我们可以直接继承SimpleOnGestureListener,然后重载感兴趣的手势。
使用实例:
1、布局文件
2、实现手势识别
package com.cx.gesturedetectordemo;
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 MainActivity extends Activity {
private ImageView image;
private GestureDetector myGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
//实例化GestureDetector
myGestureDetector = new GestureDetector(this, new myOnGestureListener());
//增加监听事件
image.setOnTouchListener(new OnTouchListener() {
@Override//可以捕获触摸屏幕发生的Event事件
public boolean onTouch(View v, MotionEvent event) {
//使用GestureDetector转发MotionEvent对象给OnGestureListener
myGestureDetector.onTouchEvent(event);
return true;
}
});
}
class myOnGestureListener extends SimpleOnGestureListener{
@Override
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);
}
}
}
3、多个手指滑动识别。例如手机的UI界面,两个手指向内滑动→可操作界面,三个手指向内滑动→界面窗口视图。该内容为后来添加内容,下面源码中不包含该部分代码。
a.修改上面的ImageView的点击方法
image.setOnTouchListener(new OnTouchListener() {
@Override//可以捕获触摸屏幕发生的Event事件
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = 1;
break;
case MotionEvent.ACTION_UP:
mode = 0;
break;
case MotionEvent.ACTION_POINTER_UP:
mode -= 1;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);//两点按下时的距离
mode += 1;
break;
case MotionEvent.ACTION_MOVE:
if (mode == 2) {
float newDist = spacing(event);
//+-100是为了增加滑动幅度
if (newDist > oldDist+100) {
Toast.makeText(MainActivity.this, "两指分!", Toast.LENGTH_SHORT).show();
//清零是为了防止多次执行
mode = 0;
}else if (newDist < oldDist-100) {
Toast.makeText(MainActivity.this, "两指聚!", Toast.LENGTH_SHORT).show();
mode = 0;
}
}else if (mode == 3) {
float newDist = spacing(event);
if (newDist > oldDist+100) {
Toast.makeText(MainActivity.this, "三指分!", Toast.LENGTH_SHORT).show();
mode = 0;
}else if (newDist < oldDist-100) {
Toast.makeText(MainActivity.this, "三指聚!", Toast.LENGTH_SHORT).show();
mode = 0;
}
}
break;
}
return true;
}
});
b.增加计算移动距离的方法
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
1、生成手势文件
我们可以直接使用官方Demo来进行手势文件的生成,Demo位置:E:\eclipse\adt-bundle-windows-x86-20130219\sdk\samples\android-10\GestureBuilder
a.Demo导入:
需要new一个已经存在的项目,步骤如下
b.运行导入的项目,在应用中添加手势并生成手势文件。(生成的手势文件一般在SD卡的根目录)
c.将生成的手势文件放在新建工程的res/raw文件夹下
2、布局文件,添加GestureOverlayView控件
2、实现手势识别
package com.cx.gestureoverlayviewdemo;
import java.util.ArrayList;
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 MainActivity extends Activity {
private GestureOverlayView gestureOverlayView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
//从资源中将手势库文件加载进来
final GestureLibrary library = GestureLibraries.fromRawResource(this, R.raw.gestures);
library.load();
//增加手势执行监听器
gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
//读出手势库中的内容 识别手势
ArrayList myGesture = library.recognize(gesture);
Prediction prediction = myGesture.get(0);
//判断相似度0.0~10.0
if(prediction.score > 5.0){
//通过名字判断手势
if(prediction.name.equals("yes")){
Toast.makeText(MainActivity.this, "正确!", Toast.LENGTH_SHORT).show();
}else if(prediction.name.equals("no")){
Toast.makeText(MainActivity.this, "错误!", Toast.LENGTH_SHORT).show();
}else if(prediction.name.equals("next")){
Toast.makeText(MainActivity.this, "下一步!", Toast.LENGTH_SHORT).show();
}else if(prediction.name.equals("back")){
Toast.makeText(MainActivity.this, "上一步!", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "无法识别!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
源码下载