Android Create gesture recognition sample code _android

Source: Internet
Author: User
Tags time interval

This article is used in the content of the Android.gesture package, the specific example of a reference to the Gesturebuilder program in Sample.

1. Gesture Creation
Gesture creation is mainly used to Gestureoverlayview and gesturelibrary. The Gestureoverlayview parent class is android.widget.FrameLayout and is the gesture plot area. Gesturelibrary the main opponent potential to save, delete and other operations, storage gesture of the warehouse. Here is an example of creating gestures, as shown in the following diagram, which can be defined as figure gestures to open csdn.net

1.1. Create plot Area

Copy Code code as follows:

Gestureoverlayview overlay = (Gestureoverlayview) Findviewbyid (R.id.gestures_overlay);

Overlay.setgesturestroketype (gestureoverlayview.gesture_stroke_type_multiple);

Overlay.setfadeoffset (2000); Multiple strokes every two times in a time interval
Overlay.setgesturecolor (Color.cyan);//Brush color
Overlay.setgesturestrokewidth (6);//Brush thickness value

Overlay.addongesturelistener (New Gesturesprocessor ());

1.2. Monitor Drawing

Copy Code code as follows:

Private class Gesturesprocessor implements Gestureoverlayview.ongesturelistener {
public void ongesturestarted (Gestureoverlayview overlay, motionevent event) {
Mdonebutton.setenabled (FALSE);
Mgesture = null;
}

public void Ongesture (Gestureoverlayview overlay, motionevent event) {
}

public void ongestureended (Gestureoverlayview overlay, motionevent event) {
Mgesture = Overlay.getgesture ();
if (Mgesture.getlength () < Length_threshold) {
Overlay.clear (FALSE);
}
Mdonebutton.setenabled (TRUE);
}

public void ongesturecancelled (Gestureoverlayview overlay, motionevent event) {
}
}

1.3. Save gestures

Copy Code code as follows:

public void Addgesture (View v) {
if (mgesture!= null) {
Final TextView input = (TextView) Findviewbyid (r.id.gesture_name);
Final charsequence name = Input.gettext ();
if (name.length () = = 0) {
Input.seterror (getString (r.string.error_missing_name));
Return
}

           /**
              * Get gesture Library
              *   Private Final File mstorefile = new file (Environment.getexternalstoragedirectory (), " Gestures ");
             *   gesturelibrary sstore = Gesturelibraries.fromfile (Mstorefile);
             *
              */
             final Gesturelibrary store = Gesturebuilderactivity.getstore ();
            store.addgesture (name.tostring (), Mgesture);
            Store.save ();

Setresult (RESULT_OK);

Final String Path = new File (Environment.getexternalstoragedirectory ()),
"gestures"). GetAbsolutePath ();
Toast.maketext (This, getString (r.string.save_success, Path), Toast.length_long). Show ();
} else {
Setresult (result_canceled);
}

Finish ();

}

2. Gesture Recognition

Gesture recognition is also through the creation of the plot area, listening to draw, compared to the results of three processes, here directly to the code.

Copy Code code as follows:

public class Gestureperformedactivity extends activity {

Private final File mstorefile = new File (
Environment.getexternalstoragedirectory (), "gestures");

Gesture Library
Gesturelibrary Mgesturelib;

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);

Setcontentview (R.layout.gesture_perform);
Gesture Artboard
Gestureoverlayview gestures = (gestureoverlayview) Findviewbyid (R.id.gestures_overlay);

Gestures.setgesturestroketype (gestureoverlayview.gesture_stroke_type_multiple);

Gestures.setfadeoffset (2000); Multiple strokes every two times in a time interval
Gestures.setgesturecolor (Color.cyan);//Brush color
Gestures.setgesturestrokewidth (6);//Brush thickness value

A listener for gesture recognition
Gestures.addongestureperformedlistener (New Gestureoverlayview.ongestureperformedlistener () {
@Override
public void ongestureperformed (Gestureoverlayview overlay,
Gesture gesture) {
Query the matching content from the gesture library, the result of the match may include multiple similar results, the result of high match is put on the front
arraylist<prediction> predictions = Mgesturelib
. recognize (gesture);
if (predictions.size () > 0) {
Prediction prediction = (prediction) predictions.get (0);
Matching gestures.
if (Prediction.score > 1.0) {//Match score value larger, maximum is 10
Toast.maketext (Gestureperformedactivity.this,
Prediction.name, Toast.length_short). Show ();
}
}
}
});

if (mgesturelib = = null) {
Mgesturelib = Gesturelibraries.fromfile (mstorefile);
Mgesturelib.load ();
}
}

}



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.