android手势识别

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


?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
<?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:
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<?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:

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
packagecn.csdn.gesture;
importjava.util.ArrayList;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.gesture.Gesture;
importandroid.gesture.GestureLibraries;
importandroid.gesture.GestureLibrary;
importandroid.gesture.GestureOverlayView;
importandroid.gesture.GestureOverlayView.OnGesturePerformedListener;
importandroid.gesture.Prediction;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.widget.Toast;
publicclassGestureTestActivityextendsActivity {
GestureOverlayView gestureView;
GestureLibrary gLibrary;
booleanloadState;
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
privatevoidinit() {
gestureView = (GestureOverlayView)this.findViewById(R.id.myGesture);
gestureView
.addOnGesturePerformedListener(newMyOnGesturePerformedListener());
// 创建首饰库对象GestureLibrary
gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
// 加载手势库资源
loadState = gLibrary.load();
}
privatefinalclassMyOnGesturePerformedListenerimplements
OnGesturePerformedListener {
publicvoidonGesturePerformed(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();
}elseif("dialto".equals(prediction.name)) {//打电话
Intent intent =newIntent(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);
}
}
}
privatevoidshowToast(inttesId) {
Toast.makeText(this, tesId, Toast.LENGTH_LONG).show();
}
}


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


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


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

你可能感兴趣的:(android手势识别)