`
hyshucom
  • 浏览: 808075 次
文章分类
社区版块
存档分类
最新评论

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. <string name="app_name">GestureTest</string>
  4. <string name="notrecognize">不能识别该手势</string>
  5. <string name="noprediction">手势识别率太低,请重新输入</string>
  6. </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. <android.gesture.GestureOverlayView
  7. android:id="@+id/myGestureView"
  8. android:layout_width="fill_parent"
  9. android:layout_height="0dip"
  10. android:layout_weight="1.0"
  11. />
  12. </LinearLayout>


Java文件:

  1. package cn.class3g.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 getstureView;
  17. GestureLibrary gLibrary;
  18. boolean loadState = true;
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. init();
  23. }
  24. private void init() {
  25. getstureView = (GestureOverlayView) this
  26. .findViewById(R.id.myGestureView);
  27. getstureView
  28. .addOnGesturePerformedListener(new MyOnGesturePerformedListener());
  29. // 创建手势库对象GestureLibrary
  30. gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
  31. // 加载手势库资源
  32. loadState = gLibrary.load();
  33. }
  34. private final class MyOnGesturePerformedListener implements
  35. OnGesturePerformedListener {
  36. public void onGesturePerformed(GestureOverlayView overlay,
  37. Gesture gesture) {
  38. if (loadState) {
  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的话太容易失败,本人测试5比较合适
  44. if ("close".equals(prediction.name)) {
  45. finish();//实现关闭功能,用的是activity自带的
  46. } else if ("dialto".equals(prediction.name)) {
  47. //实现播出固定电话的功能,记得添加权限
  48. Intent intent = new Intent(Intent.ACTION_CALL,
  49. Uri.parse("tel:13333333333"));
  50. startActivity(intent);
  51. }
  52. }
  53. } else {
  54. showToast(R.string.notrecognize);
  55. }
  56. } else {
  57. showToast(R.string.noprediction);
  58. }
  59. }
  60. }
  61. private void showToast(int resId) {
  62. Toast.makeText(this, resId, Toast.LENGTH_LONG).show();
  63. }
  64. }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics