S40 Touch API-Gesture API

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.