zoukankan      html  css  js  c++  java
  • Android成长之路-手势识别的实现

     

    手势识别系统:

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

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

     

    然后开始项目的创建:

     

    strings.xml:

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.   
    4.     <string name="app_name">GestureTest</string>  
    5.     <string name="notrecognize">没有手势</string>  
    6.     <string name="noprediction">手势识别率太低,请重新输入</string>  
    7.     <string name="noloading">手势库没有加载成功</string>  
    8.   
    9. </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.   
    7.     
    8.     <android.gesture.GestureOverlayView   
    9.         android:id="@+id/myGesture"  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="0dip"  
    12.         android:layout_weight="1.0"  
    13.         />  
    14.   
    15. </LinearLayout>  


     

    GestureTestActivity.java:

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


     

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

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

     

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

  • 相关阅读:
    CF676E:The Last Fight Between Human and AI
    BZOJ2079: [Poi2010]Guilds
    BZOJ4518: [Sdoi2016]征途
    BZOJ2216: [Poi2011]Lightning Conductor
    51nod1766 树上的最远点对
    洛谷P1257 平面上的最接近点对
    BZOJ2144: 跳跳棋
    BZOJ4773: 负环
    BZOJ4552: [Tjoi2016&Heoi2016]排序
    The Falling Leaves(建树方法)
  • 原文地址:https://www.cnblogs.com/xgjblog/p/3877921.html
Copyright © 2011-2022 走看看