English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article analyzes the usage of Gesture gesture recognition in Android implementation. Shared with everyone for reference. Specifically as follows:
I'm glad to be able to in Android1.6I saw the gesture recognition feature in the sdk, and I have been thinking about how to implement the gesture control of nds games in android (more accurately, it should be pen gesture) to control the game character? Now I finally see a little light, but there are still many details to deal with to make the gestures as flexible as pen gestures.
In Android1.6The emulator pre-installs a program called Gestures Builder, which allows you to create your own gestures (the source code of Gestures Builder is in the sdk samples, you can take a look if you are interested). The gestures created will be saved to/sdcard/gestures, copy this file to your project/res/Copy these gestures to/res/The gestures in raw are read-only, which means you cannot modify or add gestures. If you want to implement modifications, you can directly load the gestures file from the sd card.
In this example, I created such a gesture:
Step 2: Create GestureOverlayView in layout, this transparent view is used for drawing gestures on it, and it can overlay other Views:
<?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 3: Load Gesture:
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); if (!mLibrary.load()) { finish(); }
Step 4: Add the response function OnGesturePerformedListener:
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); gestures.addOnGesturePerformedListener(this);
The above four steps can realize a simple Gesture recognition prototype:
The program's running result is as follows, write a letter 'a', and the program recognizes it and then shows a toast '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) predictions.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(); } } } }
Readers who are interested in more content related to Android can check the special topics on this site: 'Summary of Activity Operation Skills in Android Programming', 'Summary of View Operation Skills in Android', 'Summary of SQLite Database Operation Skills in Android', 'Summary of JSON Format Data Operation Skills in Android', 'Summary of Database Operation Skills in Android', 'Summary of File Operation Skills in Android', 'Summary of SD Card Operation Methods in Android Programming', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Resource Operation Skills in Android', and 'Summary of Android Control Usage'
I hope the content described in this article will be helpful to everyone in Android program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any infringement, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.