Android Gesture gesture creation and use example

Source: Internet
Author: User

in the Android1.6 simulator is preloaded with a program called gestures Builder, this program is to let you create your own gestures (gestures Builder's source code in the SDK asked samples inside there, interested to see)

Copy the above four files to your project directory,

Run the project file above the emulator and create some gesture files on the emulator, for example:

The created gesture will be saved to the/mnt/sdcard/gestures, and a new test gesture project file will be built and the gestures file copied to the raw file in the Res directory,

Then configure the XML file with the following XML configuration:

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9tools:context= "Com.xunfang.gesture.MainActivity" >Ten  One<Android.gesture.GestureOverlayView AAndroid:id= "@+id/gv" -Android:layout_width= "Match_parent" -android:layout_height= "Match_parent" theAndroid:background= "#000000" -Android:gesturestrokewidth= "10" -Android:gesturecolor= "#ff0000" -/> +     -  +</RelativeLayout>

Gestureoverlayview: A transparent overlay for gesture input that can be overridden on top of other controls, or can contain other controls.
Android:gesturestroketype defines the type of strokes (defined as gestures)
Android:gesturestrokewidth the width of the stroke when drawing gestures
Activity file content is as follows

1 Packagecom.xunfang.gesture;2 3 Importjava.util.ArrayList;4 5 Importandroid.app.Activity;6 Importandroid.content.Intent;7 Importandroid.gesture.Gesture;8 Importandroid.gesture.GestureLibraries;9 Importandroid.gesture.GestureLibrary;Ten ImportAndroid.gesture.GestureOverlayView; One Importandroid.gesture.Prediction; A ImportAndroid.net.Uri; - ImportAndroid.os.Bundle; - Importandroid.view.MotionEvent; the ImportAndroid.widget.Toast; - - Public classMainactivityextendsActivity { - + PrivateGestureoverlayview GV; - + Private BooleanLoadstatus; A at Privategesturelibrary gesturelibrary; - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - in //Get Control -GV =(Gestureoverlayview) Findviewbyid (R.ID.GV); to + //Create a tool to load the gesture library -Gesturelibrary = Gesturelibraries.fromrawresource ( This, r.raw.gestures); the //Load Gesture Library *Loadstatus =gesturelibrary.load (); $ Panax Notoginseng //add a listener to the GV control - //Ongestureperformedlistener Listener monitors a gesture (end of stroke) theGv.addongestureperformedlistener (NewGestureoverlayview.ongestureperformedlistener () { + A @Override the Public voidongestureperformed (Gestureoverlayview overlay, + Gesture Gesture) { - $ //If the gesture library is loaded successfully $ if(loadstatus) { - //recognition gesture prediction is a similarity object in which the similarity in the collection is arranged from high to low -arraylist<prediction> pres =gesturelibrary.recognize (gesture); the if(!Pres.isempty ()) { - //get the object with the highest degree of similarityWuyiPrediction pre = Pres.get (0) ; the //percent >60% by number of integers - if(Pre.score > 6){ Wu //get the name of the gesture, judge the next logic. - if("94". Equals (Pre.name)) { About //that means you want to turn off your current activity $ finish (); -}Else if("Yes". Equals (Pre.name)) { - //It means you want to call. -Intent Intent =NewIntent (); A intent.setaction (intent.action_call); +Intent.setdata (Uri.parse ("tel://110")) ; the startactivity (intent); -}Else if("666". Equals (Pre.name)) { $ //It means you want to play a toast . theToast.maketext (mainactivity. This, "Haha, I bounced out", 0). Show (); the } the}Else{ theToast.maketext (mainactivity. This, "Gesture mismatch", 0). Show (); - } in}Else{ theToast.maketext (mainactivity. This, "Gesture library failed to load", 0). Show (); the } About } the } the }) ; the

This is used to dial the phone interface, be sure to add permissions, as shown in

After that, the code is played and can be tested.

I enter a 6

And then it bounced out. Indicates that the validation was successful.

Android Gesture gesture creation and use example

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.