Android手势识别的实现
jopen
11年前
手势识别系统:
先把手势库放到项目中:(创建手势库见下一篇博客)
在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:
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(); } }
效果图:(必须画的比较精确)
如果画c形状的话,会退出这个程序
如果画一个对钩的话,会去进行拨号的操作
来自:http://blog.csdn.net/like7xiaoben/article/details/7172783