Series40 設備上多點觸摸交互的處理
在 Java Runtime 2.0.0 for Series 40 版本中添加了對多點觸摸API的支持,那么本文的目的就是介紹如何使用多點觸摸API,處理交互的事件。
多點觸摸API允許在基于Canvas的MIDlet中處理多點觸摸事件,比如說GameCanvas, FullCanvas等,通過API我們可以注冊以便能夠接收到多點觸摸事件。
API介紹
每一個觸摸點是包括3個重要的數值:
一個唯一的ID
當前的坐標值,包括X, Y
當前觸點的狀態
其中觸摸的狀態有3種:
POINTER_PRESSED
POINTER_DRAGGED
POINTER_RELEASED
多點觸摸的API很簡單,主要包括了下面的:
-MultipointTouch 該類主要用來訪問多個觸電的相關坐標,狀態等信息,設備所支持的觸點數等信息,綁定或移除多點觸摸監聽者等。
-MultipointTouchListener 接收多點觸摸事件,并做出相應處理。
API的使用
判斷當前設備是否支持 MultiTouch API
Nokia SDK中提供了屬性com.nokia.mid.ui.multipointtouch.version 來獲取API版本信息。
if (System.getProperty("com.nokia.mid.ui.multipointtouch.version") != null) {
// API is supported: Can implement multipoint touch functionality
} else {
// API is not supported: Cannot implement multipoint touch functionality
}
如何獲取特定設備所支持的最大觸點數呢: 可以使用- MultipointTouch.getMaxPointers
獲取MultipointTouch實例
MultipointTouch mpt = MultipointTouch.getInstance();
為MIDlet注冊MultipointTouchListener
public class MainCanvas extends Canvas implements MultipointTouchListener
{
public MainCanvas() {
// ...
mpt.addMultipointTouchListener(this);
}
......
}
處理多點觸摸事件
從函數pointersChanged(int[] pointerIds)可以看出參數僅僅是一個觸摸點ID的數組,然后我們是通過ID值使用MultipointTouch來獲取觸點的相關信息。
這里需要注意的,參數pointerIds 這個數組僅僅是包含了狀態,位置有變化的觸摸點的ID,沒有變化的并不會被包含在該數組中。
public void pointersChanged(int[] pointerIds) {
for(int i=0; i<pointerIds.length; i++) { // Loop through the changed touch points
{
int pointerId = pointerIds[i]; // Get the touch point ID
int state = MultipointTouch.getState(pointerId); // Get the touch point state
// Get the touch point x and y coordinates
int x = MultipointTouch.getX(pointerId);
int y = MultipointTouch.getY(pointerId);
// Handle the UI update based on the touch point state, ID and coordinates
switch(state) {
case MultipointTouch.POINTER_PRESSED: // A new finger was pressed against the screen
drawTouch(pointerId, x, y); break;
case MultipointTouch.POINTER_DRAGGED: // A pressed finger was dragged over the screen
drawTouch(pointerId, x, y); break;
case MultipointTouch.POINTER_RELEASED: // A pressed finger was lifted from the screen
break;
}
}
}
實例演示