接着上一篇,学习一下怎么识别手势,
首先还是要向AndroidManifest.xml添加SD卡的读写权限

[html] view plain copy print ?
  1. <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
  2. <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 使用手势编辑组件 --><android.gesture.GestureOverlayViewandroid:id="@+id/gesture"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gestureStrokeType="multiple" /></LinearLayout>

result.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ListView android:id="@+id/show"android:layout_width="fill_parent"android:layout_height="fill_parent"/></LinearLayout>


主界面代码

package WangLi.IO.RecogniseGesture;import java.util.ArrayList;import android.app.Activity;import android.app.AlertDialog;import android.gesture.Gesture;import android.gesture.GestureLibraries;import android.gesture.GestureLibrary;import android.gesture.GestureOverlayView;import android.gesture.Prediction;import android.gesture.GestureOverlayView.OnGesturePerformedListener;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.Toast;public class RecogniseGesture extends Activity{// 定义手势编辑组件GestureOverlayView gestureView;// 记录手机上已有的手势库GestureLibrary gestureLibrary;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 读取上一个程序所创建的手势库gestureLibrary = GestureLibraries.fromFile("/sdcard/mygestures");if (gestureLibrary.load()){Toast.makeText(RecogniseGesture.this,"手势文件装载成功!" ,8000).show();}else{Toast.makeText(RecogniseGesture.this,"手势文件装载失败!" ,8000).show();}// 获取手势编辑组件gestureView = (GestureOverlayView) findViewById(R.id.gesture);// 为手势编辑组件绑定事件监听器gestureView.addOnGesturePerformedListener(new OnGesturePerformedListener(){@Overridepublic void onGesturePerformed(GestureOverlayView overlay,Gesture gesture){// 识别用户刚刚所绘制的手势ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture);ArrayList<String> result = new ArrayList<String>();//遍历所有找到的Prediction对象for (Prediction pred : predictions){// 只有相似度大于2.0的手势才会被输出if (pred.score > 2.0){result.add("与手势【" + pred.name + "】相似度为"+ pred.score);}}if (result.size() > 0){ArrayAdapter adapter = new ArrayAdapter(RecogniseGesture.this,android.R.layout.simple_dropdown_item_1line, result.toArray());// 使用一个带List的对话框来显示所有匹配的手势new AlertDialog.Builder(RecogniseGesture.this).setAdapter(adapter, null).setPositiveButton("确定", null).show();}else{Toast.makeText(RecogniseGesture.this,"无法找到能匹配的手势!" ,8000).show();}}});}}



更多相关文章

  1. 四大组件之Broadcast Receiver
  2. protected-broadcast 规范使用系统应用组件自定义广播
  3. Android之RadioGroup获取选中值
  4. android 中webview的屏幕适配问题
  5. ActivityManagerService 详解
  6. Android(安卓)SDK无法更新
  7. android 4组件之 provider--访问通讯录
  8. 搭建Android(安卓)CTS测试环境总结
  9. Android手机端加载电脑端网页

随机推荐

  1. Log4j2记录日志到数据库(MySQL&MongoDB)
  2. php mysql有条件地将行插入到不同的表中
  3. MySQL架构:空列与联接
  4. CentOS 6.4 安装 JAVA + MYSQL + APACHE
  5. 为什么准备好的语句由每个会话管理?
  6. 程序猿(媛)Shell脚本必备技能之一: 在Lin
  7. ThinkPHP2.1 读取SqlServer数据(并解决Sql
  8. 将MySQL用于OUTFILE以生成Excel可读的UTF
  9. 如何构建一个命令来连接mySQL中的3个不同
  10. 161205、win10安装mysql5.7.16数据库