Series40 設(shè)備上多點觸摸交互的處理
在 Java Runtime 2.0.0 for Series 40 版本中添加了對多點觸摸API的支持,那么本文的目的就是介紹如何使用多點觸摸API,處理交互的事件。
多點觸摸API允許在基于Canvas的MIDlet中處理多點觸摸事件,比如說GameCanvas, FullCanvas等,通過API我們可以注冊以便能夠接收到多點觸摸事件。
API介紹
每一個觸摸點是包括3個重要的數(shù)值:
一個唯一的ID
當(dāng)前的坐標(biāo)值,包括X, Y
當(dāng)前觸點的狀態(tài)
其中觸摸的狀態(tài)有3種:
POINTER_PRESSED
POINTER_DRAGGED
POINTER_RELEASED
多點觸摸的API很簡單,主要包括了下面的:
-MultipointTouch 該類主要用來訪問多個觸電的相關(guān)坐標(biāo),狀態(tài)等信息,設(shè)備所支持的觸點數(shù)等信息,綁定或移除多點觸摸監(jiān)聽者等。
-MultipointTouchListener 接收多點觸摸事件,并做出相應(yīng)處理。
API的使用
判斷當(dāng)前設(shè)備是否支持 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
}
如何獲取特定設(shè)備所支持的最大觸點數(shù)呢: 可以使用- MultipointTouch.getMaxPointers
獲取MultipointTouch實例
MultipointTouch mpt = MultipointTouch.getInstance();
為MIDlet注冊MultipointTouchListener
public class MainCanvas extends Canvas implements MultipointTouchListener
{
public MainCanvas() {
// ...
mpt.addMultipointTouchListener(this);
}
......
}
處理多點觸摸事件
從函數(shù)pointersChanged(int[] pointerIds)可以看出參數(shù)僅僅是一個觸摸點ID的數(shù)組,然后我們是通過ID值使用MultipointTouch來獲取觸點的相關(guān)信息。
這里需要注意的,參數(shù)pointerIds 這個數(shù)組僅僅是包含了狀態(tài),位置有變化的觸摸點的ID,沒有變化的并不會被包含在該數(shù)組中。
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;
}
}
}
實例演示




















