Android+ Gesture Recognition Detailed

Source: Internet
Author: User

Today to the previous learning articles and experience simple summary out, here I directly put the code down, I hope to give beginners to do the best learning reference, but also hope that more developers to join the Android development team, participate in more innovative ways of development, good, Today, I would like to briefly explain the most basic of gesture recognition and the most need to master a technology node, because he can give us in the development of the most up-to-date user experience effect, such as gesture recognition, you only need a simple gesture action can be done you want to accomplish something that may be more complex, If you make a phone call with a gesture, you don't need to find the phone number you need to be a hassle, such as if you can sign in directly to an app you've set up, or sign in to a website, and set up an action response that you want to make certain actions on an app. However, we have to do this can not ignore the first thing we need to do is you have to complete a simple gesture operation, the world is the most difficult to learn the program is to write the first hello,world! program and print out, of course, if you do so, it is just the beginning of the rhythm, And then we have to double to learn and fully understand why it can achieve such an effect, out of the most basic operations can be done there is something else, is it possible to add some additional thinking on this basis, OK, in order to achieve this goal, we first to complete a basic gesture recognition, 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 function of exit, here I combined with a gesture case under the SDK directly to operate the implementation process, create a gesture library process, to see d:/toolss/ Android-sdk-windows/samples/android-8/gesturebuilder This gesture case, OK, I first use the SDK under the Gesturebuilder to generate two of gestures placed in the library/mnt/sdcard/ LOST. Dir/gestures file, and then we copy the gestures file to the new project next to the new:/res/raw/gestures, and then set the following

in the Main.xml file.

<?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:

<?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 percentage is too low, please re-enter </string>
</resources>
The source code under SRC is:

Package com.jsd.gesture;

Import java.util.ArrayList;

Import android.app.Activity;
Import android.content.Intent;
Import Android.gesture.Gesture;
Import android.gesture.GestureLibraries;
Import Android.gesture.GestureLibrary;
Import Android.gesture.GestureOverlayView;
Import android.gesture.Prediction;
Import Android.gesture.GestureOverlayView.OnGesturePerformedListener;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.widget.Toast;

public class Mainactivity extends Activity {
Private Gesturelibrary Libraray;
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Libraray = Gesturelibraries.fromrawresource (this, r.raw.gestures);//Load Gesture Library Object
Libraray.load ();//Load Gesture Library
Gestureoverlayview Overlayview = (gestureoverlayview) This.findviewbyid (r.id.gestures);

Overlayview.addongestureperformedlistener (New Gesturelistener ());
}

Private Final class Gesturelistener implements ongestureperformedlistener{

@Override
public void ongestureperformed (Gestureoverlayview overlay,
Gesture Gesture) {
TODO auto-generated Method Stub
arraylist<prediction> predictions = libraray.recognize (gesture);//Identify whether the user input gesture exists in the gesture library
if (!predictions.isempty ()) {
Prediction prediction = predictions.get (0);//Get matching gestures
if (Prediction.score > 3) {
if ("Close". Equals (Prediction.name)) {
Close the app
Finish ();
}else if ("Phone". Equals (Prediction.name)) {
Designate a person to call
Intent Intent = new Intent (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 ();
}
}

}
/**
* In this method to invoke its close
* There are three ways to close an application:
* 1. Get its process ID to kill the process: Referral 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 void Ondstroy () {
TODO Auto-generated Method Stu
Android.os.Process.killProcess (Android.os.Process.myPid ());//When the activity is destroyed, we kill the application.

}
}

Above all the code of this application, of course, if there is a code that does not understand, please download the source code, the above is I paste directly, then the following their own ideas to write the code and comments together, I hope that beginners can also be based on their own ideas to write their own comments and understand the results of the harvest:

Gesture Recognition:
1. What is gesture recognition technology: If a person 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 represent the

Someone calls and waits.
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, building a good

The 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, the R class in the Gen directory.

A constant reference to the file is generated in the
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 a user-entered gesture is stored

In the gesture library, and return all gestures similar to this gesture, and it will put the most similar gesture at the top, that is, in this opportunity of the first

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) {//Judgment similarity: 0~10 >40% can be
if ("Close". Equals (Prediction.name)) {
Close the app: 1. First get the current process ID and then kill the process (it is recommended to make

Use): Android. Process.killprocess (android.os.Process mypid ()); 2. Terminates the currently running Java Virtual machine, causing the program to end

Stop: system.exit (0); 3. Forcibly closes everything associated with the package execution: Activitymanager manager = (Activitymanager)

Getsystemservice (Context.activity_service); Manager.restartpackage (Getpackagename ()); <uses-permission

Android:name= "Android.permission.RESTART_PACKAGERS"/>; here it is important to note that we cannot directly use one of these three methods here

The Ondestory () method of the activity cannot be called so that it does not shut down properly, but we can call the finish () method here.

To allow its activity to shut down normally 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;
}
}
}
Problem: Most gestures are complete, but the requirements may take two or more to complete, which can be set using the Gesturestroketype property

Reset: android:gesturestroketype= "multiple"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.