Quoted Http://www.cnblogs.com/android100/p/android-hand.html http://blog.csdn.net/jiangshide/article/details/6293017
The identification and creation of Android gestures. The use of the Android.gesture package, the specific example refers to the sample in the Gesturebuilder program, using the SDK under the Gesturebuilder to generate a potential gestures file, Then we copy the gestures file to the new project. Next NEW:/res/raw/gestures, implement gesture detection:
The Main.xml file is set up as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" Fill_parent " Android:layout_height= "Fill_parent" > <android.gesture.GestureOverlayView Android:id = "@+id/gestures" android:layout_width= "Fill_parent" android:layout_height= " 0dip " android:layout_weight=" 1.0 " android:gesturestroketype=" multiple " / ></LinearLayout>
Its configuration character parameter is Strings.xml set as follows:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "Hello" >hello world, mainactivity!</string> <string name= "app_name" > Gesture recognition 2</string> <string name= " Norecohnize "> does not recognize this gesture </string> <string name=" nopediction "> Gesture recognition is too low, please re-enter </string></ Resources>
SRC under the Mainactivity.jar source code is: the main implementation of the function is to dial the designated person by a gesture to achieve the call and then through a gesture to achieve the exit function,
Packagecom.jsd.gesture;Importjava.util.ArrayList;Importandroid.app.Activity;Importandroid.content.Intent;Importandroid.gesture.Gesture;Importandroid.gesture.GestureLibraries;Importandroid.gesture.GestureLibrary;ImportAndroid.gesture.GestureOverlayView;Importandroid.gesture.Prediction;ImportAndroid.gesture.GestureOverlayView.OnGesturePerformedListener;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Privategesturelibrary Libraray; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Libraray= Gesturelibraries.fromrawresource ( This, r.raw.gestures);//load Gesture Library ObjectsLibraray.load ();//Load Gesture LibraryGestureoverlayview Overlayview = (Gestureoverlayview) This. Findviewbyid (R.id.gestures); Overlayview.addongestureperformedlistener (NewGesturelistener ()); } Private Final classGesturelistenerImplementsongestureperformedlistener{@Override Public voidongestureperformed (Gestureoverlayview overlay, Gesture Gesture) {//TODO auto-generated Method Stubarraylist<prediction> predictions = libraray.recognize (gesture);//identify whether a user-entered gesture exists in the gesture library if(!Predictions.isempty ()) {Prediction Prediction= Predictions.get (0);//get a matching gesture if(Prediction.score > 3){ if("Close". Equals (Prediction.name)) { //Close the appfinish (); }Else if("Phone". Equals (Prediction.name)) { //designate a person to callIntent Intent =NewIntent (Intent.action_call,uri.parse ("tel:18601159149")); StartActivity (Intent); } }Else{toast.maketext (mainactivity). This, R.string.nopediction, 1). Show (); } }Else{toast.maketext (mainactivity). This, R.string.norecohnize, 1). Show (); } } } /*** There are three ways to call it off in this method: * 1. Get its process ID to kill the process: Recommended use: Android.process.killProcess (android.os.Process mypid ( )); * 2. Terminate the running Java Virtual machine, which causes the program to terminate: System.exist (0); * 3. Force closure of all executions related to the newspaper: Acitvitymanager manager = (Activitymanager) getsystemservice (Context.activity_service); Manager.restartpackage (Getpackagename ()); <uses-permission android:name= "Android.permission.RESTART_PACKAGES" />*/ Private voidOndstroy () {//TODO auto-generated Method StuAndroid.os.Process.killProcess (Android.os.Process.myPid ());//when the activity is destroyed, we kill the app. }}
Code Explanation:
Gesture Recognition:
1. What is gesture recognition technology: A person who uses a finger to draw some symbols on the screen to represent a business that needs to be manipulated, such as drawing a circle to make a call to someone.
2. Create a gesture cry: a data store similar to a database, that is, some gesture symbols. Look at an example: Sdk/samples/android-8/gesturebuilder, the establishment of a good gesture library will exist on the SD card, the default file name is: gestures.
3. According to the user input gesture after the judgment, if its database has a corresponding gesture to return,
4. Create a new directory for static files under Res Raw, copy the gesture library file to its directory file, and of course it will generate a constant reference to the file in the R class under the Gen directory.
5. Then write in Main.xml:
<android.gesture.GestureOverlayView android:id= "@+id/gestures" android:layout_width = "Fill_parent" android:layout_height= "0dip" android:layout_weight= "1.0"/>
With the above controls, the user draws the corresponding symbol on the phone with the finger, and then it needs to be referenced in the code: Gestureoverlayview
Overlayview = (Gestureoverlayview) This.findviewbyid (r.id.gestures);
6. Add a listener event after the gesture has been drawn: Overlayview.addongestureperformedlistener (New Gesturelistener ());
7. Provide a class to implement the interface for gesture monitoring: Private Final class Gesturelistener implements
ongestureperformedlistener{
When the user finishes drawing, it gives the user a parameter to pass in its method
public void ongestureperformed (Gestureoverlayview overlay,gesture Gesture) {//method to Implement Interface
The following need to determine whether the gesture exists in the database and its accuracy is required, the need to load the gesture library this class:
arraylist<prediction> prediction = libraray.recognize (gesture);//Identify whether the user input gesture exists in the gesture library, and return all gestures similar to this gesture, And it puts the most similar gestures at the top, meaning the first in this opportunity
Element similarity is the highest, now only the most similar gestures are needed:
if(!Predictions.isempty ()) {Prediction Prediction= Predictions.get (0);//get the most matching gesture if(Prediction.score) {//Judging similarity: 0~10 >40% can if("Close". Equals (Prediction.name)) { //Close the app: 1. First get the current process ID and then kill the process (it is recommended to makeuse): Android. Process.killprocess (android.os.Process mypid ());
2terminates the currently running Java Virtual machine, causing the program to terminate: System.exit (0);
3. Force close everything associated with the package execution: Activitymanager manager =(Activitymanager) Getsystemservice (Context.activity_service); Manager.restartpackage (GetPackageName ());<uses-Permission Android:name= "Android.permission.RESTART_PACKAGERS"/>It is important to note that we cannot directly use one of these three methods here, and if so the Ondestory () method of the activity cannot be called to shut down normally, but we can call finish () here. method to allow its activity to shut down gracefully and then invoke it in the trigger Ondestory ()}Else if("Phone". Equals (Prediction.name)) { //designate a person to call } }Else{toast.maketext (mainactivity). This, r.string/nopediction,1). Show; } }Else{toast.maketext (mainactivity). This, r.string.norecognize,1). Show; } }}
Android Gesture Recognition Learning