Android gesture recognition development-gesture auto-dialing

Source: Internet
Author: User

This paper mainly develops two features of Android gesture recognition

    • Recognition gesture Auto-dialing
    • Recognition gesture Close Program

First step: Create a gesture library
Use the SDK's own example Gesturebuilder to create a gesture library (location: Adt-bundle-windows-x86-20140321\sdk\samples\android-15\gesturebuilder). Before using Gesturebuilder, you need to restore it to your development environment, and then compile and deploy it to your phone. At this point, you can use Gesturebuilder to create a gesture library, the generated gesture library file on SDcard, the default file name is called: gestures.
Specific steps:
1. Locate the project Gesturebuilder in the SDK directory and copy the Gesturebuilder to the Android workspaces directory.
2. Create a new project gesture, copy the following three files under the gesture directory to Gesturebuilder root directory,

3. Import the Gesturebuilder into Eclipse, compile the deployment to run on the emulator, set up the gesture library as follows,


4. After the setup is complete, generate the gesture library file gestures in the SDcard directory and export the file gestures.

Step Two: Develop gesture recognition
Load the gesture library file gestures in the app and develop the gesture recognition code.
Copy the gesture library file gestures to the Res/raw directory of the project gesture. In this time, you can see the ID of gestures in the R file of the project gesture. Then add the view for gesture drawing in the layout file:

    <android.gesture.GestureOverlayView        android:id="@+id/gestures"        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:gestureStrokeType="multiple" />

In most cases, gestures are done through a single stroke. However, there are some special requirements that need to be implemented by multiple strokes, which can be set using the Gesturestroketype property: Android:gesturestroketype= "multiple".

Main.xml

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" fill_parent " android:layout_height  =" fill_parent " android:orientation  =;     <android.gesture.gestureoverlayview  android:id  =  Android:layout_width  = "fill_parent"  android: Layout_height  = "0DP"  android:layout_weight
       = "1"  android:gesturestroketype  = "multiple" />     <buttonandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:layout_weight="0"android:onclick="Find"android:text= "@string/recognize" />                                        </linearlayout>

Mainactivity.java

 PackageCn.itcast.gesture;ImportJava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.gesture.Gesture;ImportAndroid.gesture.GestureLibraries;ImportAndroid.gesture.GestureLibrary;ImportAndroid.gesture.GestureOverlayView;ImportAndroid.gesture.GestureOverlayView.OnGestureListener;ImportAndroid.gesture.GestureOverlayView.OnGesturePerformedListener;ImportAndroid.gesture.Prediction;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public  class mainactivity extends Activity {    Private Static FinalString TAG ="Mainactivity";PrivateGesturelibrary Library;PrivateGesture mgesture;PrivateGestureoverlayview Overlayview;@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.main); Library = Gesturelibraries.fromrawresource ( This, r.raw.gestures);//Get gesture LibraryLibrary.load ();//Load gesture LibraryOverlayview = (Gestureoverlayview) This. Findviewbyid (R.id.gestures);//For single gesture only: Overlayview.addongestureperformedlistener (New Gestureperformedlistener ());Overlayview.addongesturelistener (NewGesturelistener ());//Add Gesture listener events for view} Public void Find(View v)        {recognize (mgesture); Overlayview.clear (true); }Private Final  class Gesturelistener implements Ongesturelistener{         Public void ongesturestarted(Gestureoverlayview overlay, motionevent event) {LOG.I (TAG,"ongesturestarted ()"); } Public void ongesture(Gestureoverlayview overlay, motionevent event) {LOG.I (TAG,"Ongesture ()"); } Public void ongestureended(Gestureoverlayview overlay, motionevent event) {LOG.I (TAG,"ongestureended ()");        Mgesture = Overlay.getgesture (); } Public void ongesturecancelled(Gestureoverlayview overlay, motionevent event) {LOG.I (TAG,"ongesturecancelled ()"); }    }Private Final  class Gestureperformedlistener implements Ongestureperformedlistener{          Public void ongestureperformed(Gestureoverlayview overlay, Gesture Gesture)        {recognize (gesture); }           }Private void recognize(Gesture Gesture) {//Query the matching content from the gesture library, the matching result may include several similar content, the result of high match is put in front. arraylist<prediction> predictions = library.recognize (gesture);if(!predictions.isempty ()) {Prediction prediction = Predictions.get (0);if(Prediction.score >=6){//Match rate greater than 60%                if("Call". Equals (Prediction.name)) {Intent Intent =NewIntent (Intent.action_call, Uri.parse ("tel:1350505050"));                StartActivity (Intent); }Else if("Close". Equals (Prediction.name)) {finish ();//Close activity}            }Else{Toast.maketext (Getapplicationcontext (), R.string.low,1). Show (); }        }Else{Toast.maketext (Getapplicationcontext (), R.string.notfind,1). Show (); }    }@Override    protected void OnDestroy() {Super. OnDestroy (); Android.os.Process.killProcess (Android.os.Process.myPid ());//Close apps}}

The compilation is deployed to the emulator to run, and when the input gesture is ticked, the program actively closes the exit automatically. When your app is no longer in use, you usually need to close the app, and you can use three ways to turn off Android apps,

    • Gets the current ID and then kills the process (recommended)

Android.os.Process.killProcess (Android.os.Process.myPid ());//Close Application

    • Terminates the currently running Java Virtual machine, causing the program to terminate

System.exit (0);

    • Force shutdown of all executions associated with the package

Activitymanager manager = (Activitymanager) getsystemservice (Context.activity_service);
Manager.restartpackage (Getpackagename ());
< Uses-permisson android:name= "Android.permission.RESTART_PACKAGESS"/>

In addition, when the gesture Z is drawn, the automatic dialing

Using Android gesture recognition, you can develop features such as:

    • Unlock your phone password
    • App Shortcuts (dialing, opening programs, etc.)

Android gesture recognition development-gesture auto-dialing

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.