Creating Your Own Gesture Library

Gesture is a prerecorded touch screen motion sequence. If user performs gesture as the prerecorded gesture, your app can recognized as a command.

Gestures API (android.gesture) was introduced in Android 1.6, lets you store, load, draw and recognize gestures. In order to use Gestures API, you need a prerecorded gestures library. The Android 1.6 SDK (or later) comes with a new application pre-installed on the emulator, called Gestures Builder. You can use this application to create a set of pre-defined gestures for your own application.

You can also find the source code of Gestures Builder in SDK samples, such that you can create it on true device to create your own gestures library.

 

gsbuilder3

In Eclipse, click File -> New – Project… to create a New Android Project.
Select “Create project from existing sample”, select Build Target higher than 1.6, select GesturesBuilder in Samples, and click Finish.
Build and Run the App

gsbuilder2

After you Add gesture and DONE, a Toast will show you where is the gestures file saved, /mnt/sdcard/gestures (root of sd card) in my case. You can copy the gestures file to /res/raw folder of you gesture-enabled app as your gestures library.

Android provides special types of touch screen events such as pinch , double tap, scrolls , long presses and flinch. These are all known as gestures.

Android provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not. To use it , you need to create an object of GestureDetector and then extend another class with GestureDetector.SimpleOnGestureListener to act as a listener and override some methods. Its syntax is given below:

 

GestureDetector myG;
myG = new GestureDetector(this,new Gesture());

class Gesture extends GestureDetector.SimpleOnGestureListener{
public boolean onSingleTapUp(MotionEvent ev) {
}
public void onLongPress(MotionEvent ev) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
}
}
}

 

Android provides ScaleGestureDetector class to handle gestures like pinch e.t.c. In order to use it , you need to instantiate an object of this class. Its syntax is as follow:

 

ScaleGestureDetector SGD;
SGD = new ScaleGestureDetector(this,new ScaleListener());

 

 

The first parameter is the context and the second parameter is the event listener. We have to define the event listener and override a function OnTouchEvent to make it working. Its syntax is given below:

 

public boolean onTouchEvent(MotionEvent ev) {
SGD.onTouchEvent(ev);
return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale = detector.getScaleFactor();
return true;
}
}

 

 

Apart from the pinch gestures , there are other methods available that notify more about touch events.