mainActivity如下:
package cn.c; import java.io.File; import java.util.ArrayList; import java.util.Set; import android.app.Activity; import android.gesture.Gesture; import android.gesture.GestureLibraries; import android.gesture.GestureLibrary; import android.gesture.GestureOverlayView; import android.gesture.GestureOverlayView.OnGestureListener; import android.gesture.Prediction; import android.os.Bundle; import android.os.Environment; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * 需求描述: * 绘制手势,如已经存在则提示;不存在则可保存 * 该例子是Android手势识别(一)(二)的综合 * 遇到的问题: * 利用mGestureOverlayView.clear(true)不能清除手势. * 解决办法: * mGestureOverlayView.clear(true) * mGestureOverlayView.cancelGesture(); * */ 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.setOnClickListener(new ButtonOnClickListenerImpl()); mSaveButton.setEnabled(false); mResetButton = (Button) findViewById(R.id.resetButton); mResetButton.setOnClickListener(new ButtonOnClickListenerImpl()); //GestureLibrary文件的路径 mGestureLibraryPath = Environment.getExternalStorageDirectory()+File.separator+"gestures9527"; //为GestureOverlayView添加监听事件 //多笔监听 mGestureOverlayView.addOnGestureListener(new GestureListenerImpl()); } private class GestureListenerImpl implements OnGestureListener{ //开始绘制手势 public void onGestureStarted(GestureOverlayView overlay,MotionEvent event) { mSaveButton.setEnabled(false); mGesture=null; } public void onGesture(GestureOverlayView overlay, MotionEvent event) { } //手势绘制结束 public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) { boolean isSaved = false; mGesture = mGestureOverlayView.getGesture(); mGestureLibrary = GestureLibraries.fromFile(mGestureLibraryPath); if (mGestureLibrary.load()) { ArrayList<Prediction> predictionsList = mGestureLibrary.recognize(mGesture); if (predictionsList.size() > 0) { System.out.println("xxxx1111 开始对比"); Prediction bestPrediction = predictionsList.get(0); if (bestPrediction.score > 5.0) { isSaved = true; mGestureOverlayView.clear(true); mGestureOverlayView.cancelGesture(); mSaveButton.setEnabled(false); mEditText.setText(""); Toast.makeText(MainActivity.this, "手势已存在",Toast.LENGTH_SHORT).show(); } } else { isSaved = false; System.out.println("xxxx1111 手势不存在"); } } else { System.out.println("xxxxx 1111手势库打开失败"); } // 1 手势不为空 // 2 手势名正确 // 3 手势未保存 // 保存手势 if (mGesture != null && mEditText.getText().toString().length() > 0&& !isSaved) { mSaveButton.setEnabled(true); } } public void onGestureCancelled(GestureOverlayView overlay,MotionEvent event) { } } private class ButtonOnClickListenerImpl implements OnClickListener { public void onClick(View v) { switch (v.getId()) { case R.id.resetButton: mGestureOverlayView.clear(true); mSaveButton.setEnabled(false); mEditText.setText(""); break; case R.id.saveButton: String gestureName = mEditText.getText().toString(); try { File gestureLibraryFile = new File(mGestureLibraryPath); // fromFile()加载该路径下的手势库. // 若不存在,则会在路径下创建一个手势库 mGestureLibrary = GestureLibraries.fromFile(mGestureLibraryPath); if (!gestureLibraryFile.exists()) { System.out.println("gestureLibraryFile 文件不存在"); mGestureLibrary.addGesture(gestureName, mGesture); if (mGestureLibrary.save()) { //只有执行完mGestureLibrary.save()的时候 //gestureLibraryFile文件才不为空 mSaveButton.setEnabled(false); mEditText.setText(""); mGestureOverlayView.clear(true); System.out.println("xxx 成功"); } else { System.out.println("xxx 失败"); } } else { if (!mGestureLibrary.load()) { System.out.println("xxxx1111 加载手势库失败"); } else { Set<String> gesturyies = mGestureLibrary.getGestureEntries(); // 删除已经存在的同名的手势 if (gesturyies.contains(gestureName)) { ArrayList<Gesture> list = mGestureLibrary.getGestures(gestureName); for (int i = 0; i < list.size(); i++) { mGestureLibrary.removeGesture(gestureName,list.get(i)); } } // 添加手势 mGestureLibrary.addGesture(gestureName, mGesture); // 判断手势是否保存成功 if (mGestureLibrary.save()) { mGestureOverlayView.clear(true); mSaveButton.setEnabled(true); mEditText.setText(""); System.out.println("xxxx1111 手势保存成功"); } else { System.out.println("xxxx1111 手势保存失败"); } } } } catch (Exception e) { } break; default: break; } } } }
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>