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>