Android realization gesture Gesture recognition usage Analysis _android

Source: Internet
Author: User
Tags sqlite database

This paper analyzes the application of gesture gesture recognition by Android. Share to everyone for your reference. Specifically as follows:

It's nice to see the gesture recognition feature in the Android1.6 SDK, and I've been wondering how to control the role of the game by using gestures (and, more accurately, gestures) in Android for an NDS game. It's a little bit of a light now, but there are a lot of details to be dealt with in gestures like gestures to control the characters as much as you want.

In the Android1.6 simulator pre-installed a program called Gestures Builder, this program is to let you create your own gestures (gestures builder source code in the SDK asked the samples, there are interested to see). The gesture will be saved to the/sdcard/gestures and copied to your project/res/raw, and you can use these gestures in your project. Copy to/res/raw under the gesture is read-only, that is, you can not modify or add gestures, if you want to achieve the change, you can directly load the SD card inside the gestures file.

In the example, I created this gesture:

Step Two: Create the Gestureoverlayview inside the layout, this transparent view is to let you draw gestures on the above, can be stacked on other view above:

<?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"
  >
<textview
  android:layout_width= "fill_parent"
  android:layout_ height= "Wrap_content"
  android:text= "@string/hello"
  />
<android.gesture.gestureoverlayview
  android:id= "@+id/gestures"
  android:layout_width= "fill_parent"
  android:layout_height= "0dip"
  android:layout_weight= "1.0"
  />
</LinearLayout>

Step three: Load into gesture:

Mlibrary = Gesturelibraries.fromrawresource (this, r.raw.gestures);
if (!mlibrary.load ()) {
  finish ();
}

Step Fourth: Increase the response function Ongestureperformedlistener:

Gestureoverlayview gestures = (gestureoverlayview) Findviewbyid (r.id.gestures);
Gestures.addongestureperformedlistener (this);

The above four steps can achieve simple gesture recognition prototype:

The result of the program running is as follows, writing a word, the program recognizes, and then toast a A:

The complete code is as follows:

Package com.ray.test;
Import java.util.ArrayList;
Import android.app.Activity;
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.os.Bundle;
Import Android.widget.Toast;
  public class Testgesture extends activity implements ongestureperformedlistener{Gesturelibrary mlibrary;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Gestureoverlayview gestures = (gestureoverlayview) Findviewbyid (r.id.gestures);
    Gestures.addongestureperformedlistener (this);
    Mlibrary = Gesturelibraries.fromrawresource (this, r.raw.gestures);
    if (!mlibrary.load ()) {finish (); @Override public void ongestureperformed (Gestureoverlayview overlay, gesture gesture) {ARRAylist predictions = mlibrary.recognize (gesture); We want at least one prediction if (predictions.size () > 0) {Prediction prediction = (prediction) predict
      Ions.get (0);
        We want at least some confidence in the result if (Prediction.score > 1.0) {//show the spell
      Toast.maketext (This, Prediction.name, Toast.length_short). Show ();

 }
    }
  }
}

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android View Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

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.