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.GestureOverlayView.OnGesturePerformedListener; 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; /** * 需求描述: * 向手势库中保存一个手势 * 错误总结: * 错误1: * 描述:在GestureOverlayView画一个手势后,该手势会 * 里面消失,不会停留显示在GestureOverlayView控件上 * 原因: * 没有在布局文件中为GestureOverlayView设置以下属性 * android:gestureStrokeType="multiple" * 错误2:对于手势库文件gestureLibraryFile和手势库mGestureLibrary * 的理解有些偏差了.片面的理解为:这是两个独立的东西或者说 * gestureLibraryFile中存放了一个mGestureLibrary * 应该这里理解: * mGestureLibrary就是一个文件! * 只是以前没有遇到这种文件,一时半会没有反应过来 */ 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+"testgestures"; //为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) { mGesture=mGestureOverlayView.getGesture(); if (mGesture!=null&&mEditText.getText().toString().length()>0) { 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); // 加载该路径下的手势库. // 若不存在,则会在路径下创建一个手势库 mGestureLibrary = GestureLibraries.fromFile(mGestureLibraryPath); if (!gestureLibraryFile.exists()) { mGestureLibrary.addGesture(gestureName, mGesture); if (mGestureLibrary.save()) { //只有执行完mGestureLibrary.save()的时候 //gestureLibraryFile文件才不为空 //所以if只会执行一次,往后都执行else mSaveButton.setEnabled(false); mEditText.setText(""); mGestureOverlayView.clear(true); System.out.println("xxxx0000 成功"); } else { System.out.println("xxxx0000 失败"); } } 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>
时间: 2024-10-01 00:39:35