Android Gestures

Gesture is an user interface in Android. Gesture is a shape,which is drawn on free hand in touch screen. GestureLibrary is used to recognize user-defined gesture.

You can download source code here.

create a user-defined gesture using Gesture builder.Refer to the below screenshot for creating Gusture.



By default gusture will save on "storage/sdcard/gesture" folder. Create a new folder named "raw" in "res" folder. Copy that gesture file and paste it in raw folder. Refer to the below screenshot for adding gusture in our application.



In GestureOverlayView draw the pattern similar to already added gesture in application.

Create Android Project with name as GestureDemo with package name "com.etr.gesturedemo". Create Activity with name as GestureActivity. This is the code for GestureActivity.

package com.etr.gesturedemo;

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.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class GestureActivity extends Activity {
GestureLibrary gLib;

@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture);

gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gLib.load()) {
finish();
}

GestureOverlayView gestureView = (GestureOverlayView) findViewById(R.id.gView);
gestureView
.addOnGesturePerformedListener(new OnGesturePerformedListener() {

@Override
public void onGesturePerformed(
GestureOverlayView overlay, Gesture gesture) {
try {
ArrayList<Prediction> prediction = gLib
.recognize(gesture);

if (prediction.size() > 0
&& prediction.get(0).score > 1.0) {
String result = prediction.get(0).name;
if ("CreateFile".equalsIgnoreCase(result)) {
Toast.makeText(GestureActivity.this,
"Create File",
Toast.LENGTH_LONG).show();
}
}

} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
});

} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gesture, menu);
return true;
}

}

This is code for activity_gesture.xml. It's contain android.gesture.GestureOverlayView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".GestureActivity" >

    <android.gesture.GestureOverlayView
        android:id="@+id/gView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Refer to the below screenshot for output.

Comments

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device