日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 处理多点触控手势

發布時間:2023/12/29 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 处理多点触控手势 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

編寫:Andrwyw?- 原文:http://developer.android.com/training/gestures/multi.html

多點觸控手勢是指在同一時間有多點(手指)觸碰屏幕。本節課程講述,如何檢測涉及多點的觸摸手勢。

追蹤多點

當多個手指同時觸摸屏幕時,系統會產生如下的觸摸事件:

  • ACTION_DOWN?- 針對觸摸屏幕的第一個點。此事件是手勢的開端。第一觸摸點的數據在MotionEvent中的索引總是0。
  • ACTION_POINTER_DOWN?- 針對第一點后,出現在屏幕上額外的點。這個點的數據在MotionEvent中的索引,可以通過getActionIndex()獲得。
  • ACTION_MOVE?- 在按下手勢期間發生變化。
  • ACTION_POINTER_UP?- 當非主要點(non-primary pointer)離開屏幕時,發送此事件。
  • ACTION_UP?- 當最后一點離開屏幕時發送此事件。

我們可以通過各個點的索引以及id,單獨地追蹤MotionEvent中的每個點。

  • Index:MotionEvent把各個點的信息都存儲在一個數組中。點的索引值就是它在數組中的位置。大多數用來與點交互的MotionEvent函數都是以索引值而不是點的ID作為參數的。
  • ID:每個點也都有一個ID映射,該ID映射在整個手勢期間一直存在,以便我們單獨地追蹤每個點。

每個獨立的點在移動事件中出現的次序是不固定的。因此,從一個事件到另一個事件,點的索引值是可以改變的,但點的ID在它的生命周期內是保證不會改變的。使用getPointerId()可以獲得一個點的ID,在手勢隨后的移動事件中,就可以用該ID來追蹤這個點。對于隨后一系列的事件,可以使用findPointerIndex()函數,來獲得對應給定ID的點在移動事件中的索引值。如下:

private int mActivePointerId;public boolean onTouchEvent(MotionEvent event) {....// Get the pointer IDmActivePointerId = event.getPointerId(0);// ... Many touch events later...// Use the pointer ID to find the index of the active pointer// and fetch its positionint pointerIndex = event.findPointerIndex(mActivePointerId);// Get the pointer's current positionfloat x = event.getX(pointerIndex);float y = event.getY(pointerIndex); }

獲取MotionEvent的動作

我們應該總是使用getActionMasked()函數(或者用MotionEventCompat.getActionMasked()這個兼容版本更好)來獲取MotionEvent的動作(action)。與舊的getAction()函數不同的是,getActionMasked()是設計用來處理多點觸摸的。它會返回執行過的動作的掩碼值,不包括點的索引位。然后,我們可以使用getActionIndex()來獲得與該動作關聯的點的索引值。這在接下來的代碼段中可以看到。

Note:?這個樣例使用的是MotionEventCompat類。該類在Support Library中。我們應該使用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 eventLog.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 "";

}

轉自:http://hukai.me/android-training-course-in-chinese/input/gestures/multi.html

總結

以上是生活随笔為你收集整理的android 处理多点触控手势的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。