Recipe Using Gestures

A gesture is a hand-drawn shape on a touch screen.The android.gesture package provides libraries to recognize and handle these in a simple way. First, every SDK has a sample program that can be used to build a collection of gestures in platforms/android-2.0/samples/GestureBuilder/. The Gesture Builder project can be imported and run on an Android device. It produces a file called /sdcard/gestures, which can be copied off of the device and used as a raw resource for this recipe.

As an example, a file of handwritten numbers can be generated as shown in Figure 5.3. Multiple gestures can have the same name, so providing different examples of the same gesture is useful to improve pattern recognition.

After this file is created for all numbers from 0 to 9 in all variants of interest, it can be copied to res/raw/numbers, for example.The layout is shown in Listing 5.15, and the main activity is shown in Listing 5.16. In the activity, the GestureLibrary is initialized with this raw resource.

This recipe adds a GestureOverlayView on top of the screen and implements an OnGesturePerformedListener.When a gesture is drawn, the gesture is passed to the onGesturePerformed() method, which compares it with all the gestures in the library and returns an ordered list of predictions starting with the most likely. Each prediction has the name as defined in the library and the score for how correlated the gesture is to the input gesture. As long as the first entry has a score greater than one, it is generally a match.

Gestures Builder

Figure 5.3 The Gesture Builder application, which comes with the Android SDK, can be used to create a gesture library.

Listing 5.15 res/layout/main.xml

<?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:gravity="center_horizontal" android:textSize="20sp" android:text="Draw a number" android:layout_margin="10dip"/>

<android.gesture.GestureOverlayView android:id="@+id/gestures" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" />

<TextView android:id="@+id/prediction" android:layout_width="fill_parent" android:layout_height="wrap_content"

android:gravity="center_horizontal" android:textSize="20sp" android:text=""

android:layout_margin="10dip"/> </LinearLayout>

For illustration, this recipe compiles all the predictions in a String and displays them on the screen.An example output is shown in Figure 5.4.This shows that even though a visual match is not complete, the partial number can match a library number well.

Gestures

Draw a number five 12.60 seven 1.70 three 1.46 one 1.28 nine 1.25 nine 1.14 six 0.96 two 0.80 eight 0.77 four 0.60

Figure 5.4 The gesture recognition example that shows prediction scores.

Listing 5.16 src/com/cookbook/gestures/Gestures.java package com.cookbook.gestures;

import java.text.DecimalFormat; import java.text.NumberFormat; 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.TextView;

public class Gestures extends Activity implements OnGesturePerformedListener { private GestureLibrary mLibrary;

private TextView tv;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.prediction);

mLibrary = GestureLibraries.fromRawResource(this, R.raw.numbers); if (!mLibrary.load()) finish();

GestureOverlayView gestures =

(GestureOverlayView) findViewByld(R.id.gestures); gestures.addOnGesturePerformedListener(this);

public void onGesturePerformed(GestureOverlayView overlay,

Gesture gesture) { ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

String predList = "";

NumberFormat formatter = new DecimalFormat("#0.00"); for(int i=0; i<predictions.size(); i++) {

Prediction prediction = predictions.get(i);

predList = predList + prediction.name + " "

+ formatter.format(prediction.score) + "\n";

tv.setText(predList);

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Responses

  • sabina
    How to compare gesture with gesturelibrary in android example?
    7 years ago

Post a comment