android中Touch事件的處理邏輯

          android中的事件類型分為按鍵事件和屏幕觸摸事件,Touch事件是屏幕觸摸事件的基礎(chǔ)事件,有必要對(duì)它進(jìn)行深入的了解。
          一個(gè)最簡(jiǎn)單的屏幕觸摸動(dòng)作觸發(fā)了一系列Touch事件:ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_MOVE->ACTION_UP
          當(dāng)屏幕中包含一個(gè)ViewGroup,而這個(gè)ViewGroup又包含一個(gè)子view,這個(gè)時(shí)候android系統(tǒng)如何處理Touch事件呢?到底是ViewGroup來處理Touch事件,還是子view來處理Touch事件呢?我只能很肯定的對(duì)你說不一定。呵呵,為什么呢?看看下面我的調(diào)查結(jié)果你就明白了。
          android系統(tǒng)中的每個(gè)View的子類都具有下面三個(gè)和TouchEvent處理密切相關(guān)的方法:
          1)public boolean dispatchTouchEvent(MotionEvent ev)  這個(gè)方法用來分發(fā)TouchEvent
          2)public boolean onInterceptTouchEvent(MotionEvent ev) 這個(gè)方法用來攔截TouchEvent
          3)public boolean onTouchEvent(MotionEvent ev) 這個(gè)方法用來處理TouchEvent
          當(dāng)TouchEvent發(fā)生時(shí),首先Activity將TouchEvent傳遞給最頂層的View, TouchEvent最先到達(dá)最頂層 view 的 dispatchTouchEvent ,然后由  dispatchTouchEvent 方法進(jìn)行分發(fā),如果dispatchTouchEvent返回true ,則交給這個(gè)view的onTouchEvent處理,如果dispatchTouchEvent返回 false ,則交給這個(gè) view 的 interceptTouchEvent 方法來決定是否要攔截這個(gè)事件,如果 interceptTouchEvent 返回 true ,也就是攔截掉了,則交給它的 onTouchEvent 來處理,如果 interceptTouchEvent 返回 false ,那么就傳遞給子 view ,由子 view 的 dispatchTouchEvent 再來開始這個(gè)事件的分發(fā)。如果事件傳遞到某一層的子 view 的 onTouchEvent 上了,這個(gè)方法返回了 false ,那么這個(gè)事件會(huì)從這個(gè) view 往上傳遞,都是 onTouchEvent 來接收。而如果傳遞到最上面的 onTouchEvent 也返回 false 的話,這個(gè)事件就會(huì)“消失”,而且接收不到下一次事件。
          通過語(yǔ)言描述這個(gè)處理邏輯很抽象,下面我就用代碼來具體說明一下。
          layout配置文件 main.xml
          <?xml version="1.0" encoding="utf-8"?>
          <test.lzqdiy.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation
          ="vertical"
              android:layout_width
          ="fill_parent"
              android:layout_height
          ="fill_parent"
              android:gravity
          ="center" >
                 
          <test.lzqdiy.MyTextView 
                      android:layout_width
          ="200px"
                      android:layout_height
          ="200px"
                      android:id
          ="@+id/tv"
                      android:text
          ="lzqdiy"
                      android:textSize
          ="40sp"
                      android:textStyle
          ="bold"
                      android:background
          ="#FFFFFF"
                      android:textColor
          ="#0000FF"/>
          </test.lzqdiy.MyLinearLayout>
          節(jié)點(diǎn)層次很簡(jiǎn)單,一個(gè)LinearLayout中添加了一個(gè)TextView。
          下面是java代碼:
          package test.lzqdiy;

          import android.app.Activity;
          import android.os.Bundle;

          public class TestTouchEventApp extends Activity {
              
          /** Called when the activity is first created. */
              @Override
              
          public void onCreate(Bundle savedInstanceState) {
                  
          super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
              }
          }
          package test.lzqdiy;

          import android.content.Context;
          import android.util.AttributeSet;
          import android.util.Log;
          import android.view.MotionEvent;
          import android.widget.LinearLayout;

          public class MyLinearLayout extends LinearLayout {
              
          private final String TAG = "MyLinearLayout";

              
          public MyLinearLayout(Context context, AttributeSet attrs) {

                  
          super(context, attrs);

                  Log.d(TAG, TAG);

              }

              @Override
              
          public boolean dispatchTouchEvent(MotionEvent ev) {
                  
          int action = ev.getAction();

                  
          switch (action) {

                  
          case MotionEvent.ACTION_DOWN:

                      Log.d(TAG, 
          "dispatchTouchEvent action:ACTION_DOWN");

                      
          break;

                  
          case MotionEvent.ACTION_MOVE:

                      Log.d(TAG, 
          "dispatchTouchEvent action:ACTION_MOVE");

                      
          break;

                  
          case MotionEvent.ACTION_UP:

                      Log.d(TAG, 
          "dispatchTouchEvent action:ACTION_UP");

                      
          break;

                  
          case MotionEvent.ACTION_CANCEL:

                      Log.d(TAG, 
          "dispatchTouchEvent action:ACTION_CANCEL");

                      
          break;

                  }
                  
          return super.dispatchTouchEvent(ev);
              }

              @Override
              
          public boolean onInterceptTouchEvent(MotionEvent ev) {

                  
          int action = ev.getAction();

                  
          switch (action) {

                  
          case MotionEvent.ACTION_DOWN:

                      Log.d(TAG, 
          "onInterceptTouchEvent action:ACTION_DOWN");

                      
          break;

                  
          case MotionEvent.ACTION_MOVE:

                      Log.d(TAG, 
          "onInterceptTouchEvent action:ACTION_MOVE");

                      
          break;

                  
          case MotionEvent.ACTION_UP:

                      Log.d(TAG, 
          "onInterceptTouchEvent action:ACTION_UP");

                      
          break;

                  
          case MotionEvent.ACTION_CANCEL:

                      Log.d(TAG, 
          "onInterceptTouchEvent action:ACTION_CANCEL");

                      
          break;

                  }

                  
          return false;

              }

              @Override
              
          public boolean onTouchEvent(MotionEvent ev) {

                  
          int action = ev.getAction();

                  
          switch (action) {

                  
          case MotionEvent.ACTION_DOWN:

                      Log.d(TAG, 
          "---onTouchEvent action:ACTION_DOWN");

                      
          break;

                  
          case MotionEvent.ACTION_MOVE:

                      Log.d(TAG, 
          "---onTouchEvent action:ACTION_MOVE");

                      
          break;

                  
          case MotionEvent.ACTION_UP:

                      Log.d(TAG, 
          "---onTouchEvent action:ACTION_UP");

                      
          break;

                  
          case MotionEvent.ACTION_CANCEL:

                      Log.d(TAG, 
          "---onTouchEvent action:ACTION_CANCEL");

                      
          break;

                  }

                  
          return true;
              }

          }
          package test.lzqdiy;

          import android.content.Context;
          import android.util.AttributeSet;
          import android.util.Log;
          import android.view.MotionEvent;
          import android.widget.TextView;

          public class MyTextView extends TextView {

              
          private final String TAG = "MyTextView";

              
          public MyTextView(Context context, AttributeSet attrs) {

                  
          super(context, attrs);

              }

              @Override
              
          public boolean dispatchTouchEvent(MotionEvent ev) {
                  
          int action = ev.getAction();

                  
          switch (action) {

                  
          case MotionEvent.ACTION_DOWN:

                      Log.d(TAG, 
          "dispatchTouchEvent action:ACTION_DOWN");

                      
          break;

                  
          case MotionEvent.ACTION_MOVE:

                      Log.d(TAG, 
          "dispatchTouchEvent action:ACTION_MOVE");

                      
          break;

                  
          case MotionEvent.ACTION_UP:

                      Log.d(TAG, 
          "dispatchTouchEvent action:ACTION_UP");

                      
          break;

                  
          case MotionEvent.ACTION_CANCEL:

                      Log.d(TAG, 
          "onTouchEvent action:ACTION_CANCEL");

                      
          break;

                  }
                  
          return super.dispatchTouchEvent(ev);
              }

              @Override
              
          public boolean onTouchEvent(MotionEvent ev) {

                  
          int action = ev.getAction();

                  
          switch (action) {

                  
          case MotionEvent.ACTION_DOWN:

                      Log.d(TAG, 
          "---onTouchEvent action:ACTION_DOWN");

                      
          break;

                  
          case MotionEvent.ACTION_MOVE:

                      Log.d(TAG, 
          "---onTouchEvent action:ACTION_MOVE");

                      
          break;

                  
          case MotionEvent.ACTION_UP:

                      Log.d(TAG, 
          "---onTouchEvent action:ACTION_UP");

                      
          break;

                  
          case MotionEvent.ACTION_CANCEL:

                      Log.d(TAG, 
          "---onTouchEvent action:ACTION_CANCEL");

                      
          break;

                  }

                  
          return true;

              }

          }
          為了指代方便,下面將MyLinearLayout簡(jiǎn)稱為L(zhǎng),將MyTextView簡(jiǎn)稱為T,L.onInterceptTouchEvent=true 表示的含義為MyLinearLayout中的onInterceptTouchEvent方法返回值為true,通過程序運(yùn)行時(shí)輸出的Log來說明調(diào)用時(shí)序。
          第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被誰(shuí)處理了呢?答案是LinearLayout最外層的Activity處理了TouchEvent。
          好累啊!終于完成了,寫得比較倉(cāng)促,難免有不妥之處,請(qǐng)博友們指正。





          posted on 2011-05-08 21:36 我為J狂 閱讀(12147) 評(píng)論(7)  編輯  收藏 所屬分類: android

          評(píng)論

          # re: android中Touch事件的處理邏輯[未登錄] 2011-06-28 17:04 匿名

          沒明白,寫的不好  回復(fù)  更多評(píng)論   

          # re: android中Touch事件的處理邏輯 2011-07-05 17:03 李倓

          還可以,最后的log算是清晰了  回復(fù)  更多評(píng)論   

          # re: android中Touch事件的處理邏輯 2011-07-15 14:06 趙林

          寫的很好,很清晰,贊  回復(fù)  更多評(píng)論   

          # re: android中Touch事件的處理邏輯[未登錄] 2011-12-22 09:34 匿名

          很好 非常感謝  回復(fù)  更多評(píng)論   

          # re: android中Touch事件的處理邏輯[未登錄] 2012-10-08 17:15 ddd

          一下 最頂層 一下 最外層 你敢在犀利點(diǎn)嗎  回復(fù)  更多評(píng)論   

          # re: android中Touch事件的處理邏輯 2012-10-09 18:50 LiYa

          你好,我看了下,發(fā)現(xiàn):public boolean onInterceptTouchEvent(MotionEvent ev) 只有在ViewGroup中有,View類本身并沒有該方法喎~~。  回復(fù)  更多評(píng)論   

          # re: android中Touch事件的處理邏輯 2013-03-13 23:53 啊沖

          很有幫助,謝謝樓主的細(xì)心分析  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2011年5月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 高雄县| 巴林左旗| 成武县| 东兰县| 仲巴县| 东辽县| 揭东县| 镇赉县| 陵川县| 依兰县| 乐都县| 石楼县| 同德县| 拉孜县| 开封县| 昔阳县| 遂平县| 南汇区| 西宁市| 青州市| 张掖市| 泰安市| 砚山县| 新平| 南皮县| 泗洪县| 曲阜市| 比如县| 昌宁县| 花莲县| 镇巴县| 陇川县| 郁南县| 韶山市| 汨罗市| 嵊州市| 乌兰浩特市| 易门县| 沭阳县| 湄潭县| 清新县|