The project describes how to implement gestures for your application.
Underlying Algorithm:
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project GestureExample
2.) Insert following code in your main.xml:
2.) Insert following code in your main.xml:
<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="Try to draw the gesture"
/>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0" />
</LinearLayout>
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="Try to draw the gesture"
/>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0" />
</LinearLayout>
3.) Copy the gesture library from the SD card – e.g. by opening the Windows-> Show View -> Android File Explorer in Eclipse and save it somewhere for now. Now Create directory res/raw and put the downloaded gesture library “gesture” in there
4.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. GestureExample.
2.) Enter following information:
Project name: GestureExample
Build Target: Android 2.3.3
Application name: GestureExample
Package name: org.example. GestureExample
Create Activity: GestureExample
Build Target: Android 2.3.3
Application name: GestureExample
Package name: org.example. GestureExample
Create Activity: GestureExample
On Clicking Finish GestureExample code structure is generated with the necessary Android Packages being imported along with GestureExample.java. GestureExample class will look like following:
package org.example.GestureExample;
import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.util.Log;
import android.widget.Toast;
/**
* A simple Activity listening for Gestures
*
**/
public class GestureExample extends Activity {
private GestureLibrary gLib;
private static final String TAG = "com.android.gesture";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gLib = GestureLibraries.fromRawResource(this, R.raw.gesture);
if (!gLib.load()) {
Log.w(TAG, "could not load gesture library");
finish();
}
GestureOverlayView gestures = (GestureOverlayView)findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(handleGestureListener);
}
/**
* our gesture listener
*/
private OnGesturePerformedListener handleGestureListener = newOnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView gestureView,
Gesture gesture) {
ArrayList<Prediction> predictions =gLib.recognize(gesture);
// one prediction needed
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);
// checking prediction
if (prediction.score > 1.0) {
// and action
Toast.makeText(GestureExample.this, prediction.name,
Toast.LENGTH_SHORT).show();
}
}
}
};
}
import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.util.Log;
import android.widget.Toast;
/**
* A simple Activity listening for Gestures
*
**/
public class GestureExample extends Activity {
private GestureLibrary gLib;
private static final String TAG = "com.android.gesture";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gLib = GestureLibraries.fromRawResource(this, R.raw.gesture);
if (!gLib.load()) {
Log.w(TAG, "could not load gesture library");
finish();
}
GestureOverlayView gestures = (GestureOverlayView)findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(handleGestureListener);
}
/**
* our gesture listener
*/
private OnGesturePerformedListener handleGestureListener = newOnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView gestureView,
Gesture gesture) {
ArrayList<Prediction> predictions =gLib.recognize(gesture);
// one prediction needed
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);
// checking prediction
if (prediction.score > 1.0) {
// and action
Toast.makeText(GestureExample.this, prediction.name,
Toast.LENGTH_SHORT).show();
}
}
}
};
}
Output – The final output
More Resources:
0 comments
Thanks for your comment