自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android dispatchTouchEvent介紹

移動開發(fā) Android
Android中的事件類型分為按鍵事件和屏幕觸摸事件,Touch事件是屏幕觸摸事件的基礎(chǔ)事件,有必要對它進(jìn)行深入的了解。

一個最簡單的屏幕觸摸動作觸發(fā)了一系列Touch事件:ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_MOVE->ACTION_UP

當(dāng)屏幕中包含一個ViewGroup,而這個ViewGroup又包含一個子view,這個時候android系統(tǒng)如何處理Touch事件呢?到底是 ViewGroup來處理Touch事件,還是子view來處理Touch事件呢?這個并不一定。為什么呢?看看下面的調(diào)查結(jié)果就明白了。

android系統(tǒng)中的每個View的子類都具有下面三個和TouchEvent處理密切相關(guān)的方法:

1)public boolean dispatchTouchEvent(MotionEvent ev)  這個方法用來分發(fā)TouchEvent

2)public boolean onInterceptTouchEvent(MotionEvent ev) 這個方法用來攔截TouchEvent

3)public boolean onTouchEvent(MotionEvent ev) 這個方法用來處理TouchEvent

當(dāng)TouchEvent發(fā)生時,首先Activity將TouchEvent傳遞給最頂層的View, TouchEvent***到達(dá)最頂層 view 的 dispatchTouchEvent ,然后由  dispatchTouchEvent 方法進(jìn)行分發(fā),如果dispatchTouchEvent返回true ,則交給這個view的onTouchEvent處理,如果dispatchTouchEvent返回 false ,則交給這個 view 的 interceptTouchEvent 方法來決定是否要攔截這個事件,如果 interceptTouchEvent 返回 true ,也就是攔截掉了,則交給它的 onTouchEvent 來處理,如果 interceptTouchEvent 返回 false ,那么就傳遞給子 view ,由子 view 的 dispatchTouchEvent 再來開始這個事件的分發(fā)。如果事件傳遞到某一層的子 view 的 onTouchEvent 上了,這個方法返回了 false ,那么這個事件會從這個 view 往上傳遞,都是 onTouchEvent 來接收。而如果傳遞到最上面的 onTouchEvent 也返回 false 的話,這個事件就會“消失”,而且接收不到下一次事件。

通過語言描述這個處理邏輯很抽象,下面就用代碼來具體說明一下。

layout配置文件 main.xml:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <test.lzqdiy.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     android:gravity="center" > 
  7.        <test.lzqdiy.MyTextView 
  8.             android:layout_width="200px" 
  9.             android:layout_height="200px" 
  10.             android:id="@+id/tv" 
  11.             android:text="lzqdiy" 
  12.             android:textSize="40sp" 
  13.             android:textStyle="bold" 
  14.             android:background="#FFFFFF" 
  15.             android:textColor="#0000FF"/> 
  16. </test.lzqdiy.MyLinearLayout>  

節(jié)點層次很簡單,一個LinearLayout中添加了一個TextView。

下面是java代碼:

  1. package test.lzqdiy; 
  2.  
  3. import android.app.Activity; 
  4. import android.os.Bundle; 
  5.  
  6. public class TestTouchEventApp extends Activity { 
  7.     /** Called when the activity is first created. */ 
  8.     @Override 
  9.     public void onCreate(Bundle savedInstanceState) { 
  10.         super.onCreate(savedInstanceState); 
  11.         setContentView(R.layout.main); 
  12.     } 
  13. package test.lzqdiy; 
  14.  
  15. import android.content.Context; 
  16. import android.util.AttributeSet; 
  17. import android.util.Log; 
  18. import android.view.MotionEvent; 
  19. import android.widget.LinearLayout; 
  20.  
  21. public class MyLinearLayout extends LinearLayout { 
  22.     private final String TAG = "MyLinearLayout"
  23.  
  24.     public MyLinearLayout(Context context, AttributeSet attrs) { 
  25.  
  26.         super(context, attrs); 
  27.  
  28.         Log.d(TAG, TAG); 
  29.  
  30.     } 
  31.  
  32.     @Override 
  33.     public boolean dispatchTouchEvent(MotionEvent ev) { 
  34.         int action = ev.getAction(); 
  35.  
  36.         switch (action) { 
  37.  
  38.         case MotionEvent.ACTION_DOWN: 
  39.  
  40.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN"); 
  41.  
  42.             break
  43.  
  44.         case MotionEvent.ACTION_MOVE: 
  45.  
  46.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE"); 
  47.  
  48.             break
  49.  
  50.         case MotionEvent.ACTION_UP: 
  51.  
  52.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP"); 
  53.  
  54.             break
  55.  
  56.         case MotionEvent.ACTION_CANCEL: 
  57.  
  58.             Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL"); 
  59.  
  60.             break
  61.  
  62.         } 
  63.         return super.dispatchTouchEvent(ev); 
  64.     } 
  65.  
  66.     @Override 
  67.     public boolean onInterceptTouchEvent(MotionEvent ev) { 
  68.  
  69.         int action = ev.getAction(); 
  70.  
  71.         switch (action) { 
  72.  
  73.         case MotionEvent.ACTION_DOWN: 
  74.  
  75.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN"); 
  76.  
  77.             break
  78.  
  79.         case MotionEvent.ACTION_MOVE: 
  80.  
  81.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE"); 
  82.  
  83.             break
  84.  
  85.         case MotionEvent.ACTION_UP: 
  86.  
  87.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP"); 
  88.  
  89.             break
  90.  
  91.         case MotionEvent.ACTION_CANCEL: 
  92.  
  93.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL"); 
  94.  
  95.             break
  96.  
  97.         } 
  98.  
  99.         return false
  100.  
  101.     } 
  102.  
  103.     @Override 
  104.     public boolean onTouchEvent(MotionEvent ev) { 
  105.  
  106.         int action = ev.getAction(); 
  107.  
  108.         switch (action) { 
  109.  
  110.         case MotionEvent.ACTION_DOWN: 
  111.  
  112.             Log.d(TAG, "---onTouchEvent action:ACTION_DOWN"); 
  113.  
  114.             break
  115.  
  116.         case MotionEvent.ACTION_MOVE: 
  117.  
  118.             Log.d(TAG, "---onTouchEvent action:ACTION_MOVE"); 
  119.  
  120.             break
  121.  
  122.         case MotionEvent.ACTION_UP: 
  123.  
  124.             Log.d(TAG, "---onTouchEvent action:ACTION_UP"); 
  125.  
  126.             break
  127.  
  128.         case MotionEvent.ACTION_CANCEL: 
  129.  
  130.             Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL"); 
  131.  
  132.             break
  133.  
  134.         } 
  135.  
  136.         return true
  137.     } 
  138.  
  139.  
  140. package test.lzqdiy; 
  141.  
  142. import android.content.Context; 
  143. import android.util.AttributeSet; 
  144. import android.util.Log; 
  145. import android.view.MotionEvent; 
  146. import android.widget.TextView; 
  147.  
  148. public class MyTextView extends TextView { 
  149.  
  150.     private final String TAG = "MyTextView"
  151.  
  152.     public MyTextView(Context context, AttributeSet attrs) { 
  153.  
  154.         super(context, attrs); 
  155.  
  156.     } 
  157.  
  158.     @Override 
  159.     public boolean dispatchTouchEvent(MotionEvent ev) { 
  160.         int action = ev.getAction(); 
  161.  
  162.         switch (action) { 
  163.  
  164.         case MotionEvent.ACTION_DOWN: 
  165.  
  166.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN"); 
  167.  
  168.             break
  169.  
  170.         case MotionEvent.ACTION_MOVE: 
  171.  
  172.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE"); 
  173.  
  174.             break
  175.  
  176.         case MotionEvent.ACTION_UP: 
  177.  
  178.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP"); 
  179.  
  180.             break
  181.  
  182.         case MotionEvent.ACTION_CANCEL: 
  183.  
  184.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL"); 
  185.  
  186.             break
  187.  
  188.         } 
  189.         return super.dispatchTouchEvent(ev); 
  190.     } 
  191.  
  192.     @Override 
  193.     public boolean onTouchEvent(MotionEvent ev) { 
  194.  
  195.         int action = ev.getAction(); 
  196.  
  197.         switch (action) { 
  198.  
  199.         case MotionEvent.ACTION_DOWN: 
  200.  
  201.             Log.d(TAG, "---onTouchEvent action:ACTION_DOWN"); 
  202.  
  203.             break
  204.  
  205.         case MotionEvent.ACTION_MOVE: 
  206.  
  207.             Log.d(TAG, "---onTouchEvent action:ACTION_MOVE"); 
  208.  
  209.             break
  210.  
  211.         case MotionEvent.ACTION_UP: 
  212.  
  213.             Log.d(TAG, "---onTouchEvent action:ACTION_UP"); 
  214.  
  215.             break
  216.  
  217.         case MotionEvent.ACTION_CANCEL: 
  218.  
  219.             Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL"); 
  220.  
  221.             break
  222.  
  223.         } 
  224.  
  225.         return true
  226.  
  227.     } 
  228.  
  229. }  

為了指代方便,下面將MyLinearLayout簡稱為L,將MyTextView簡稱為 T,L.onInterceptTouchEvent=true 表示的含義為MyLinearLayout中的onInterceptTouchEvent方法返回值為true,通過程序運行時輸出的Log來說明調(diào)用 時序。

第1種情況 L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=true 輸出下面的Log:

D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_DOWN
D/MyTextView(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(11865): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_MOVE
D/MyTextView(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyTextView(11865): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_UP
D/MyTextView(11865): dispatchTouchEvent action:ACTION_UP
D/MyTextView(11865): ---onTouchEvent action:ACTION_UP

結(jié)論:TouchEvent完全由TextView處理。

第2種情況  L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=false 輸出下面的Log:

D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): onInterceptTouchEvent action:ACTION_DOWN
D/MyTextView(13101): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_UP

結(jié)論:TextView只處理了ACTION_DOWN事件,LinearLayout處理了所有的TouchEvent。

第3種情況  L.onInterceptTouchEvent=true&& L.onTouchEvent=true 輸出下面的Log:

D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): onInterceptTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_UP

結(jié)論:LinearLayout處理了所有的TouchEvent。

第4種情況  L.onInterceptTouchEvent=true&& L.onTouchEvent=false 輸出下面的Log:

D/MyLinearLayout(13452): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13452): onInterceptTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13452): ---onTouchEvent action:ACTION_DOWN 

結(jié)論:LinearLayout只處理了ACTION_DOWN事件,那么其他的TouchEvent被誰處理了呢?答案是LinearLayout最外層的Activity處理了TouchEvent。

責(zé)任編輯:徐川 來源: OSChina
相關(guān)推薦

2013-01-16 14:41:14

Android開發(fā)Android SDK

2017-01-23 21:35:58

Android人臉檢測介紹

2010-01-27 16:41:48

Android特點

2013-12-27 09:54:58

Android開發(fā)NDK

2010-01-27 17:08:01

Android Hel

2010-01-26 10:31:32

Android onK

2010-02-07 13:55:39

Android圖形

2009-09-17 17:20:21

Android Deb

2009-03-24 08:33:14

AndroidGoogle移動os

2010-02-05 16:21:02

Android導(dǎo)航

2011-05-26 17:15:26

Android Droid-Fu

2013-01-16 14:19:03

Android工程目錄結(jié)構(gòu)Android開發(fā)

2010-02-06 18:04:21

Android 接口

2013-01-10 13:50:25

Android開發(fā)組件

2010-03-02 09:13:55

Android手機系統(tǒng)

2010-03-03 13:56:24

2010-03-05 13:28:14

Android手機系統(tǒng)

2010-02-07 14:29:10

Android SDK

2010-03-03 17:29:30

Android程序

2010-03-05 16:58:02

Android Mar
點贊
收藏

51CTO技術(shù)棧公眾號