接着上一篇,学习一下怎么识别手势,
首先还是要向AndroidManifest.xml添加SD卡的读写权限
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 使用手势编辑组件 --> <android.gesture.GestureOverlayView android: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"?> <LinearLayout xmlns: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; @Override public 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() { @Override public 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(); } } }); } }