mainActivity如下:
package cn.c;import java.io.File;import java.util.ArrayList;import android.app.Activity;import android.gesture.Gesture;import android.gesture.GestureLibraries;import android.gesture.GestureLibrary;import android.gesture.GestureOverlayView;import android.gesture.GestureOverlayView.OnGesturePerformedListener;import android.gesture.Prediction;import android.os.Bundle;import android.os.Environment;import android.view.KeyEvent;import android.view.View;import android.view.View.OnKeyListener;import android.widget.Button;import android.widget.EditText;/** * 需求描述: * 在例子1的基礎上增加手勢識別. * 所以不用再寫添加手勢的步驟. * 錯誤總結: * 在手勢識別的時候沒有執行mGestureLibrary.load() * 導致mGestureLibrary.recognize()方法失敗. * 該錯誤很類似於剛開始操作 SharedPreferences的時候 * Editor沒有執行commit()方法 * */public class MainActivity extends Activity { private EditText mEditText; private GestureOverlayView mGestureOverlayView; private GestureLibrary mGestureLibrary; private Gesture mGesture; private Button mSaveButton; private Button mResetButton; private String mGestureLibraryPath; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); }private void init() {mEditText = (EditText) findViewById(R.id.editText);mEditText.setOnKeyListener(new OnKeyListener() {public boolean onKey(View v, int keyCode, KeyEvent event) {if (mGesture!=null&&mEditText.getText().toString().length()>0) {mSaveButton.setEnabled(true);} else {mSaveButton.setEnabled(false);}return false;}});mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView);mSaveButton = (Button) findViewById(R.id.saveButton);mSaveButton.setEnabled(false);mResetButton = (Button) findViewById(R.id.resetButton);//GestureLibrary檔案的路徑mGestureLibraryPath =Environment.getExternalStorageDirectory()+File.separator+"gesturesTest";//為GestureOverlayView添加監聽事件mGestureOverlayView.addOnGesturePerformedListener(new GesturePerformedListenerImpl());}private class GesturePerformedListenerImpl implements OnGesturePerformedListener{public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {// 載入該路徑下的手勢庫.// 若不存在,則會在路徑下建立一個手勢庫mGestureLibraryPath =Environment.getExternalStorageDirectory()+File.separator+"gesturesTest";mGestureLibrary = GestureLibraries.fromFile(mGestureLibraryPath);//載入手勢庫//一定要執行load()方法,否則recognize()沒有用!if (mGestureLibrary.load()) {ArrayList<Prediction> predictionsList=mGestureLibrary.recognize(gesture);if (predictionsList.size()>0) {Prediction bestPrediction=predictionsList.get(0);if (bestPrediction.score>7.0) { System.out.println("xxxx1111 該手勢已經存在");}} else { System.out.println("xxxx1111 該手勢不存在");}} else {System.out.println("xxxx1111 載入手勢庫失敗");}}}}
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:orientation="horizontal" > <TextView android:id="@+id/textView" android:layout_width="100dip" android:layout_height="wrap_content" android:text="手勢的名稱:" /> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="在此輸入" /> </LinearLayout> <android.gesture.GestureOverlayView android:id="@+id/gestureOverlayView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/linearLayout" android:layout_marginBottom="50dip" android:gestureStrokeType="multiple" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dip" android:layout_alignParentBottom="true" android:orientation="horizontal" > <Button android:id="@+id/saveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="儲存" /> <Button android:id="@+id/resetButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="重設" /> </LinearLayout> </RelativeLayout>