Android成长之路-手势识别的实现

时间:2023-03-09 16:35:12
Android成长之路-手势识别的实现

手势识别系统:

先把手势库放到项目中:(创建手势库见下一篇博客)

在res文件夹下新建一个名为raw的文件夹,然后把手势库放进去

然后开始项目的创建:

strings.xml:

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

main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <android.gesture.GestureOverlayView
  7. android:id="@+id/myGesture"
  8. android:layout_width="fill_parent"
  9. android:layout_height="0dip"
  10. android:layout_weight="1.0"
  11. />
  12. </LinearLayout>

GestureTestActivity.java:

  1. package cn.****.gesture;
  2. import java.util.ArrayList;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.gesture.Gesture;
  6. import android.gesture.GestureLibraries;
  7. import android.gesture.GestureLibrary;
  8. import android.gesture.GestureOverlayView;
  9. import android.gesture.GestureOverlayView.OnGesturePerformedListener;
  10. import android.gesture.Prediction;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.widget.Toast;
  15. public class GestureTestActivity extends Activity {
  16. GestureOverlayView gestureView;
  17. GestureLibrary gLibrary;
  18. boolean loadState;
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. init();
  23. }
  24. private void init() {
  25. gestureView = (GestureOverlayView) this.findViewById(R.id.myGesture);
  26. gestureView
  27. .addOnGesturePerformedListener(new MyOnGesturePerformedListener());
  28. // 创建首饰库对象GestureLibrary
  29. gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
  30. // 加载手势库资源
  31. loadState = gLibrary.load();
  32. }
  33. private final class MyOnGesturePerformedListener implements
  34. OnGesturePerformedListener {
  35. public void onGesturePerformed(GestureOverlayView overlay,
  36. Gesture gesture) {
  37. if (loadState) {//加载手势资源成功
  38. // 获取画的图形进行匹配,匹配程度就是Prediction中的score
  39. ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
  40. if (!predictions.isEmpty()) {// 如果用户画了图形,就会匹配
  41. Prediction prediction = predictions.get(0);
  42. Log.i("TAG", String.valueOf(prediction.score));
  43. if (prediction.score > 5) {// 判断相似度大于1,与里面的两者进行匹配
  44. if ("close".equals(prediction.name)) {//关闭
  45. finish();
  46. } else if ("dialto".equals(prediction.name)) {//打电话
  47. Intent intent = new Intent(Intent.ACTION_CALL,
  48. Uri.parse("tel:11111111111"));
  49. startActivity(intent);
  50. }
  51. } else {// 相似度小于1,不识别
  52. showToast(R.string.noprediction);
  53. }
  54. } else {//没有画图形
  55. showToast(R.string.notrecognize);
  56. }
  57. } else {
  58. showToast(R.string.noloading);
  59. }
  60. }
  61. }
  62. private void showToast(int tesId) {
  63. Toast.makeText(this, tesId, Toast.LENGTH_LONG).show();
  64. }
  65. }

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

Android成长之路-手势识别的实现

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

Android成长之路-手势识别的实现

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