Android手势识别


第一步:建立手势库

使用SDK自带例子GestureBuilder建立手势库(位置:Android-sdk-windows\samples\android-8\GestureBuilder)。使用GestureBuilder之前,你需要恢复其到开发环境,然后进行编绎并部署到手机上。此时,就可以使用GestureBuilder建立手势库,生成的手势库文件在SCDard上,默认文件名称为:gestures

第二步:在应用中加载手势库文件,然后开发手势识别代码。

把手势库文件gestures文件拷贝到项目的res/raw目录下。然后在布局文件中添加用于手势绘制的View:

 <android.gesture.GestureOverlayView

    android:id="@+id/gestures"

    android:layout_width="fill_parent“ android:layout_height="0dip"

    android:layout_weight="1.0" />

为View添加手势监听事件:gestureOverlayView.addOnGesturePerformedListener();

得到手势库:mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);

加载手势库:mLibrary.load();

List<Prediction> predictions = mLibrary.recognize(gesture);//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的内容,匹配度高的结果放在最前面

大多数情况下,手势都是通过一笔完成。然而有一些特别的需求就需要通过多个笔画来实现,这时可以使用gestureStrokeType属性进行设置:android:gestureStrokeType="multiple"

String文件

  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.   
  8. </resources>  

布局文件

  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.     <android.gesture.GestureOverlayView   
  8.         android:id="@+id/myGestureView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="0dip"  
  11.         android:layout_weight="1.0"  
  12.         />  
  13.   
  14. </LinearLayout>  
  • 1
  • 2
  • 下一页

相关内容