Android 手势识别的实现方法

手势识别系统:先把手势库放到项目中:(创建手势库见下一篇博客)在res文件夹下新建一个名为raw的文件夹,然后把手势库放进去 然后开始项目的创建: strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="app_name">GestureTest</string>
    <string name="notrecognize">没有手势</string>
    <string name="noprediction">手势识别率太低,请重新输入</string>
    <string name="noloading">手势库没有加载成功</string>
 
</resources>

main.xml:

<?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/myGesture"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        />
 
</LinearLayout>


GestureTestActivity.java:

package cn.csdn.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.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
 
public class GestureTestActivity extends Activity {
 
        GestureOverlayView gestureView;
        GestureLibrary gLibrary;
        boolean loadState;
 
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
 
                init();
        }
   
        private void init() {
                gestureView = (GestureOverlayView) this.findViewById(R.id.myGesture);
                gestureView
                                .addOnGesturePerformedListener(new MyOnGesturePerformedListener());
 
                // 创建首饰库对象GestureLibrary
                gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
                // 加载手势库资源
                loadState = gLibrary.load();
        }
 
        private final class MyOnGesturePerformedListener implements
                        OnGesturePerformedListener {
 
                public void onGesturePerformed(GestureOverlayView overlay,
                                Gesture gesture) {
                        if (loadState) {//加载手势资源成功
                                // 获取画的图形进行匹配,匹配程度就是Prediction中的score
                                ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
 
                                if (!predictions.isEmpty()) {// 如果用户画了图形,就会匹配
 
                                        Prediction prediction = predictions.get(0);
                                        Log.i("TAG", String.valueOf(prediction.score));
                                        if (prediction.score > 5) {// 判断相似度大于1,与里面的两者进行匹配
                                                if ("close".equals(prediction.name)) {//关闭
                                                        finish();
                                                } else if ("dialto".equals(prediction.name)) {//打电话
                                                        Intent intent = new Intent(Intent.ACTION_CALL,
                                                                        Uri.parse("tel:11111111111"));
                                                        startActivity(intent);
                                                }
                                        } else {// 相似度小于1,不识别
                                                showToast(R.string.noprediction);
                                        }
                                } else {//没有画图形
                                        showToast(R.string.notrecognize);
                                }
                        } else {
                                showToast(R.string.noloading);
                        }
                }
        }
 
        private void showToast(int tesId) {
                Toast.makeText(this, tesId, Toast.LENGTH_LONG).show();
        }
}


效果图:(必须画的比较精确)

0_13255721818LlP.gif

如果画c形状的话,会退出这个程序

0_1325572185nF5Y.gif

如果画一个对钩的话,会去进行拨号的操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值