Mainactivity is as follows:
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. ongesturew.medlistener; import andro Id. 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;/*** Requirement Description: * save a gesture to the gesture library * error Summary: * Error 1: * Description: After drawing a gesture in GestureOverlayView, this gesture will disappear in * and will not stay in Gestur * Cause: * the following properties are not set for GestureOverlayView in the layout file * android: gestureStrokeType = "multiple" * Error 2: the gestureLibraryFile and mGestureLibrary * of the gesture library are somewhat different. one-sided understanding: This is two independent things, or a mGestureLibrary is stored in * gestureLibraryFile. * It should be understood here: * mGestureLibrary is a file! * This type of file is not encountered before. It will not be reflected at half past one */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 (savedInstanceStat E); 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 (); // path of the GestureLibrary file mGestureLibraryPath = Environment. getExternalStorageDirectory () + File. separator + "testgestures"; // Add the listening event mGestureOverlayView for GestureOverlayView. addOnGestureListener (new GestureListenerImpl ();} private class GestureListenerImpl implements OnGestureListener {// start to draw the Gesture public void onGestureStarted (Gesture OverlayView overlay, MotionEvent event) {mSaveButton. setEnabled (false); mGesture = null;} public void onGesture (GestureOverlayView overlay, MotionEvent event) {}// stop 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); // load the gesture library under this path. // if it does not exist, a gesture library mGestureLibrary = GestureLibraries will be created in the path. fromFile (mGestureLibraryPath); if (! GestureLibraryFile. exists () {mGestureLibrary. addGesture (gestureName, mGesture); if (mGestureLibrary. save () {// only mGestureLibrary is executed. when saving (), the // gestureLibraryFile is not empty. // Therefore, if is executed only once, elsemSaveButton will be executed in the future. setEnabled (false); mEditText. setText (""); mGestureOverlayView. clear (true); System. out. println ("xxxx0000 successful");} else {System. out. println ("xxxx0000 failed") ;}} else {if (! MGestureLibrary. load () {System. out. println ("xxxx1111 failed to load the gesture library");} else {Set <String> gesturyies = mGestureLibrary. getGestureEntries (); // delete an existing gesture with the same name if (gesturyies. contains (gestureName) {ArrayList <Gesture> list = mGestureLibrary. getGestures (gestureName); for (int I = 0; I <list. size (); I ++) {mGestureLibrary. removeGesture (gestureName, list. get (I) ;}/// add gesture mGestureLibrary. addGesture (gestureName, mGesture); // determines whether the gesture is successfully saved if (mGestureLibrary. save () {mGestureOverlayView. clear (true); mSaveButton. setEnabled (true); mEditText. setText (""); System. out. println ("xxxx1111 gesture saved successfully");} else {System. out. println ("failed to save the xxxx1111 gesture") ;}}} catch (Exception e) {} break; default: break ;}}}}
Main. XML is as follows:
<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 = "gesture name:"/> <EditText android: id = "@ + id/editText" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: hint = "input here"/> </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: 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 = "save"/> <Button android: id = "@ + id/resetButton" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "reset"/> </LinearLayout> </RelativeLayout>