android 觸控 MotionEvent

(1)首先是MotionEvent 中getAction()與getActionMasked()的區別:

	public static final int ACTION_MASK             = 0xff;
	/**
	Return the kind of action being performed. Consider using getActionMasked() and getActionIndex() to retrieve the separate masked action and pointer index.
	Returns:
	The action, such as ACTION_DOWN or the combination of ACTION_POINTER_DOWN with a shifted pointer index.
	 */
	public final int getAction() {
		return nativeGetAction(mNativePtr);
	}

	/**  
	Return the masked action being performed, without pointer index information. Use getActionIndex() to return the index associated with pointer actions.
	Returns:
	The action, such as ACTION_DOWN or ACTION_POINTER_DOWN.
	 */
	public final int getActionMasked() {
		return nativeGetAction(mNativePtr) & ACTION_MASK;
	}

代碼摘自[android 4.3]  [設定nativeGetAction(mNativePtr)的返回值爲 mAction]

他們有什麼區別呢?如果nativeGetAction(mNativePtr)的值是在0x00到0xff之間的話。getAction()返回的值,和

getActionMasked()的返回的值是一樣的。

(Q1)那什麼時候返回的值是一樣的呢?即當nativeGetAction(mNativePtr)值大於0xff時,那什麼時候會大於0xff呢?

  這就是是當有多點觸控時。當有多點觸控時nativeGetAction(mNativePtr)返回值的低8位即0x00到0xff用來表示動作的類型信息。

 例如:MotionEvent#ACTION_DOWN的值是 0,即0x00。

         MotionEvent#ACTION_UP的值是 1,即0x01。

  等等。

 但是,我們知道Android是支持多點觸控的,那麼怎麼知道這個一個MotionEvent是哪一個觸控點觸發的呢?那麼就還需要MotionEvent帶有觸控點索引信息。

 Android的解決方案時在nativeGetAction(mNativePtr)返回值的後8位中存儲。

例如,如果mAction的值是0x0000,則表示是第一個觸控點的ACTION_DOWN操作。

       如果mAction的值是0x0100呢,則表示是第二個觸控點的ACTION_DOWN操作。

       第三個的ACTION_DOWN呢?相信你可以推出來是0x0200。

總而言之,mAction時的低8位(也就是0-7位)是動作類型信息。  mAction的8-15位呢,是觸控點的索引信息。(即表示是哪一個觸控點的事件)。

摘自:http://my.oschina.net/banxi/blog/56421
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章