Android + Gesture Recognition

Source: Internet
Author: User

Today, I will simply summarize my previous learning articles and experiences. Here I will directly paste the code, hoping to provide the best learning reference for beginners, we also hope that more developers will join the android development team and participate in more innovative development methods, today, I will briefly explain a technical node that is the most fundamental and essential for Gesture Recognition, because it can provide us with the latest user experience in development, for example, to use Gesture Recognition, you only need a simple gesture operation to complete something that may be complicated, such as calling through a gesture, instead of looking for the phone number you need, this is a troublesome process. For example, you can use a gesture recognition tool to directly log on to an application you set up in advance, or you can log on to a website and set the operation response that you want to make certain actions on an application, however, to do this, we must not ignore the fact that the first thing we need to do is to complete the simplest gesture operation. The most difficult thing in the world is to write the first hello, world! Program and print it out. Of course, if you do it that way, it's just the beginning of its pace. Then we have to learn more and fully understand why it achieves this effect, are there any other things you can do to complete these basic operations? Can you add some additional ideas on this basis? OK, in order to achieve this purpose, let's first complete the most basic gesture recognition. The main function is to use a gesture to call a specified person's phone number and then use a gesture to exit, here, I will use a gesture case in the SDK to directly perform the implementation process, create a gesture library, and view it myself. D: /toolss/Android-SDK-Windows/samples/Android-8/gesturebuilder, first, I used gesturebuilder In the SDK to generate two gestures in the Library/mnt/sdcard/lost. in the Dir/gestures file, copy the gestures file to the new project:/RES/raw/gestures. the settings in the XML file are 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>

The configuration supporting 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"> this gesture cannot be recognized </string>
<String name = "nopediction"> the percentage of Gesture Recognition is too low. enter a new one. </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. ongesturew.medlistener;
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 the gesture library object
Libraray. Load (); // load the gesture Library
Gestureoverlayview overlayview = (gestureoverlayview) This. findviewbyid (R. Id. Gestures );

Overlayview. addongesturew.medlistener (New gesturelistener ());
}

Private final class gesturelistener implements ongesturesponmedlistener {

@ Override
Public void ongesturesponmed (gestureoverlayview overlay,
Gesture gesture ){
// Todo auto-generated method stub
Arraylist <prediction> predictions = libraray. Recognize (gesture); // identifies whether a user-input gesture exists in the gesture Library
If (! Predictions. isempty ()){
Prediction prediction = predictions. Get (0); // get the matching gesture
If (prediction. score> 3 ){
If ("close". Equals (prediction. Name )){
// Close the application
Finish ();
} Else if ("phone". Equals (prediction. Name )){
// Specify 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 ();
}
}

}
/**
* Call this method to disable it.
* There are three ways to disable an application:
* 1. Get the process ID to kill the process: Android. process. killprocess (Android. OS. Process mypid () is recommended ());
* 2. Terminate the running Java virtual machine, resulting in program termination: system. exist (0 );
* 3. forcibly disable all executions related to the report: 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, the application is killed.

}
}

All the code above for this application. If you do not know much about the code, please download the source code and paste it directly above, then paste the code and comments that you have written based on your own ideas. I hope that beginners can write their own comments based on their own ideas:

Gesture Recognition:
1. What is Gesture Recognition Technology? For example, a person uses a finger to draw some symbols on the screen to indicate a business that needs to be operated.

Someone calls and so on.
2. Create a gesture cry: similar to a database, that is, data storage of some gesture symbols. Let's look at an example: SDK/samples/Android-8/gesturebuilder.

The gesture library will exist on the SD card. The default file name is gestures.
3. Make a judgment based on the gesture entered by the user. If the database has the corresponding gesture, it will be returned,
4. Create a directory named raw under Res to store static files. Copy the gesture library file to its directory file. Of course, it will also be in the r class under the gen directory.

Generate a constant reference to the file
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"/>
Through the above control, the user will draw the corresponding symbol on the mobile phone through the finger, and then need to reference it in the Code: gestureoverlayview

Overlayview = (gestureoverlayview) This. findviewbyid (R. Id. Gestures );
6. Add a listener event after the gesture is drawn: overlayview. addongestureperformedlistener (New gesturelistener ());
7. Provides a class to implement the interface for gesture listening: private final class gesturelistener implements

Ongesturesponmedlistener {
// After the user draws the image, a parameter is provided to the user to pass in the method.
Public void ongesturesponmed (gestureoverlayview overlay, gesture) {// method for implementing the interface
// You need to judge whether the gesture exists in the Database and whether its precision meets the requirements. In this case, you need to load the gesture library class:
Arraylist <prediction> Prediction = libraray. Recognize (gesture); // identifies whether a user input gesture is saved

In the gesture library, return all gestures similar to this gesture, and place the gesture with the highest similarity at the beginning, that is, the first in this opportunity.

Element similarity is the highest. Now you only need the gesture with the highest similarity:
If (! Predictions. isempty ()){
Prediction prediction = predictions. Get (0); // get the most matched gesture
If (prediction. Score) {// similarity determination: 0 ~ 10> 40%
If ("close". Equals (prediction. Name )){
// Close the application: 1. first obtain the current process ID and then kill the process (we recommend that you

Use): Android. process. killprocess (Android. OS. Process mypid (); 2. Terminate the currently running Java virtual machine, resulting in the end of the program

Stop: system. Exit (0); 3. Force disable all executions associated with the package: activitymanager manager = (activitymanager)

Getsystemservice (context. activity_service); manager. restartpackage (getpackagename (); <uses-Permission

Android: Name = "android. Permission. restart_packagers"/>; note that we cannot directly use one of the three methods here.

In this case, the ondestory () method of the activity cannot be called so that it cannot be closed normally. However, we can call the finish () method here.

Before calling the ondestory ()

} Else if ("phone". Equals (prediction. Name )){
// Specify 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 completed in one stroke. However, two or more requests may be required. In this case, you can use the gesturestroketype attribute to set

Set: 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.