Sep 19, 2011

Detection of Gesture

Detection of Gesture


To detect Gesture, you have to prepare your own gestures library. Refer to last post to create your gestures library using Gestures Builder, and copy to /res/raw.

package com.GestureMonitor;

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.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.TextView;

public class GestureMonitorActivity extends Activity {

GestureLibrary gestureLibrary = null;
GestureOverlayView gestureOverlayView;
TextView gestureResult;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureResult = (TextView)findViewById(R.id.gestureresult);
gestureOverlayView = (GestureOverlayView)findViewById(R.id.gestures);

gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
gestureLibrary.load();

gestureOverlayView.addOnGesturePerformedListener(gesturePerformedListener);
}

OnGesturePerformedListener gesturePerformedListener
= new OnGesturePerformedListener(){

@Override
public void onGesturePerformed(GestureOverlayView view, Gesture gesture) {
// TODO Auto-generated method stub
ArrayList<Prediction> prediction = gestureLibrary.recognize(gesture);
if(prediction.size() > 0){
gestureResult.setText(prediction.get(0).name);
}

}};
}


<?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"
/>
<TextView
android:id="@+id/gestureresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"/>
</LinearLayout>

2 comments:

  1. I need a help ,how i can make gesture library file in the file explore plz help by tutorial

    ReplyDelete

Infolinks In Text Ads