Using Touch Screen gestures can greatly improve the user experience of applications, such as using Drag for screen and Flick for scrolling.
Starting from S40_6th_Edition_FP1, the com. nokia. mid. ui. gestures package is added to the nokia S40 SDK to support touch screen gestures. Developers no longer need to implement their own gesture recognition engine. The Nokia Gesture API package consists of two interfaces and two classes.
Interface
GestureEvent
The GestureEvent interface class is used by an application to receive gesture recognition events from the platform.
GestureListener
This interface is used by applications which need to receive gesture events from the implementation.
Class
GestureInteractiveZone
The GestureInteractiveZone class is used by an application to define an area of the screen that reacts to a set of specified gestures.
GestureRegistrationManager
The GestureRegistrationManager class provides the ability to register a GestureListener to be notified when a gesture event occurs within a container.
Gesture API adopts the observer design mode.
Step 1: Create a GestureInteractiveZone instance
GestureInteractiveZone defines a region that can receive gesture events. The default GestureInteractiveZone includes the entire screen. GestureInteractiveZone also registers the gesture events that the MIDlet responds.
// Create a GestureInteractiveZone object GestureInteractiveZone giz = new GestureInteractiveZone (GestureInteractiveZone. GESTURE_ALL) for all gesture events; // set the corresponding region giz. setRectangle (x, y, width, height );
A recognizable gesture is defined in GestureInteractiveZone:
Static IntGESTURE_ALL ConstantForAll Gesture Events.
Static IntGESTURE_DRAG ConstantForThe Drag Gesture.
Static IntGESTURE_DROP ConstantForThe Drop Gesture.
Static IntGESTURE_FLICK ConstantForThe Flick Gesture.
Static IntGESTURE_LONG_PRESS ConstantForThe Long Press Gesture.
Static IntGESTURE_LONG_PRESS_REPEATED ConstantForThe Long Press Repeated Gesture.
Static IntGESTURE_TAP ConstantForThe Tap Gesture.
Step 2: implement the GestureListener Interface
The GestureListener interface defines only one method: gestureAction (). When the system's gesture recognition engine detects a gesture registered in the first step, this method is called. This method receives a GestureEvent instance each time it is called. GestureEvent contains the parameters of the last received gesture event.
public void gestureAction(Object container, GestureInteractiveZone gestureZone, GestureEvent gestureEvent)
{
switch( event.getType() ) {
case GestureInteractiveZone.GESTURE_TAP:
...;
break;
case GestureInteractiveZone.GESTURE_LONG_PRESS:
case GestureInteractiveZone.GESTURE_LONG_PRESS_REPEATED:
case GestureInteractiveZone.GESTURE_DRAG:
case GestureInteractiveZone.GESTURE_DROP:
case GestureInteractiveZone.GESTURE_FLICK:
}
}
The GestureEvent interface defines a large number of get methods. X and y coordinates can be obtained for all gesture events;
IntGetFlickSpeedX () QueryForThe Flick gesture events speed in horizontal ction.
IntGetFlickSpeedY () QueryForThe Flick gesture events speed in vertical direction.
For the DRAG and DROP events, you can also obtain the Change distance in the x and y directions;
IntGetDragDistanceX ()
QueryForThe Drag & Drop gesture events movement in horizontal direction since last drag gesture.
IntGetDragDistanceY ()
QueryForThe Drag & Drop gesture events movement in vertical direction since last drag gesture.
For FLICK events, you can get the moving speed and direction;
float getFlickDirection() Query for the Flick gesture events direction.
int getFlickSpeed() Query for the Flick gesture events speed in actual flick direction.
int getFlickSpeedX() Query for the Flick gesture events speed in horizontal direction.
int getFlickSpeedY() Query for the Flick gesture events speed in vertical direction.
Step 3: Register GestureInteractiveZone and Listener
GestureRegistrationManager class
static boolean register(java.lang.Object container, GestureInteractiveZone gestureInteractiveZone)
Register a gesture interactive zone to a container.
static void setListener(java.lang.Object container, GestureListener listener)
Add a listener to the a container.
The parameters of both methods include a container class Canvas or CustomItem ). The following code demonstrates how to register GestureInteractiveZone and Listener:
// Register GestureInteractiveZone
Canvas canvas =NewGestureCanvas ();
GestureRegistrationManager. register (canvas, giz );
// Register Listener
GestureRegistrationManager. setListener (canvas,This);
Note the following when using the Gesture API:
1. Do not use gestureAction (...) Method to block the UI thread.
2. Do not set gestureAction (...) Other variables point to the GestureEvent instance.
Sample Code Media: TouchSample.zip