S40 Touch API - Gesture API
利用觸摸屏的手勢操會使得應用程序的用戶體驗大大提升,比如用Drag進行屏,用Flick進行滾動等。
從S40_6th_Edition_FP1開始,諾基亞S40 SDK中加入了com.nokia.mid.ui.gestures 包,對觸摸屏手勢進行支持。開發者無需再自己實現手勢識別引擎。Nokia Gesture API包由兩個接口和兩個類組成。
接口
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.
類
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采用了觀察者設計模式。
第一步:創建GestureInteractiveZone實例
GestureInteractiveZone定義了一個可以接收手勢事件的區域。缺省的GestureInteractiveZone包括整個屏幕。GestureInteractiveZone同時注冊了MIDlet響應哪些手勢事件。
// 創建一個相應所有手勢事件的GestureInteractiveZone 對象 GestureInteractiveZone giz = new GestureInteractiveZone( GestureInteractiveZone.GESTURE_ALL ); // 設置相應區域 giz.setRectangle( x, y, width, height);
在GestureInteractiveZone中定義了可被識別的手勢:
static int GESTURE_ALL Constant for All Gesture Events.
static int GESTURE_DRAG Constant for the Drag Gesture.
static int GESTURE_DROP Constant for the Drop Gesture.
static int GESTURE_FLICK Constant for the Flick Gesture.
static int GESTURE_LONG_PRESS Constant for the Long Press Gesture.
static int GESTURE_LONG_PRESS_REPEATED Constant for the Long Press Repeated Gesture.
static int GESTURE_TAP Constant for the Tap Gesture.
第二步,實現GestureListener接口
GestureListener接口只定義了一個方法:gestureAction(), 當系統的手勢識別引擎探測到在第一步中注冊了的手勢后,這個方法會被調用。該方法每次被調用時會接收到一個GestureEvent實例。GestureEvent中保存有最后接收到的手勢事件的參數。
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:
}
}
GestureEvent接口中定義了大量的get方法。 對于所有的手勢事件都可以得到x,y坐標;
int getFlickSpeedX() Query for the Flick gesture events speed in horizontal direction.
int getFlickSpeedY() Query for the Flick gesture events speed in vertical direction.對于DRAG 和 DROP事件還可以分別得到x和y方向上變化的距離;
int getDragDistanceX()
Query for the Drag & Drop gesture events movement in horizontal direction since last drag gesture.
int getDragDistanceY()
Query for the Drag & Drop gesture events movement in vertical direction since last drag gesture.
對于FLICK事件,可以得到移動的速度和方向;
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.
第三步,注冊GestureInteractiveZone和Listener
GestureRegistrationManager類
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.
這兩個方法的參數中都包括了一個容器類( Canvas 或者 CustomItem)。下面的代碼演示了如何注冊GestureInteractiveZone和Listener:
// 注冊GestureInteractiveZone
Canvas canvas = new GestureCanvas();
GestureRegistrationManager.register( canvas, giz );
//注冊 Listener
GestureRegistrationManager.setListener(canvas, this);
使用Gesture API時應注意:
1. 不要在gestureAction(…)方法中阻塞UI線程。
2. 不要將gestureAction(…)以外的變量指向GestureEvent實例。