Steps:
1. Use gestures builder in the simulator to create a gesture. If the simulator does not have this, create an android sample project, select gestures builder, and run it.) create several gestures.
2. Export the created gestrues file in the SD card directory in ddms.
3. Create a raw directory and copy the gestrues file.
4. In the layout xml, use the gestures overlay view control.
5. Read the gesture library in the Code and import it To the memory.
6. Match the gesture and compare the gesture entered by the user with the library. Sort the gesture according to the similarity to monitor the event.
The Code is as follows:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // read the gesture library final GestureLibrary library = GestureLibraries. fromRawResource (this, R. raw. gestures); library. load (); // import the gesture library and load it To the memory. // match the GestureOverlayView overlayView = (GestureOverlayView) findViewById (R. id. gestureOverlay View1); overlayView. addongesturesponmedlistener (new ongesturesponmedlistener () {// gesture listening event @ Override // Gesture indicates the gesture public void ongesturesponmed (GestureOverlayView overlay, gesture Gesture) {ArrayList <Prediction> recognize = library. recognize (gesture); // The user-input gesture matches the gesture in the library to get the recognize container, which is sorted from high to low according to the matching similarity, 0 ~ 10 minutes Prediction prediction = recognize. get (0); // obtain the most similar if (prediction. score> = 5) // similar score {if ("toast ". equals (prediction. name) // gesture name {Toast. makeText (MainActivity. this, "toast", Toast. LENGTH_LONG ). show ();} else if ("exit ". equals (prediction. name) {finish () ;}}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
This article is from the "wangcuijing" blog, please be sure to keep this source http://wangcuijing.blog.51cto.com/7233352/1286594