Android 手势识别详解

来源:互联网 发布:魔王神官2知轩下载 编辑:程序博客网 时间:2024/05/16 08:25
第一步:写布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


 <android.gesture.GestureOverlayView
     android:id="@+id/gov_gesture"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1"
     android:gestureStrokeType="multiple"
     ></android.gesture.GestureOverlayView>
<Button 
    android:id="@+id/bt_yesid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="确定"
    android:onClick="clickyes"
    />
<Button 
    android:id="@+id/bt_noid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="取消"
    android:onClick="clickno"
    />
</LinearLayout>


第二步:在res资源文件目录下建一个raw文件夹来存放手势库文件


第三步:写Activity


package com.geek.gesture;


import java.util.ArrayList;


import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {
    /** Called when the activity is first created. */
GestureOverlayView gov_gesture;
Gesture gesture;
GestureLibrary library;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
private void init() {
// TODO Auto-generated method stub
gov_gesture=(GestureOverlayView) findViewById(R.id.gov_gesture);
   //加载手势库
   library=GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);
   library.load();
   //添加手势控件的监听事件
   gov_gesture.addOnGestureListener(new MyOnGestureListtener());
}
//手势控件的监听事件类
class MyOnGestureListtener implements OnGestureListener{


@Override
public void onGestureStarted(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub

}


@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub

}
        //手势结束
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
//拿到手势
gesture=overlay.getGesture();
}


@Override
public void onGestureCancelled(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub

}


}
//确定
public void clickyes(View view){
//用拿到的手势到手势库里匹配,返回集合
ArrayList<Prediction>  predictions= library.recognize(gesture);
//如果集合不为空就表示匹配到了
if(!predictions.isEmpty()){
//拿到匹配到的第一个手势
Prediction prediction=predictions.get(0);
//拿到匹配的相似度
double score=prediction.score;
//如果相似度大于等于5
if(score>=5){
//如果匹配到的手势名称是打电话
if(prediction.name.equals("打电话")){
//就跳到拨号页面
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
}
//如果匹配到的手势名称是对
else if(prediction.name.equals("对")){
Toast.makeText(MainActivity.this, "对", 2000).show();
}
//如果匹配到的手势名称是错
else if(prediction.name.equals("错")){
Toast.makeText(MainActivity.this, "错", 2000).show();
}
}else{
//如果相似度小于5
Toast.makeText(MainActivity.this, "手势匹配度太低", 2000).show();
}
}else{
//如果集合为空就表示没有匹配到
Toast.makeText(MainActivity.this, "无法识别", 2000).show();
}
//清除手势控件里面的手势
     gov_gesture.clear(true);
}
//取消
public void clickno(View view){
//清除手势控件里面的手势
gov_gesture.clear(true);
}
    
}
0 0
原创粉丝点击