android多点触摸手势,安卓手势学习笔记(三) 多点触控
8種機械鍵盤軸體對比
本人程序員,要買一個寫代碼的鍵盤,請問紅軸和茶軸怎么選?
跟蹤多個觸點
當多個手指同時觸碰屏幕時,系統產生如下的事件:ACTION_DOWN –第一個觸點。它啟動了手勢,在MotionEvent中該觸點的數據索引是0.
ACTION_POINTER_DOWN –第一個觸點之后觸碰屏幕的其他觸點。其索引通過getActionIndex()獲取。
ACTION_MOVE –已經按下的手勢發生變化。
ACTION_POINTER_UP –非最初手指抬起時發送。
ACTION_UP –最后一個手指離開屏幕時發送。
在MotionEvent中通過索引和ID可以跟蹤各個觸點。
索引:MotionEvent在一個數組中存儲各個觸點的信息。觸點的索引就是觸點在數組中的位置。大多數的MotionEvent方法使用索引來與觸點交互。
ID:每個觸點在觸摸事件中維持一個ID映射,可以在整個手勢過程中跟蹤單獨的觸點。
各個觸點在觸摸事件中出現的順序是不確定的。所以觸點的索引可能在各個事件中不同,但其ID則會保持為一個常數。使用getPointerID()方法可以獲取其ID。接下來的事件中,使用findPointerIndex()方法可以獲取指定ID的觸點的索引。例如:private int mActivePointerId;
public boolean onTouchEvent(MotionEvent event) {
....
// Get the pointer ID
mActivePointerId = event.getPointerId(0);
// ... Many touch events later...
// Use the pointer ID to find the index of the active pointer
// and fetch its position
int pointerIndex = event.findPointerIndex(mActivePointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
}
獲取MotionEvent的動作
使用getActionMasked()(或者用兼容版本MotionEventCompat.getActionMasked()更好)獲取MotionEvent的動作。getActionMasked()用來處理多觸點,而原來的getAction()方法不行。它返回正在執行的修飾過的動作,不包括觸點的索引。可以用getActionIndex()來返回觸點的索引。
注意:下面的例子使用MotionEventCompat類,可以為更多平臺提供兼容性支持。但MotionEventCompat并非MotionEvent類的替代。它提供了一些靜態方法,把MotionEvent作為參數傳入,可以獲取其相關的事件。int action = MotionEventCompat.getActionMasked(event);
// Get the index of the pointer associated with the action.
int index = MotionEventCompat.getActionIndex(event);
int xPos = -1;
int yPos = -1;
Log.d(DEBUG_TAG,"The action is " + actionToString(action));
if (event.getPointerCount() > 1) {
Log.d(DEBUG_TAG,"Multitouch event");
// The coordinates of the current screen contact, relative to
// the responding View or Activity.
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
} else {
// Single touch event
Log.d(DEBUG_TAG,"Single touch event");
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
}
...
// Given an action int, returns a string description
public static String actionToString(int action) {
switch (action) {
case MotionEvent.ACTION_DOWN: return "Down";
case MotionEvent.ACTION_MOVE: return "Move";
case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down";
case MotionEvent.ACTION_UP: return "Up";
case MotionEvent.ACTION_POINTER_UP: return "Pointer Up";
case MotionEvent.ACTION_OUTSIDE: return "Outside";
case MotionEvent.ACTION_CANCEL: return "Cancel";
}
return "";
}
總結
看到這里發現有點混亂,所以和前面單觸點的小節整合到一起看一看。首先,每一次觸屏事件導致onTouchEvent()被調用,帶來一個MotionEvent;
調用MotionEventCompat.getActionMasked(event)方法可以獲取這個event的動作;
MotionEvent在一個數組從存儲各個觸點的數據,他們的索引并不固定;
每個觸點同時有一個不變的ID;
調用event.getPointerID(index)可以獲取指定索引的ID;
調用event.findPointerIndex(id)可以獲取指定ID的索引;
調用MotionEventCompat.getActionIndex()可以獲取動作觸點的index; // 這里看上去很奇怪,一個event中存在多個觸點,為什么要獲取index?答案應該是,每個action只針對其中一個觸點,所以這樣就可以獲取當前事件的動作的唯一觸點,而其他觸點的數據也存在這個event之中;
調用event.getPointerCount()可以區分當前是單觸點操作還是多觸點操作。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的android多点触摸手势,安卓手势学习笔记(三) 多点触控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日志库EasyLogging++学习系列
- 下一篇: 日志库EasyLogging++学习系列