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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理

發(fā)布時間:2024/4/17 Android 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在Android中需要經(jīng)常對用戶手勢進(jìn)行判斷,在判斷手勢時需要精細(xì)的分清楚每個觸摸事件以及每個View對事件的接收情況,在View,ViewGroup,Activity中都可以接收事件,在對事件進(jìn)行處理時onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent這三個函數(shù)的調(diào)用順序及關(guān)系需要好好理清楚。原理代碼有點多,如果不對著具體事例,理解起來很難。下面對著代碼進(jìn)行分析。代碼地址為:https://github.com/huangtianyu/DispatchTouchEvent,記得幫忙點Star

MainActivity.java

[java] view plaincopy
  • package?com.zqc.dispatchtouchevent;??
  • ??
  • import?android.app.Activity;??
  • import?android.os.Bundle;??
  • import?android.util.Log;??
  • import?android.view.MotionEvent;??
  • import?android.view.View;??
  • ??
  • import?static?com.zqc.dispatchtouchevent.Constants.TAG;??
  • ??
  • public?class?MainActivity?extends?Activity?implements?View.OnTouchListener?{??
  • ????private?MyView?myView;??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????Log.e(TAG,?"MainActivity?onCreate");??
  • ????????setContentView(R.layout.activity_main);??
  • ????????myView?=?(MyView)?findViewById(R.id.myview);??
  • ????????myView.setOnTouchListener(MainActivity.this);??
  • ????}??
  • ????@Override??
  • ????public?boolean?dispatchTouchEvent(MotionEvent?ev)?{??
  • ????????Log.e(TAG,?"MainActivity?dispatchTouchEvent");??
  • ????????return?super.dispatchTouchEvent(ev);??
  • ????}??
  • ????@Override??
  • ????public?boolean?onTouchEvent(MotionEvent?event)?{??
  • ????????Log.e(TAG,?"MainActivity?onTouchEvent");??
  • ????????switch?(event.getAction())?{??
  • ????????????case?MotionEvent.ACTION_DOWN:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouchEvent?ACTION_DOWN");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_MOVE:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouchEvent?ACTION_MOVE");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_CANCEL:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouchEvent?ACTION_CANCEL");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_UP:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouchEvent?ACTION_UP");??
  • ????????????????break;??
  • ????????????default:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouchEvent?"?+?event.getAction());??
  • ????????????????break;??
  • ????????}??
  • ????????return?super.onTouchEvent(event);??
  • ????}??
  • ????@Override??
  • ????protected?void?onResume()?{??
  • ????????Log.e(TAG,?"MainActivity?onResume");??
  • ????????super.onResume();??
  • ????}??
  • ????@Override??
  • ????protected?void?onPause()?{??
  • ????????Log.e(TAG,?"MainActivity?onPause");??
  • ????????super.onPause();??
  • ????}??
  • ????@Override??
  • ????public?boolean?onTouch(View?v,?MotionEvent?event)?{??
  • ????????Log.e(TAG,?"MainActivity?onTouch");??
  • ????????switch?(event.getAction()?&?MotionEvent.ACTION_MASK)?{??
  • ????????????case?MotionEvent.ACTION_DOWN:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouch?ACTION_DOWN");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_MOVE:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouch?ACTION_MOVE");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_CANCEL:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouch?ACTION_CANCEL");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_UP:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouch?ACTION_UP");??
  • ????????????????break;??
  • ????????????default:??
  • ????????????????Log.e(TAG,?"MainActivity?onTouchEvent?"?+?event.getAction());??
  • ????????????????break;??
  • ????????}??
  • ????????return?false;??
  • ????}??
  • }??
  • MyView.java

    [java] view plaincopy
  • package?com.zqc.dispatchtouchevent;??
  • ??
  • import?android.content.Context;??
  • import?android.util.AttributeSet;??
  • import?android.util.Log;??
  • import?android.view.GestureDetector;??
  • import?android.view.MotionEvent;??
  • import?android.widget.TextView;??
  • ??
  • import?static?com.zqc.dispatchtouchevent.Constants.MY_GESTURE_TAG;??
  • import?static?com.zqc.dispatchtouchevent.Constants.TAG;??
  • ??
  • ??
  • public?class?MyView?extends?TextView?{??
  • ????private?Context?mContext;??
  • ????//private?GestureDetector?mGesture;??
  • ??
  • ????public?MyView(Context?context)?{??
  • ????????this(context,?null);??
  • ????}??
  • ??
  • ????public?MyView(Context?context,?AttributeSet?attrs)?{??
  • ????????super(context,?attrs);??
  • ????????Log.e(TAG,?"MyView");??
  • ????????mContext?=?context;??
  • ????????//手勢初始化??
  • ???????//?mGesture?=?new?GestureDetector(mContext,?mGestureListener);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onTouchEvent(MotionEvent?event)?{??
  • ????????Log.e(TAG,?"MyView?onTouchEvent");??
  • ????????switch?(event.getAction())?{??
  • ????????????case?MotionEvent.ACTION_DOWN:??
  • ????????????????Log.e(TAG,?"MyView?onTouchEvent?ACTION_DOWN");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_MOVE:??
  • ????????????????Log.e(TAG,?"MyView?onTouchEvent?ACTION_MOVE");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_CANCEL:??
  • ????????????????Log.e(TAG,?"MyView?onTouchEvent?ACTION_CANCEL");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_UP:??
  • ????????????????Log.e(TAG,?"MyView?onTouchEvent?ACTION_UP");??
  • ????????????????break;??
  • ????????????default:??
  • ????????????????Log.e(TAG,?"MyView?onTouchEvent?"?+?event.getAction());??
  • ????????????????break;??
  • ????????}??
  • //????????設(shè)置手勢監(jiān)聽??
  • ???????//?mGesture.onTouchEvent(event);??
  • ????????return?super.onTouchEvent(event);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?dispatchTouchEvent(MotionEvent?event)?{??
  • ????????Log.e(TAG,?"MyView?dispatchTouchEvent");??
  • ????????return?super.dispatchTouchEvent(event);??
  • ????}??
  • }??
  • MyViewGroup.java

    [java] view plaincopy
  • package?com.zqc.dispatchtouchevent;??
  • ??
  • import?android.content.Context;??
  • import?android.util.AttributeSet;??
  • import?android.util.Log;??
  • import?android.view.MotionEvent;??
  • import?android.widget.RelativeLayout;??
  • ??
  • import?static?com.zqc.dispatchtouchevent.Constants.TAG;??
  • ??
  • public?class?MyViewGroup?extends?RelativeLayout?{??
  • ????public?MyViewGroup(Context?context)?{??
  • ????????this(context,?null);??
  • ????}??
  • ??
  • ????public?MyViewGroup(Context?context,?AttributeSet?attrs)?{??
  • ????????super(context,?attrs);??
  • ????????Log.e(TAG,?"MyViewGroup");??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onInterceptTouchEvent(MotionEvent?ev)?{??
  • ????????Log.e(TAG,?"MyViewGroup?onInterceptTouchEvent");??
  • ????????return?super.onInterceptTouchEvent(ev);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?dispatchTouchEvent(MotionEvent?ev)?{??
  • ????????Log.e(TAG,?"MyViewGroup?dispatchTouchEvent");??
  • ????????return?super.dispatchTouchEvent(ev);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onTouchEvent(MotionEvent?event)?{??
  • ????????Log.e(TAG,?"MyViewGroup?onTouchEvent");??
  • ????????switch?(event.getAction())?{??
  • ????????????case?MotionEvent.ACTION_DOWN:??
  • ????????????????Log.e(TAG,?"MyViewGroup?onTouchEvent?ACTION_DOWN");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_MOVE:??
  • ????????????????Log.e(TAG,?"MyViewGroup?onTouchEvent?ACTION_MOVE");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_CANCEL:??
  • ????????????????Log.e(TAG,?"MyViewGroup?onTouchEvent?ACTION_CANCEL");??
  • ????????????????break;??
  • ????????????case?MotionEvent.ACTION_UP:??
  • ????????????????Log.e(TAG,?"MyViewGroup?onTouchEvent?ACTION_UP");??
  • ????????????????break;??
  • ????????????default:??
  • ????????????????Log.e(TAG,?"MyViewGroup?onTouchEvent?"?+?event.getAction());??
  • ????????????????break;??
  • ????????}??
  • ????????return?super.onTouchEvent(event);??
  • ????}??
  • }??
  • Contants.java

    [java] view plaincopy
  • package?com.zqc.dispatchtouchevent;??
  • ??
  • public?class?Constants?{??
  • ??
  • ????public?final?static?String?TAG?=?"MY_LOG";??
  • ????public?final?static?String?MY_GESTURE_TAG?=?"GESTURE_TAG";??
  • }??
  • 在代碼中將每個函數(shù)分別列出并加上Log輸出,這樣對著Log日志進(jìn)行分析,則一目了然。

    1.讓所有的onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent均返回super.onTouchEvent即均返回false時,輕輕點擊MyView然后快速抬起,查看相應(yīng)的Log:

    ?

    通過Log能清楚的查看代碼執(zhí)行的流程,具體流程如下:

    DOWN事件:MainActivity.dispatchTouchEvent->MyViewGroup.dispatchTouchEvet->MyViewGroup.onInterceptTouchEvent->MyView.dispatchTouchEvent->setOnTouchListener.onTouch->MyView.onTouchEvent->MyViewGroup.onTouchEvent->MainActivity.onTouchEvent

    UP事件:MainActivity.dispatchTouchEvent->MainActivity.onTouchEvent

    從上面流程可以看出,點擊事件最先傳給窗口Activity的dispatchTouchEvent函數(shù)進(jìn)行事件分發(fā),然后對于View類,是先傳給對應(yīng)的父View的dispatchTouchEvent進(jìn)行事件分發(fā),然后在傳給里面點擊的View。當(dāng)down事件沒有被各個view消費時,最終會調(diào)用Acitivity的onTouchEvent,并在在Down后續(xù)的UP事件不在傳給MyViewGroup和MyView,直接傳給MainAcitivity。所以當(dāng)事件沒有被窗口中的View消費時,最終都是給了該窗口Activity類中的onTouchEvent事件處理。從Log中也可以看出setOnTouchListener中的onTouch事件是在對應(yīng)View的onTouchEvent事件之前被執(zhí)行。

    2.當(dāng)MainAcivity中dispathTouchEvent返回true時,輕輕點擊MyView,查看對應(yīng)Log:

    通過Log可以看到當(dāng)窗口Activity的dispatchTouchEvent返回true時,DOWN事件沒有往View中傳,也就沒有調(diào)用任何的onTouchEvent事件,UP事件也是走到Activity的dispatchTouchEvent時也就結(jié)束了。

    3.重新置Activity中dispatchTouchEvent返回false,然后置ViewGroup中onInterceptTouchEvent返回true時,輕輕點擊MyView查看對應(yīng)Log:

    這時DOWN事件和UP事件的執(zhí)行流程如下:

    DOWN事件:MainActivity.dipatchTouchEvent->MyViewGroup.dispatchTouchEvent->MyViewGroup.onInterceptTouchEvent->MyViewGroup.onTouchEvent->MainActivity.onTouchEvent.

    UP事件:MainActiviy.dispatchTouchEvent->MainActivity.onTouchEvent.

    從Log中可以看出,當(dāng)onInterceptTouchEvent返回true時,事件即被MyViewGroup攔截了,這時事件就直接傳給MyViewGroup.onTouchEvent,不在往子View傳,由于MyViewGroup.onTouchEvent返回的是false,即MyViewGroup并沒有消費事件,這時事件會傳給窗口Activity,UP事件會傳給最后一個接受Down事件的窗口或View。

    4.當(dāng)MyView中onTouchEvent返回true時,即MyView會消費傳給他的事件。輕點MyView查看對應(yīng)的Log:


    繼續(xù)分析DOWN事件的流程:

    DOWN事件:MainActivity.dispatchTouchEvent->MyViewGroup.dispatchTouchEvet->MyViewGroup.onInterceptTouchEvent->MyView.dispatchTouchEvent->setOnTouchListener.onTouch->MyView.onTouchEvent

    UP事件:MainActivity.dispatchTouchEvent->MyViewGroup.dispatchTouchEvet->MyViewGroup.onInterceptTouchEvent->MyView.dispatchTouchEvent->setOnTouchListener.onTouch->MyView.onTouchEvent

    從上面的執(zhí)行流程可以看出當(dāng)事件被MyView消費后,事件不會在往上傳,后續(xù)的UP事件也直接通過dispatchTouchEvent分發(fā)給對應(yīng)的View,這里還是提一下,在MainAcitivy中設(shè)置的setOnTouchListener中的onTouch事件是在MyView自身的onTouchEvent事件之前被執(zhí)行,因而設(shè)置的setOnTouchEvent的onTouch函數(shù)還是會被執(zhí)行。

    先只分析這幾種場景,MOVE事件和UP事件一樣只要DOWN事件被某個View消耗了,那么MOVE事件也就直接傳到這個View。可以下載代碼運行后,在MyView上面滑動下看下Log,具體Log我也貼一份。

    情況1:

    情況2:

    下面對著Android源碼來具體分析View的觸摸事件到底是怎么執(zhí)行的。首先根據(jù)Log可以最先接收到消息的是Activity的dispatchTouchEvent,在該處設(shè)置斷點,然后查看對應(yīng)的調(diào)用方法棧(你會發(fā)現(xiàn)在調(diào)到MainActivity的dispatchTouchEvent時,前面已經(jīng)調(diào)用了很多方法),如下:

    由于Android系統(tǒng)啟動后會先啟動Zygote進(jìn)程,該進(jìn)程會在手機開機后一直運行,Android中的幾個系統(tǒng)服務(wù)都是由Zygote進(jìn)程fork出來的,一個應(yīng)用在啟動時所分配到的進(jìn)程也是由Zygote進(jìn)程fork出來的,通常說一個應(yīng)用的起點是Application里面的onCreate函數(shù),其實真正的起點是ActivityThread里面的main函數(shù),看到這個main函數(shù)是不是有種熟悉的感覺啊。在main函數(shù)中初始化了應(yīng)用程序的主線程,同時初始化了主線程的消息隊列,并調(diào)用了Looper.loop()函數(shù)使主線程不斷的對消息隊列進(jìn)行循環(huán)檢測,有消息則進(jìn)行處理。點擊事件產(chǎn)生一個消息,該消息傳到InputEventReceiver后,由InputEventReceiver的繼承類WindowInputEventReceiver去處理,WindowInputEventReceiver類是ViewRootImpl類的內(nèi)部類,查看對應(yīng)代碼如下:

    ViewRootImpl.java

    [java] view plaincopy
  • final?class?WindowInputEventReceiver?extends?InputEventReceiver?{??
  • ????public?WindowInputEventReceiver(InputChannel?inputChannel,?Looper?looper)?{??
  • ????????super(inputChannel,?looper);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onInputEvent(InputEvent?event)?{??
  • ????????enqueueInputEvent(event,?this,?0,?true);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onBatchedInputEventPending()?{??
  • ????????if?(mUnbufferedInputDispatch)?{??
  • ????????????super.onBatchedInputEventPending();??
  • ????????}?else?{??
  • ????????????scheduleConsumeBatchedInput();??
  • ????????}??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?dispose()?{??
  • ????????unscheduleConsumeBatchedInput();??
  • ????????super.dispose();??
  • ????}??
  • }??
  • 查看代碼可以當(dāng)點擊消息過來時,直接調(diào)用ViewRootImpl類中的enqueueInputEvent(event,this,0,true)方法:

    ViewRootImpl.java

    [java] view plaincopy
  • void?enqueueInputEvent(InputEvent?event,??
  • ????????InputEventReceiver?receiver,?int?flags,?boolean?processImmediately)?{??
  • ????adjustInputEventForCompatibility(event);??
  • ????QueuedInputEvent?q?=?obtainQueuedInputEvent(event,?receiver,?flags);??
  • ??
  • ????//?Always?enqueue?the?input?event?in?order,?regardless?of?its?time?stamp.??
  • ????//?We?do?this?because?the?application?or?the?IME?may?inject?key?events??
  • ????//?in?response?to?touch?events?and?we?want?to?ensure?that?the?injected?keys??
  • ????//?are?processed?in?the?order?they?were?received?and?we?cannot?trust?that??
  • ????//?the?time?stamp?of?injected?events?are?monotonic.??
  • ????QueuedInputEvent?last?=?mPendingInputEventTail;??
  • ????if?(last?==?null)?{??
  • ????????mPendingInputEventHead?=?q;??
  • ????????mPendingInputEventTail?=?q;??
  • ????}?else?{??
  • ????????last.mNext?=?q;??
  • ????????mPendingInputEventTail?=?q;??
  • ????}??
  • ????mPendingInputEventCount?+=?1;??
  • ????Trace.traceCounter(Trace.TRACE_TAG_INPUT,?mPendingInputEventQueueLengthCounterName,??
  • ????????????mPendingInputEventCount);??
  • ??
  • ????if?(processImmediately)?{??
  • ????????doProcessInputEvents();??
  • ????}?else?{??
  • ????????scheduleProcessInputEvents();??
  • ????}??
  • }??
  • 由于processImmediately為true,因而是立即處理,即直接調(diào)用doProcessInputEvents();

    ViewRootImpl.java

    [java] view plaincopy
  • void?doProcessInputEvents()?{??
  • ????//?Deliver?all?pending?input?events?in?the?queue.??
  • ????while?(mPendingInputEventHead?!=?null)?{??
  • ????????QueuedInputEvent?q?=?mPendingInputEventHead;??
  • ????????mPendingInputEventHead?=?q.mNext;??
  • ????????if?(mPendingInputEventHead?==?null)?{??
  • ????????????mPendingInputEventTail?=?null;??
  • ????????}??
  • ????????q.mNext?=?null;??
  • ??
  • ????????mPendingInputEventCount?-=?1;??
  • ????????Trace.traceCounter(Trace.TRACE_TAG_INPUT,?mPendingInputEventQueueLengthCounterName,??
  • ????????????????mPendingInputEventCount);??
  • ??
  • ????????long?eventTime?=?q.mEvent.getEventTimeNano();??
  • ????????long?oldestEventTime?=?eventTime;??
  • ????????if?(q.mEvent?instanceof?MotionEvent)?{??
  • ????????????MotionEvent?me?=?(MotionEvent)q.mEvent;??
  • ????????????if?(me.getHistorySize()?>?0)?{??
  • ????????????????oldestEventTime?=?me.getHistoricalEventTimeNano(0);??
  • ????????????}??
  • ????????}??
  • ????????mChoreographer.mFrameInfo.updateInputEventTime(eventTime,?oldestEventTime);??
  • ??
  • ????????deliverInputEvent(q);??
  • ????}??
  • ??
  • ????//?We?are?done?processing?all?input?events?that?we?can?process?right?now??
  • ????//?so?we?can?clear?the?pending?flag?immediately.??
  • ????if?(mProcessInputEventsScheduled)?{??
  • ????????mProcessInputEventsScheduled?=?false;??
  • ????????mHandler.removeMessages(MSG_PROCESS_INPUT_EVENTS);??
  • ????}??
  • }??
  • z之后調(diào)用了deliverInputEvent(q)

    ViewRootImpl.java

    [java] view plaincopy
  • private?void?deliverInputEvent(QueuedInputEvent?q)?{??
  • ????Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW,?"deliverInputEvent",??
  • ????????????q.mEvent.getSequenceNumber());??
  • ????if?(mInputEventConsistencyVerifier?!=?null)?{??
  • ????????mInputEventConsistencyVerifier.onInputEvent(q.mEvent,?0);??
  • ????}??
  • ??
  • ????InputStage?stage;??
  • ????if?(q.shouldSendToSynthesizer())?{??
  • ????????stage?=?mSyntheticInputStage;??
  • ????}?else?{??
  • ????????stage?=?q.shouldSkipIme()???mFirstPostImeInputStage?:?mFirstInputStage;??
  • ????}??
  • ??
  • ????if?(stage?!=?null)?{??
  • ????????stage.deliver(q);??
  • ????}?else?{??
  • ????????finishInputEvent(q);??
  • ????}??
  • }??
  • 在這里初始化了一個InputStage類的實例,然后調(diào)用了該類的deliver(q),具體方法如下:

    [java] view plaincopy
  • /**?
  • ?????*?Base?class?for?implementing?a?stage?in?the?chain?of?responsibility?
  • ?????*?for?processing?input?events.?
  • ?????*?<p>?
  • ?????*?Events?are?delivered?to?the?stage?by?the?{@link?#deliver}?method.??The?stage?
  • ?????*?then?has?the?choice?of?finishing?the?event?or?forwarding?it?to?the?next?stage.?
  • ?????*?</p>?
  • ?????*/??
  • ????abstract?class?InputStage?{??
  • ????????private?final?InputStage?mNext;??
  • ??
  • ????????protected?static?final?int?FORWARD?=?0;??
  • ????????protected?static?final?int?FINISH_HANDLED?=?1;??
  • ????????protected?static?final?int?FINISH_NOT_HANDLED?=?2;??
  • ??
  • ????????/**?
  • ?????????*?Creates?an?input?stage.?
  • ?????????*?@param?next?The?next?stage?to?which?events?should?be?forwarded.?
  • ?????????*/??
  • ????????public?InputStage(InputStage?next)?{??
  • ????????????mNext?=?next;??
  • ????????}??
  • ??
  • ????????/**?
  • ?????????*?Delivers?an?event?to?be?processed.?
  • ?????????*/??
  • ????????public?final?void?deliver(QueuedInputEvent?q)?{??
  • ????????????if?((q.mFlags?&?QueuedInputEvent.FLAG_FINISHED)?!=?0)?{??
  • ????????????????forward(q);??
  • ????????????}?else?if?(shouldDropInputEvent(q))?{??
  • ????????????????finish(q,?false);??
  • ????????????}?else?{??
  • ????????????????apply(q,?onProcess(q));??
  • ????????????}??
  • ????????}??
  • ??
  • ????????/**?
  • ?????????*?Marks?the?the?input?event?as?finished?then?forwards?it?to?the?next?stage.?
  • ?????????*/??
  • ????????protected?void?finish(QueuedInputEvent?q,?boolean?handled)?{??
  • ????????????q.mFlags?|=?QueuedInputEvent.FLAG_FINISHED;??
  • ????????????if?(handled)?{??
  • ????????????????q.mFlags?|=?QueuedInputEvent.FLAG_FINISHED_HANDLED;??
  • ????????????}??
  • ????????????forward(q);??
  • ????????}??
  • ??
  • ????????/**?
  • ?????????*?Forwards?the?event?to?the?next?stage.?
  • ?????????*/??
  • ????????protected?void?forward(QueuedInputEvent?q)?{??
  • ????????????onDeliverToNext(q);??
  • ????????}??
  • ??
  • ????????/**?
  • ?????????*?Applies?a?result?code?from?{@link?#onProcess}?to?the?specified?event.?
  • ?????????*/??
  • ????????protected?void?apply(QueuedInputEvent?q,?int?result)?{??
  • ????????????if?(result?==?FORWARD)?{??
  • ????????????????forward(q);??
  • ????????????}?else?if?(result?==?FINISH_HANDLED)?{??
  • ????????????????finish(q,?true);??
  • ????????????}?else?if?(result?==?FINISH_NOT_HANDLED)?{??
  • ????????????????finish(q,?false);??
  • ????????????}?else?{??
  • ????????????????throw?new?IllegalArgumentException("Invalid?result:?"?+?result);??
  • ????????????}??
  • ????????}??
  • ??
  • ????????/**?
  • ?????????*?Called?when?an?event?is?ready?to?be?processed.?
  • ?????????*?@return?A?result?code?indicating?how?the?event?was?handled.?
  • ?????????*/??
  • ????????protected?int?onProcess(QueuedInputEvent?q)?{??
  • ????????????return?FORWARD;??
  • ????????}??
  • ??
  • ????????/**?
  • ?????????*?Called?when?an?event?is?being?delivered?to?the?next?stage.?
  • ?????????*/??
  • ????????protected?void?onDeliverToNext(QueuedInputEvent?q)?{??
  • ????????????if?(DEBUG_INPUT_STAGES)?{??
  • ????????????????Log.v(TAG,?"Done?with?"?+?getClass().getSimpleName()?+?".?"?+?q);??
  • ????????????}??
  • ????????????if?(mNext?!=?null)?{??
  • ????????????????mNext.deliver(q);??
  • ????????????}?else?{??
  • ????????????????finishInputEvent(q);??
  • ????????????}??
  • ????????}??
  • ??
  • ????????protected?boolean?shouldDropInputEvent(QueuedInputEvent?q)?{??
  • ????????????if?(mView?==?null?||?!mAdded)?{??
  • ????????????????Slog.w(TAG,?"Dropping?event?due?to?root?view?being?removed:?"?+?q.mEvent);??
  • ????????????????return?true;??
  • ????????????}?else?if?((!mAttachInfo.mHasWindowFocus??
  • ????????????????????&&?!q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER))?||?mStopped??
  • ????????????????????||?(mPausedForTransition?&&?!isBack(q.mEvent)))?{??
  • ????????????????//?This?is?a?focus?event?and?the?window?doesn't?currently?have?input?focus?or??
  • ????????????????//?has?stopped.?This?could?be?an?event?that?came?back?from?the?previous?stage??
  • ????????????????//?but?the?window?has?lost?focus?or?stopped?in?the?meantime.??
  • ????????????????if?(isTerminalInputEvent(q.mEvent))?{??
  • ????????????????????//?Don't?drop?terminal?input?events,?however?mark?them?as?canceled.??
  • ????????????????????q.mEvent.cancel();??
  • ????????????????????Slog.w(TAG,?"Cancelling?event?due?to?no?window?focus:?"?+?q.mEvent);??
  • ????????????????????return?false;??
  • ????????????????}??
  • ??
  • ????????????????//?Drop?non-terminal?input?events.??
  • ????????????????Slog.w(TAG,?"Dropping?event?due?to?no?window?focus:?"?+?q.mEvent);??
  • ????????????????return?true;??
  • ????????????}??
  • ????????????return?false;??
  • ????????}??
  • ??
  • ????????void?dump(String?prefix,?PrintWriter?writer)?{??
  • ????????????if?(mNext?!=?null)?{??
  • ????????????????mNext.dump(prefix,?writer);??
  • ????????????}??
  • ????????}??
  • ??
  • ????????private?boolean?isBack(InputEvent?event)?{??
  • ????????????if?(event?instanceof?KeyEvent)?{??
  • ????????????????return?((KeyEvent)?event).getKeyCode()?==?KeyEvent.KEYCODE_BACK;??
  • ????????????}?else?{??
  • ????????????????return?false;??
  • ????????????}??
  • ????????}??
  • ????}??
  • 對應(yīng)方法棧可以看出,進(jìn)過一些列調(diào)用最終會調(diào)用到ViewPostImeInputStage類的processPointerEvent方法.

    ViewRootImpl.java

    [java] view plaincopy
  • private?int?processPointerEvent(QueuedInputEvent?q)?{??
  • ????final?MotionEvent?event?=?(MotionEvent)q.mEvent;??
  • ??
  • ????mAttachInfo.mUnbufferedDispatchRequested?=?false;??
  • ????boolean?handled?=?mView.dispatchPointerEvent(event);??
  • ????if?(mAttachInfo.mUnbufferedDispatchRequested?&&?!mUnbufferedInputDispatch)?{??
  • ????????mUnbufferedInputDispatch?=?true;??
  • ????????if?(mConsumeBatchedInputScheduled)?{??
  • ????????????scheduleConsumeBatchedInputImmediately();??
  • ????????}??
  • ????}??
  • ????return?handled???FINISH_HANDLED?:?FORWARD;??
  • }??
  • 在該方法中調(diào)用了mView的dispatchPointerEvent,這個mView的初始化可以查看Activity的創(chuàng)建代碼,在Activity創(chuàng)建的時候會給Activity設(shè)置一個根布局也就是DecorView,這里的mView就是DecorView,這個DecorView是PhoneWindow的私有內(nèi)部類,它繼承于FrameLayout并實現(xiàn)了RootViewSurfaceTaker接口,但是該方法是View類的一個final方法,子類無法覆寫,直接查看View中的相應(yīng)代碼即可。代碼如下:

    View.java

    [java] view plaincopy
  • /**?
  • ?*?Dispatch?a?pointer?event.?
  • ?*?<p>?
  • ?*?Dispatches?touch?related?pointer?events?to?{@link?#onTouchEvent(MotionEvent)}?and?all?
  • ?*?other?events?to?{@link?#onGenericMotionEvent(MotionEvent)}.??This?separation?of?concerns?
  • ?*?reinforces?the?invariant?that?{@link?#onTouchEvent(MotionEvent)}?is?really?about?touches?
  • ?*?and?should?not?be?expected?to?handle?other?pointing?device?features.?
  • ?*?</p>?
  • ?*?
  • ?*?@param?event?The?motion?event?to?be?dispatched.?
  • ?*?@return?True?if?the?event?was?handled?by?the?view,?false?otherwise.?
  • ?*?@hide?
  • ?*/??
  • public?final?boolean?dispatchPointerEvent(MotionEvent?event)?{??
  • ????if?(event.isTouchEvent())?{??
  • ????????return?dispatchTouchEvent(event);??
  • ????}?else?{??
  • ????????return?dispatchGenericMotionEvent(event);??
  • ????}??
  • }??
  • 繼續(xù)查看DecorView類中的dispatchTouchEvent方法,代碼如下:

    PhoneWindow.java

    [java] view plaincopy
  • @Override??
  • public?boolean?dispatchTouchEvent(MotionEvent?ev)?{??
  • ????final?Callback?cb?=?getCallback();??
  • ????return?cb?!=?null?&&?!isDestroyed()?&&?mFeatureId?<?0???cb.dispatchTouchEvent(ev)??
  • ????????????:?super.dispatchTouchEvent(ev);??
  • }??
  • 這個getCallback也就是當(dāng)前的Activity,當(dāng)當(dāng)前Activity沒有destroy的時候即調(diào)用該Activity的dispatchTouchEvent,這里代碼就回到了應(yīng)用層了,框架層完成了很多操作,這些操作只有查看源碼才知道,這里終于回到了我們編寫代碼的地方了。當(dāng)然這之后還是會通過框架層將對應(yīng)的Touch事件傳給對應(yīng)的ViewGroup和View。下面先看下Activity中dispatchTouchEvent的代碼:

    Activity.java

    [java] view plaincopy
  • /**?
  • ?*?Called?to?process?touch?screen?events.??You?can?override?this?to?
  • ?*?intercept?all?touch?screen?events?before?they?are?dispatched?to?the?
  • ?*?window.??Be?sure?to?call?this?implementation?for?touch?screen?events?
  • ?*?that?should?be?handled?normally.?
  • ?*?
  • ?*?@param?ev?The?touch?screen?event.?
  • ?*?
  • ?*?@return?boolean?Return?true?if?this?event?was?consumed.?
  • ?*/??
  • public?boolean?dispatchTouchEvent(MotionEvent?ev)?{??
  • ????if?(ev.getAction()?==?MotionEvent.ACTION_DOWN)?{??
  • ????????onUserInteraction();??
  • ????}??
  • ????if?(getWindow().superDispatchTouchEvent(ev))?{//這個getWindow就是PhoneWindow,也就是通過PhoneWindow繼續(xù)對touch事件進(jìn)行分發(fā)。??
  • ????????return?true;??
  • ????}//當(dāng)上面返回true,也就是View把事件消費了,那么就不再調(diào)用Activity的onTouchEvent函數(shù)了。??
  • ????return?onTouchEvent(ev);??
  • }??
  • 果然這里又回到了框架層,這里getWindow就是PhoneWindow,繼續(xù)查看PhoneWindow的代碼:

    PhoneWindow.java

    [java] view plaincopy
  • @Override??
  • public?boolean?superDispatchTouchEvent(MotionEvent?event)?{??
  • ????return?mDecor.superDispatchTouchEvent(event);??
  • }??
  • 這里把事件就傳給了DecorView進(jìn)行分發(fā)。

    PhoneWindow.java->DecorView

    [java] view plaincopy
  • public?boolean?superDispatchTouchEvent(MotionEvent?event)?{??
  • ????return?super.dispatchTouchEvent(event);??
  • }??
  • 前面說過DecorView繼承于FrameLayout,這里super.dispatchTouchEvent就是調(diào)用了FrameLayout里面的dispatchTouchEvent,而FrameLayout類中并未重寫dispatchTouchEvent,因而直接調(diào)用的是ViewGroup中的dispatchTouchEvent。繼續(xù)查看代碼:

    ViewGroup.java

    [java] view plaincopy
  • /**?
  • ?*?{@inheritDoc}?
  • ?*/??
  • @Override??
  • public?boolean?dispatchTouchEvent(MotionEvent?ev)?{??
  • ????if?(mInputEventConsistencyVerifier?!=?null)?{??
  • ????????mInputEventConsistencyVerifier.onTouchEvent(ev,?1);??
  • ????}??
  • ??
  • ????//?If?the?event?targets?the?accessibility?focused?view?and?this?is?it,?start??
  • ????//?normal?event?dispatch.?Maybe?a?descendant?is?what?will?handle?the?click.??
  • ????if?(ev.isTargetAccessibilityFocus()?&&?isAccessibilityFocusedViewOrHost())?{??
  • ????????ev.setTargetAccessibilityFocus(false);??
  • ????}??
  • ??
  • ????boolean?handled?=?false;??
  • ????if?(onFilterTouchEventForSecurity(ev))?{??
  • ????????final?int?action?=?ev.getAction();??
  • ????????final?int?actionMasked?=?action?&?MotionEvent.ACTION_MASK;??
  • ??
  • ????????//?Handle?an?initial?down.??
  • ????????if?(actionMasked?==?MotionEvent.ACTION_DOWN)?{??
  • ????????????//?Throw?away?all?previous?state?when?starting?a?new?touch?gesture.??
  • ????????????//?The?framework?may?have?dropped?the?up?or?cancel?event?for?the?previous?gesture??
  • ????????????//?due?to?an?app?switch,?ANR,?or?some?other?state?change.??
  • ????????????cancelAndClearTouchTargets(ev);??
  • ????????????resetTouchState();??
  • ????????}??
  • ??
  • ????????//?Check?for?interception.??
  • ????????final?boolean?intercepted;??
  • ????????if?(actionMasked?==?MotionEvent.ACTION_DOWN??
  • ????????????????||?mFirstTouchTarget?!=?null)?{??
  • ????????????final?boolean?disallowIntercept?=?(mGroupFlags?&?FLAG_DISALLOW_INTERCEPT)?!=?0;??
  • ????????????if?(!disallowIntercept)?{??
  • ????????????????intercepted?=?onInterceptTouchEvent(ev);??
  • ????????????????ev.setAction(action);?//?restore?action?in?case?it?was?changed??
  • ????????????}?else?{??
  • ????????????????intercepted?=?false;??
  • ????????????}??
  • ????????}?else?{??
  • ????????????//?There?are?no?touch?targets?and?this?action?is?not?an?initial?down??
  • ????????????//?so?this?view?group?continues?to?intercept?touches.??
  • ????????????intercepted?=?true;??
  • ????????}??
  • ??
  • ????????//?If?intercepted,?start?normal?event?dispatch.?Also?if?there?is?already??
  • ????????//?a?view?that?is?handling?the?gesture,?do?normal?event?dispatch.??
  • ????????if?(intercepted?||?mFirstTouchTarget?!=?null)?{??
  • ????????????ev.setTargetAccessibilityFocus(false);??
  • ????????}??
  • ??
  • ????????//?Check?for?cancelation.??
  • ????????final?boolean?canceled?=?resetCancelNextUpFlag(this)??
  • ????????????????||?actionMasked?==?MotionEvent.ACTION_CANCEL;??
  • ??
  • ????????//?Update?list?of?touch?targets?for?pointer?down,?if?needed.??
  • ????????final?boolean?split?=?(mGroupFlags?&?FLAG_SPLIT_MOTION_EVENTS)?!=?0;??
  • ????????TouchTarget?newTouchTarget?=?null;??
  • ????????boolean?alreadyDispatchedToNewTouchTarget?=?false;??
  • ????????if?(!canceled?&&?!intercepted)?{??
  • ??
  • ????????????//?If?the?event?is?targeting?accessiiblity?focus?we?give?it?to?the??
  • ????????????//?view?that?has?accessibility?focus?and?if?it?does?not?handle?it??
  • ????????????//?we?clear?the?flag?and?dispatch?the?event?to?all?children?as?usual.??
  • ????????????//?We?are?looking?up?the?accessibility?focused?host?to?avoid?keeping??
  • ????????????//?state?since?these?events?are?very?rare.??
  • ????????????View?childWithAccessibilityFocus?=?ev.isTargetAccessibilityFocus()??
  • ??????????????????????findChildWithAccessibilityFocus()?:?null;??
  • ??
  • ????????????if?(actionMasked?==?MotionEvent.ACTION_DOWN??
  • ????????????????????||?(split?&&?actionMasked?==?MotionEvent.ACTION_POINTER_DOWN)??
  • ????????????????????||?actionMasked?==?MotionEvent.ACTION_HOVER_MOVE)?{??
  • ????????????????final?int?actionIndex?=?ev.getActionIndex();?//?always?0?for?down??
  • ????????????????final?int?idBitsToAssign?=?split???1?<<?ev.getPointerId(actionIndex)??
  • ????????????????????????:?TouchTarget.ALL_POINTER_IDS;??
  • ??
  • ????????????????//?Clean?up?earlier?touch?targets?for?this?pointer?id?in?case?they??
  • ????????????????//?have?become?out?of?sync.??
  • ????????????????removePointersFromTouchTargets(idBitsToAssign);??
  • ??
  • ????????????????final?int?childrenCount?=?mChildrenCount;??
  • ????????????????if?(newTouchTarget?==?null?&&?childrenCount?!=?0)?{??
  • ????????????????????final?float?x?=?ev.getX(actionIndex);??
  • ????????????????????final?float?y?=?ev.getY(actionIndex);??
  • ????????????????????//?Find?a?child?that?can?receive?the?event.??
  • ????????????????????//?Scan?children?from?front?to?back.??
  • ????????????????????final?ArrayList<View>?preorderedList?=?buildOrderedChildList();??
  • ????????????????????final?boolean?customOrder?=?preorderedList?==?null??
  • ????????????????????????????&&?isChildrenDrawingOrderEnabled();??
  • ????????????????????final?View[]?children?=?mChildren;??
  • ????????????????????for?(int?i?=?childrenCount?-?1;?i?>=?0;?i--)?{??
  • ????????????????????????final?int?childIndex?=?customOrder??
  • ??????????????????????????????????getChildDrawingOrder(childrenCount,?i)?:?i;??
  • ????????????????????????final?View?child?=?(preorderedList?==?null)??
  • ??????????????????????????????????children[childIndex]?:?preorderedList.get(childIndex);??
  • ??
  • ????????????????????????//?If?there?is?a?view?that?has?accessibility?focus?we?want?it??
  • ????????????????????????//?to?get?the?event?first?and?if?not?handled?we?will?perform?a??
  • ????????????????????????//?normal?dispatch.?We?may?do?a?double?iteration?but?this?is??
  • ????????????????????????//?safer?given?the?timeframe.??
  • ????????????????????????if?(childWithAccessibilityFocus?!=?null)?{??
  • ????????????????????????????if?(childWithAccessibilityFocus?!=?child)?{??
  • ????????????????????????????????continue;??
  • ????????????????????????????}??
  • ????????????????????????????childWithAccessibilityFocus?=?null;??
  • ????????????????????????????i?=?childrenCount?-?1;??
  • ????????????????????????}??
  • ??
  • ????????????????????????if?(!canViewReceivePointerEvents(child)??
  • ????????????????????????????????||?!isTransformedTouchPointInView(x,?y,?child,?null))?{??
  • ????????????????????????????ev.setTargetAccessibilityFocus(false);??
  • ????????????????????????????continue;??
  • ????????????????????????}??
  • ??
  • ????????????????????????newTouchTarget?=?getTouchTarget(child);??
  • ????????????????????????if?(newTouchTarget?!=?null)?{??
  • ????????????????????????????//?Child?is?already?receiving?touch?within?its?bounds.??
  • ????????????????????????????//?Give?it?the?new?pointer?in?addition?to?the?ones?it?is?handling.??
  • ????????????????????????????newTouchTarget.pointerIdBits?|=?idBitsToAssign;??
  • ????????????????????????????break;??
  • ????????????????????????}??
  • ??
  • ????????????????????????resetCancelNextUpFlag(child);??
  • ????????????????????????if?(dispatchTransformedTouchEvent(ev,?false,?child,?idBitsToAssign))?{??
  • ????????????????????????????//?Child?wants?to?receive?touch?within?its?bounds.??
  • ????????????????????????????mLastTouchDownTime?=?ev.getDownTime();??
  • ????????????????????????????if?(preorderedList?!=?null)?{??
  • ????????????????????????????????//?childIndex?points?into?presorted?list,?find?original?index??
  • ????????????????????????????????for?(int?j?=?0;?j?<?childrenCount;?j++)?{??
  • ????????????????????????????????????if?(children[childIndex]?==?mChildren[j])?{??
  • ????????????????????????????????????????mLastTouchDownIndex?=?j;??
  • ????????????????????????????????????????break;??
  • ????????????????????????????????????}??
  • ????????????????????????????????}??
  • ????????????????????????????}?else?{??
  • ????????????????????????????????mLastTouchDownIndex?=?childIndex;??
  • ????????????????????????????}??
  • ????????????????????????????mLastTouchDownX?=?ev.getX();??
  • ????????????????????????????mLastTouchDownY?=?ev.getY();??
  • ????????????????????????????newTouchTarget?=?addTouchTarget(child,?idBitsToAssign);??
  • ????????????????????????????alreadyDispatchedToNewTouchTarget?=?true;??
  • ????????????????????????????break;??
  • ????????????????????????}??
  • ??
  • ????????????????????????//?The?accessibility?focus?didn't?handle?the?event,?so?clear??
  • ????????????????????????//?the?flag?and?do?a?normal?dispatch?to?all?children.??
  • ????????????????????????ev.setTargetAccessibilityFocus(false);??
  • ????????????????????}??
  • ????????????????????if?(preorderedList?!=?null)?preorderedList.clear();??
  • ????????????????}??
  • ??
  • ????????????????if?(newTouchTarget?==?null?&&?mFirstTouchTarget?!=?null)?{??
  • ????????????????????//?Did?not?find?a?child?to?receive?the?event.??
  • ????????????????????//?Assign?the?pointer?to?the?least?recently?added?target.??
  • ????????????????????newTouchTarget?=?mFirstTouchTarget;??
  • ????????????????????while?(newTouchTarget.next?!=?null)?{??
  • ????????????????????????newTouchTarget?=?newTouchTarget.next;??
  • ????????????????????}??
  • ????????????????????newTouchTarget.pointerIdBits?|=?idBitsToAssign;??
  • ????????????????}??
  • ????????????}??
  • ????????}??
  • ??
  • ????????//?Dispatch?to?touch?targets.??
  • ????????if?(mFirstTouchTarget?==?null)?{??
  • ????????????//?No?touch?targets?so?treat?this?as?an?ordinary?view.??
  • ????????????handled?=?dispatchTransformedTouchEvent(ev,?canceled,?null,??
  • ????????????????????TouchTarget.ALL_POINTER_IDS);??
  • ????????}?else?{??
  • ????????????//?Dispatch?to?touch?targets,?excluding?the?new?touch?target?if?we?already??
  • ????????????//?dispatched?to?it.??Cancel?touch?targets?if?necessary.??
  • ????????????TouchTarget?predecessor?=?null;??
  • ????????????TouchTarget?target?=?mFirstTouchTarget;??
  • ????????????while?(target?!=?null)?{??
  • ????????????????final?TouchTarget?next?=?target.next;??
  • ????????????????if?(alreadyDispatchedToNewTouchTarget?&&?target?==?newTouchTarget)?{??
  • ????????????????????handled?=?true;??
  • ????????????????}?else?{??
  • ????????????????????final?boolean?cancelChild?=?resetCancelNextUpFlag(target.child)??
  • ????????????????????????????||?intercepted;??
  • ????????????????????if?(dispatchTransformedTouchEvent(ev,?cancelChild,??
  • ????????????????????????????target.child,?target.pointerIdBits))?{??
  • ????????????????????????handled?=?true;??
  • ????????????????????}??
  • ????????????????????if?(cancelChild)?{??
  • ????????????????????????if?(predecessor?==?null)?{??
  • ????????????????????????????mFirstTouchTarget?=?next;??
  • ????????????????????????}?else?{??
  • ????????????????????????????predecessor.next?=?next;??
  • ????????????????????????}??
  • ????????????????????????target.recycle();??
  • ????????????????????????target?=?next;??
  • ????????????????????????continue;??
  • ????????????????????}??
  • ????????????????}??
  • ????????????????predecessor?=?target;??
  • ????????????????target?=?next;??
  • ????????????}??
  • ????????}??
  • ??
  • ????????//?Update?list?of?touch?targets?for?pointer?up?or?cancel,?if?needed.??
  • ????????if?(canceled??
  • ????????????????||?actionMasked?==?MotionEvent.ACTION_UP??
  • ????????????????||?actionMasked?==?MotionEvent.ACTION_HOVER_MOVE)?{??
  • ????????????resetTouchState();??
  • ????????}?else?if?(split?&&?actionMasked?==?MotionEvent.ACTION_POINTER_UP)?{??
  • ????????????final?int?actionIndex?=?ev.getActionIndex();??
  • ????????????final?int?idBitsToRemove?=?1?<<?ev.getPointerId(actionIndex);??
  • ????????????removePointersFromTouchTargets(idBitsToRemove);??
  • ????????}??
  • ????}??
  • ??
  • ????if?(!handled?&&?mInputEventConsistencyVerifier?!=?null)?{??
  • ????????mInputEventConsistencyVerifier.onUnhandledEvent(ev,?1);??
  • ????}??
  • ????return?handled;??
  • }??
  • 代碼有點多,通過調(diào)試可知將會調(diào)用dispatchTransformedTouchEvent,查看代碼如下:

    ViewGroup.java

    [java] view plaincopy
  • /**?
  • ?*?Transforms?a?motion?event?into?the?coordinate?space?of?a?particular?child?view,?
  • ?*?filters?out?irrelevant?pointer?ids,?and?overrides?its?action?if?necessary.?
  • ?*?If?child?is?null,?assumes?the?MotionEvent?will?be?sent?to?this?ViewGroup?instead.?
  • ?*/??
  • private?boolean?dispatchTransformedTouchEvent(MotionEvent?event,?boolean?cancel,??
  • ????????View?child,?int?desiredPointerIdBits)?{??
  • ????final?boolean?handled;??
  • ??
  • ????//?Canceling?motions?is?a?special?case.??We?don't?need?to?perform?any?transformations??
  • ????//?or?filtering.??The?important?part?is?the?action,?not?the?contents.??
  • ????final?int?oldAction?=?event.getAction();??
  • ????if?(cancel?||?oldAction?==?MotionEvent.ACTION_CANCEL)?{??
  • ????????event.setAction(MotionEvent.ACTION_CANCEL);??
  • ????????if?(child?==?null)?{??
  • ????????????handled?=?super.dispatchTouchEvent(event);??
  • ????????}?else?{??
  • ????????????handled?=?child.dispatchTouchEvent(event);??
  • ????????}??
  • ????????event.setAction(oldAction);??
  • ????????return?handled;??
  • ????}??
  • ??
  • ????//?Calculate?the?number?of?pointers?to?deliver.??
  • ????final?int?oldPointerIdBits?=?event.getPointerIdBits();??
  • ????final?int?newPointerIdBits?=?oldPointerIdBits?&?desiredPointerIdBits;??
  • ??
  • ????//?If?for?some?reason?we?ended?up?in?an?inconsistent?state?where?it?looks?like?we??
  • ????//?might?produce?a?motion?event?with?no?pointers?in?it,?then?drop?the?event.??
  • ????if?(newPointerIdBits?==?0)?{??
  • ????????return?false;??
  • ????}??
  • ??
  • ????//?If?the?number?of?pointers?is?the?same?and?we?don't?need?to?perform?any?fancy??
  • ????//?irreversible?transformations,?then?we?can?reuse?the?motion?event?for?this??
  • ????//?dispatch?as?long?as?we?are?careful?to?revert?any?changes?we?make.??
  • ????//?Otherwise?we?need?to?make?a?copy.??
  • ????final?MotionEvent?transformedEvent;??
  • ????if?(newPointerIdBits?==?oldPointerIdBits)?{??
  • ????????if?(child?==?null?||?child.hasIdentityMatrix())?{??
  • ????????????if?(child?==?null)?{??
  • ????????????????handled?=?super.dispatchTouchEvent(event);??
  • ????????????}?else?{??
  • ????????????????final?float?offsetX?=?mScrollX?-?child.mLeft;??
  • ????????????????final?float?offsetY?=?mScrollY?-?child.mTop;??
  • ????????????????event.offsetLocation(offsetX,?offsetY);??
  • ??
  • ????????????????handled?=?child.dispatchTouchEvent(event);??
  • ??
  • ????????????????event.offsetLocation(-offsetX,?-offsetY);??
  • ????????????}??
  • ????????????return?handled;??
  • ????????}??
  • ????????transformedEvent?=?MotionEvent.obtain(event);??
  • ????}?else?{??
  • ????????transformedEvent?=?event.split(newPointerIdBits);??
  • ????}??
  • ??
  • ????//?Perform?any?necessary?transformations?and?dispatch.??
  • ????if?(child?==?null)?{??
  • ????????handled?=?super.dispatchTouchEvent(transformedEvent);??
  • ????}?else?{??
  • ????????final?float?offsetX?=?mScrollX?-?child.mLeft;??
  • ????????final?float?offsetY?=?mScrollY?-?child.mTop;??
  • ????????transformedEvent.offsetLocation(offsetX,?offsetY);??
  • ????????if?(!?child.hasIdentityMatrix())?{??
  • ????????????transformedEvent.transform(child.getInverseMatrix());??
  • ????????}??
  • ??
  • ????????handled?=?child.dispatchTouchEvent(transformedEvent);??
  • ????}??
  • ??
  • ????//?Done.??
  • ????transformedEvent.recycle();??
  • ????return?handled;??
  • }??
  • 在該函數(shù)中調(diào)用了child.dispatchTouchEvent(),這里便走到了子View的dispatchTouchEvent中。子View也就是MyView,也就走到了TextView的dispathTouchEvent中,由于TextView并未重寫dispathTouchEvent,因而直接進(jìn)入View的dispatchTouchEvent中,代碼如下:

    View.java

    [java] view plaincopy
  • /**?
  • ?*?Pass?the?touch?screen?motion?event?down?to?the?target?view,?or?this?
  • ?*?view?if?it?is?the?target.?
  • ?*?
  • ?*?@param?event?The?motion?event?to?be?dispatched.?
  • ?*?@return?True?if?the?event?was?handled?by?the?view,?false?otherwise.?
  • ?*/??
  • public?boolean?dispatchTouchEvent(MotionEvent?event)?{??
  • ????//?If?the?event?should?be?handled?by?accessibility?focus?first.??
  • ????if?(event.isTargetAccessibilityFocus())?{??
  • ????????//?We?don't?have?focus?or?no?virtual?descendant?has?it,?do?not?handle?the?event.??
  • ????????if?(!isAccessibilityFocusedViewOrHost())?{??
  • ????????????return?false;??
  • ????????}??
  • ????????//?We?have?focus?and?got?the?event,?then?use?normal?event?dispatch.??
  • ????????event.setTargetAccessibilityFocus(false);??
  • ????}??
  • ??
  • ????boolean?result?=?false;??
  • ??
  • ????if?(mInputEventConsistencyVerifier?!=?null)?{??
  • ????????mInputEventConsistencyVerifier.onTouchEvent(event,?0);??
  • ????}??
  • ??
  • ????final?int?actionMasked?=?event.getActionMasked();??
  • ????if?(actionMasked?==?MotionEvent.ACTION_DOWN)?{??
  • ????????//?Defensive?cleanup?for?new?gesture??
  • ????????stopNestedScroll();??
  • ????}??
  • ??
  • ????if?(onFilterTouchEventForSecurity(event))?{??
  • ????????//noinspection?SimplifiableIfStatement??
  • ????????ListenerInfo?li?=?mListenerInfo;??
  • ????????if?(li?!=?null?&&?li.mOnTouchListener?!=?null??
  • ????????????????&&?(mViewFlags?&?ENABLED_MASK)?==?ENABLED??
  • ????????????????&&?li.mOnTouchListener.onTouch(this,?event))?{//在這里就調(diào)用了setOnTouchListener中的onTouch函數(shù),如果有一個消費了,那么result=true??
  • ????????????result?=?true;??
  • ????????}??
  • ??
  • ????????if?(!result?&&?onTouchEvent(event))?{//當(dāng)上面的result為true時,子View的onTouchEvent便不會執(zhí)行了。??
  • ????????????result?=?true;??
  • ????????}??
  • ????}??
  • ??
  • ????if?(!result?&&?mInputEventConsistencyVerifier?!=?null)?{??
  • ????????mInputEventConsistencyVerifier.onUnhandledEvent(event,?0);??
  • ????}??
  • ??
  • ????//?Clean?up?after?nested?scrolls?if?this?is?the?end?of?a?gesture;??
  • ????//?also?cancel?it?if?we?tried?an?ACTION_DOWN?but?we?didn't?want?the?rest??
  • ????//?of?the?gesture.??
  • ????if?(actionMasked?==?MotionEvent.ACTION_UP?||??
  • ????????????actionMasked?==?MotionEvent.ACTION_CANCEL?||??
  • ????????????(actionMasked?==?MotionEvent.ACTION_DOWN?&&?!result))?{??
  • ????????stopNestedScroll();??
  • ????}??
  • ??
  • ????return?result;??
  • }??
  • 在該函數(shù)中看到了在MainActivity中設(shè)置的setOnTouchListener對應(yīng)的Listener接口,當(dāng)setListener中的onTouch返回true時,MyView本身的onTouchEvent便不被調(diào)用。接下來看下View的onTouchEvent代碼:

    View.java

    [java] view plaincopy
  • public?boolean?onTouchEvent(MotionEvent?event)?{??
  • ????????final?float?x?=?event.getX();??
  • ????????final?float?y?=?event.getY();??
  • ????????final?int?viewFlags?=?mViewFlags;??
  • ????????final?int?action?=?event.getAction();??
  • ??
  • ????????if?((viewFlags?&?ENABLED_MASK)?==?DISABLED)?{??
  • ????????????if?(action?==?MotionEvent.ACTION_UP?&&?(mPrivateFlags?&?PFLAG_PRESSED)?!=?0)?{??
  • ????????????????setPressed(false);??
  • ????????????}??
  • ????????????//?A?disabled?view?that?is?clickable?still?consumes?the?touch??
  • ????????????//?events,?it?just?doesn't?respond?to?them.??
  • ????????????return?(((viewFlags?&?CLICKABLE)?==?CLICKABLE??
  • ????????????????????||?(viewFlags?&?LONG_CLICKABLE)?==?LONG_CLICKABLE)??
  • ????????????????????||?(viewFlags?&?CONTEXT_CLICKABLE)?==?CONTEXT_CLICKABLE);??
  • ????????}??
  • ??
  • ????????if?(mTouchDelegate?!=?null)?{//一個View還可以設(shè)置TouchDelegate,也可以在TouchDelegate的onTouchEvent里面處理點擊事件??
  • ????????????if?(mTouchDelegate.onTouchEvent(event))?{??
  • ????????????????return?true;??
  • ????????????}??
  • ????????}??
  • ??
  • ????????if?(((viewFlags?&?CLICKABLE)?==?CLICKABLE?||??
  • ????????????????(viewFlags?&?LONG_CLICKABLE)?==?LONG_CLICKABLE)?||??
  • ????????????????(viewFlags?&?CONTEXT_CLICKABLE)?==?CONTEXT_CLICKABLE)?{??
  • ????????????switch?(action)?{??
  • ????????????????case?MotionEvent.ACTION_UP:??
  • ????????????????????boolean?prepressed?=?(mPrivateFlags?&?PFLAG_PREPRESSED)?!=?0;??
  • ????????????????????if?((mPrivateFlags?&?PFLAG_PRESSED)?!=?0?||?prepressed)?{??
  • ????????????????????????//?take?focus?if?we?don't?have?it?already?and?we?should?in??
  • ????????????????????????//?touch?mode.??
  • ????????????????????????boolean?focusTaken?=?false;??
  • ????????????????????????if?(isFocusable()?&&?isFocusableInTouchMode()?&&?!isFocused())?{??
  • ????????????????????????????focusTaken?=?requestFocus();??
  • ????????????????????????}??
  • ??
  • ????????????????????????if?(prepressed)?{??
  • ????????????????????????????//?The?button?is?being?released?before?we?actually??
  • ????????????????????????????//?showed?it?as?pressed.??Make?it?show?the?pressed??
  • ????????????????????????????//?state?now?(before?scheduling?the?click)?to?ensure??
  • ????????????????????????????//?the?user?sees?it.??
  • ????????????????????????????setPressed(true,?x,?y);??
  • ???????????????????????}??
  • ??
  • ????????????????????????if?(!mHasPerformedLongPress?&&?!mIgnoreNextUpEvent)?{??
  • ????????????????????????????//?This?is?a?tap,?so?remove?the?longpress?check??
  • ????????????????????????????removeLongPressCallback();??
  • ??
  • ????????????????????????????//?Only?perform?take?click?actions?if?we?were?in?the?pressed?state??
  • ????????????????????????????if?(!focusTaken)?{??
  • ????????????????????????????????//?Use?a?Runnable?and?post?this?rather?than?calling??
  • ????????????????????????????????//?performClick?directly.?This?lets?other?visual?state??
  • ????????????????????????????????//?of?the?view?update?before?click?actions?start.??
  • ????????????????????????????????if?(mPerformClick?==?null)?{??
  • ????????????????????????????????????mPerformClick?=?new?PerformClick();??
  • ????????????????????????????????}??
  • ????????????????????????????????if?(!post(mPerformClick))?{??
  • ????????????????????????????????????performClick();??
  • ????????????????????????????????}??
  • ????????????????????????????}??
  • ????????????????????????}??
  • ??
  • ????????????????????????if?(mUnsetPressedState?==?null)?{??
  • ????????????????????????????mUnsetPressedState?=?new?UnsetPressedState();??
  • ????????????????????????}??
  • ??
  • ????????????????????????if?(prepressed)?{??
  • ????????????????????????????postDelayed(mUnsetPressedState,??
  • ????????????????????????????????????ViewConfiguration.getPressedStateDuration());??
  • ????????????????????????}?else?if?(!post(mUnsetPressedState))?{??
  • ????????????????????????????//?If?the?post?failed,?unpress?right?now??
  • ????????????????????????????mUnsetPressedState.run();??
  • ????????????????????????}??
  • ??
  • ????????????????????????removeTapCallback();??
  • ????????????????????}??
  • ????????????????????mIgnoreNextUpEvent?=?false;??
  • ????????????????????break;??
  • ??
  • ????????????????case?MotionEvent.ACTION_DOWN:??
  • ????????????????????mHasPerformedLongPress?=?false;??
  • ??
  • ????????????????????if?(performButtonActionOnTouchDown(event))?{??
  • ????????????????????????break;??
  • ????????????????????}??
  • ??
  • ????????????????????//?Walk?up?the?hierarchy?to?determine?if?we're?inside?a?scrolling?container.??
  • ????????????????????boolean?isInScrollingContainer?=?isInScrollingContainer();??
  • ??
  • ????????????????????//?For?views?inside?a?scrolling?container,?delay?the?pressed?feedback?for??
  • ????????????????????//?a?short?period?in?case?this?is?a?scroll.??
  • ????????????????????if?(isInScrollingContainer)?{??
  • ????????????????????????mPrivateFlags?|=?PFLAG_PREPRESSED;??
  • ????????????????????????if?(mPendingCheckForTap?==?null)?{??
  • ????????????????????????????mPendingCheckForTap?=?new?CheckForTap();??
  • ????????????????????????}??
  • ????????????????????????mPendingCheckForTap.x?=?event.getX();??
  • ????????????????????????mPendingCheckForTap.y?=?event.getY();??
  • [java] view plaincopy
  • //這個注意下,這里會調(diào)用ViewRootImpl內(nèi)部函數(shù)也就是后面的MOVE為啥知道前面DOWN了??
  • postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); } else { // Not inside a scrolling container, so show the feedback right away setPressed(true, x, y);

    [java] view plaincopy
  • //這個去檢查是否有長按事件??
  • checkForLongClick(0); } break; case MotionEvent.ACTION_CANCEL: setPressed(false); removeTapCallback(); removeLongPressCallback(); mInContextButtonPress = false; mHasPerformedLongPress = false; mIgnoreNextUpEvent = false; break; case MotionEvent.ACTION_MOVE: drawableHotspotChanged(x, y); // Be lenient about moving outside of buttons if (!pointInView(x, y, mTouchSlop)) { // Outside button removeTapCallback(); if ((mPrivateFlags & PFLAG_PRESSED) != 0) { // Remove any future long press/tap checks removeLongPressCallback(); setPressed(false); } } break; } return true; } return false; }這里僅分析下DOWN事件的處理,這里會先處理按鈕自身的一些事件,具體事件見如下代碼:

    [java] view plaincopy
  • /**?
  • ?*?Performs?button-related?actions?during?a?touch?down?event.?
  • ?*?
  • ?*?@param?event?The?event.?
  • ?*?@return?True?if?the?down?was?consumed.?
  • ?*?
  • ?*?@hide?
  • ?*/??
  • protected?boolean?performButtonActionOnTouchDown(MotionEvent?event)?{??
  • ????if?(event.getToolType(0)?==?MotionEvent.TOOL_TYPE_MOUSE?&&??
  • ????????(event.getButtonState()?&?MotionEvent.BUTTON_SECONDARY)?!=?0)?{??
  • ????????showContextMenu(event.getX(),?event.getY(),?event.getMetaState());??
  • ????????mPrivateFlags?|=?PFLAG_CANCEL_NEXT_UP_EVENT;??
  • ????????return?true;??
  • ????}??
  • ????return?false;??
  • }??
  • 然后判斷當(dāng)前View的父View是否在滾動,如果不在滾動就調(diào)用postDelayed:

    View.java

    [java] view plaincopy
  • public?boolean?postDelayed(Runnable?action,?long?delayMillis)?{??
  • ????final?AttachInfo?attachInfo?=?mAttachInfo;??
  • ????if?(attachInfo?!=?null)?{??
  • ????????return?attachInfo.mHandler.postDelayed(action,?delayMillis);??
  • ????}??
  • ????//?Assume?that?post?will?succeed?later??
  • ????ViewRootImpl.getRunQueue().postDelayed(action,?delayMillis);??
  • ????return?true;??
  • }??
  • 將action延遲一段時間,用于后續(xù)判斷(是否長按事件,后續(xù)MOVE事件,UP事件)。

    轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/8915919.html

    與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。