終於建了一個自己個人小站:https://huangtianyu.gitee.io,以後優先更新小站博客,歡迎進站,O(∩_∩)O~~
在Android中需要經常對用戶手勢進行判斷,在判斷手勢時需要精細的分清楚每個觸摸事件以及每個View對事件的接收情況,在View,ViewGroup,Activity中都可以接收事件,在對事件進行處理時onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent這三個函數的調用順序及關係需要好好理清楚。原理代碼有點多,如果不對着具體事例,理解起來很難。下面對着代碼進行分析。代碼地址爲:https://github.com/huangtianyu/DispatchTouchEvent,記得幫忙點Star
MainActivity.java
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.javapackage 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;
}
// 設置手勢監聽
// mGesture.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.e(TAG, "MyView dispatchTouchEvent");
return super.dispatchTouchEvent(event);
}
}
MyViewGroup.java
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.javapackage com.zqc.dispatchtouchevent;
public class Constants {
public final static String TAG = "MY_LOG";
public final static String MY_GESTURE_TAG = "GESTURE_TAG";
}
在代碼中將每個函數分別列出並加上Log輸出,這樣對着Log日誌進行分析,則一目瞭然。1.讓所有的onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent均返回super.onTouchEvent即均返回false時,輕輕點擊MyView然後快速擡起,查看相應的Log:
通過Log能清楚的查看代碼執行的流程,具體流程如下:
DOWN事件:MainActivity.dispatchTouchEvent->MyViewGroup.dispatchTouchEvet->MyViewGroup.onInterceptTouchEvent->MyView.dispatchTouchEvent->setOnTouchListener.onTouch->MyView.onTouchEvent->MyViewGroup.onTouchEvent->MainActivity.onTouchEvent
UP事件:MainActivity.dispatchTouchEvent->MainActivity.onTouchEvent
從上面流程可以看出,點擊事件最先傳給窗口Activity的dispatchTouchEvent函數進行事件分發,然後對於View類,是先傳給對應的父View的dispatchTouchEvent進行事件分發,然後在傳給裏面點擊的View。當down事件沒有被各個view消費時,最終會調用Acitivity的onTouchEvent,並在在Down後續的UP事件不在傳給MyViewGroup和MyView,直接傳給MainAcitivity。所以當事件沒有被窗口中的View消費時,最終都是給了該窗口Activity類中的onTouchEvent事件處理。從Log中也可以看出setOnTouchListener中的onTouch事件是在對應View的onTouchEvent事件之前被執行。
2.當MainAcivity中dispathTouchEvent返回true時,輕輕點擊MyView,查看對應Log:
通過Log可以看到當窗口Activity的dispatchTouchEvent返回true時,DOWN事件沒有往View中傳,也就沒有調用任何的onTouchEvent事件,UP事件也是走到Activity的dispatchTouchEvent時也就結束了。
3.重新置Activity中dispatchTouchEvent返回false,然後置ViewGroup中onInterceptTouchEvent返回true時,輕輕點擊MyView查看對應Log:
這時DOWN事件和UP事件的執行流程如下:
DOWN事件:MainActivity.dipatchTouchEvent->MyViewGroup.dispatchTouchEvent->MyViewGroup.onInterceptTouchEvent->MyViewGroup.onTouchEvent->MainActivity.onTouchEvent.
UP事件:MainActiviy.dispatchTouchEvent->MainActivity.onTouchEvent.
從Log中可以看出,當onInterceptTouchEvent返回true時,事件即被MyViewGroup攔截了,這時事件就直接傳給MyViewGroup.onTouchEvent,不在往子View傳,由於MyViewGroup.onTouchEvent返回的是false,即MyViewGroup並沒有消費事件,這時事件會傳給窗口Activity,UP事件會傳給最後一個接受Down事件的窗口或View。
4.當MyView中onTouchEvent返回true時,即MyView會消費傳給他的事件。輕點MyView查看對應的Log:
繼續分析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
從上面的執行流程可以看出當事件被MyView消費後,事件不會在往上傳,後續的UP事件也直接通過dispatchTouchEvent分發給對應的View,這裏還是提一下,在MainAcitivy中設置的setOnTouchListener中的onTouch事件是在MyView自身的onTouchEvent事件之前被執行,因而設置的setOnTouchEvent的onTouch函數還是會被執行。
先只分析這幾種場景,MOVE事件和UP事件一樣只要DOWN事件被某個View消耗了,那麼MOVE事件也就直接傳到這個View。可以下載代碼運行後,在MyView上面滑動下看下Log,具體Log我也貼一份。
情況1:
情況2:
下面對着Android源碼來具體分析View的觸摸事件到底是怎麼執行的。首先根據Log可以最先接收到消息的是Activity的dispatchTouchEvent,在該處設置斷點,然後查看對應的調用方法棧(你會發現在調到MainActivity的dispatchTouchEvent時,前面已經調用了很多方法),如下:
由於Android系統啓動後會先啓動Zygote進程,該進程會在手機開機後一直運行,Android中的幾個系統服務都是由Zygote進程fork出來的,一個應用在啓動時所分配到的進程也是由Zygote進程fork出來的,通常說一個應用的起點是Application裏面的onCreate函數,其實真正的起點是ActivityThread裏面的main函數,看到這個main函數是不是有種熟悉的感覺啊。在main函數中初始化了應用程序的主線程,同時初始化了主線程的消息隊列,並調用了Looper.loop()函數使主線程不斷的對消息隊列進行循環檢測,有消息則進行處理。點擊事件產生一個消息,該消息傳到InputEventReceiver後,由InputEventReceiver的繼承類WindowInputEventReceiver去處理,WindowInputEventReceiver類是ViewRootImpl類的內部類,查看對應代碼如下:
ViewRootImpl.java
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();
}
}
查看代碼可以當點擊消息過來時,直接調用ViewRootImpl類中的enqueueInputEvent(event,this,0,true)方法:ViewRootImpl.java
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,因而是立即處理,即直接調用doProcessInputEvents();ViewRootImpl.java
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之後調用了deliverInputEvent(q)ViewRootImpl.java
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類的實例,然後調用了該類的deliver(q),具體方法如下:/**
* 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;
}
}
}
對應方法棧可以看出,進過一些列調用最終會調用到ViewPostImeInputStage類的processPointerEvent方法.ViewRootImpl.java
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;
}
在該方法中調用了mView的dispatchPointerEvent,這個mView的初始化可以查看Activity的創建代碼,在Activity創建的時候會給Activity設置一個根佈局也就是DecorView,這裏的mView就是DecorView,這個DecorView是PhoneWindow的私有內部類,它繼承於FrameLayout並實現了RootViewSurfaceTaker接口,但是該方法是View類的一個final方法,子類無法覆寫,直接查看View中的相應代碼即可。代碼如下:
View.java
/**
* 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);
}
}
繼續查看DecorView類中的dispatchTouchEvent方法,代碼如下:
PhoneWindow.java
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final Callback cb = getCallback();
return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev)
: super.dispatchTouchEvent(ev);
}
這個getCallback也就是當前的Activity,噹噹前Activity沒有destroy的時候即調用該Activity的dispatchTouchEvent,這裏代碼就回到了應用層了,框架層完成了很多操作,這些操作只有查看源碼才知道,這裏終於回到了我們編寫代碼的地方了。當然這之後還是會通過框架層將對應的Touch事件傳給對應的ViewGroup和View。下面先看下Activity中dispatchTouchEvent的代碼:Activity.java
/**
* 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繼續對touch事件進行分發。
return true;
}//當上面返回true,也就是View把事件消費了,那麼就不再調用Activity的onTouchEvent函數了。
return onTouchEvent(ev);
}
果然這裏又回到了框架層,這裏getWindow就是PhoneWindow,繼續查看PhoneWindow的代碼:PhoneWindow.java
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
return mDecor.superDispatchTouchEvent(event);
}
這裏把事件就傳給了DecorView進行分發。PhoneWindow.java->DecorView
public boolean superDispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
前面說過DecorView繼承於FrameLayout,這裏super.dispatchTouchEvent就是調用了FrameLayout裏面的dispatchTouchEvent,而FrameLayout類中並未重寫dispatchTouchEvent,因而直接調用的是ViewGroup中的dispatchTouchEvent。繼續查看代碼:ViewGroup.java
/**
* {@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;
}
代碼有點多,通過調試可知將會調用dispatchTransformedTouchEvent,查看代碼如下:ViewGroup.java
/**
* 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;
}
在該函數中調用了child.dispatchTouchEvent(),這裏便走到了子View的dispatchTouchEvent中。子View也就是MyView,也就走到了TextView的dispathTouchEvent中,由於TextView並未重寫dispathTouchEvent,因而直接進入View的dispatchTouchEvent中,代碼如下:View.java
/**
* 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)) {//在這裏就調用了setOnTouchListener中的onTouch函數,如果有一個消費了,那麼result=true
result = true;
}
if (!result && onTouchEvent(event)) {//當上面的result爲true時,子View的onTouchEvent便不會執行了。
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;
}
在該函數中看到了在MainActivity中設置的setOnTouchListener對應的Listener接口,當setListener中的onTouch返回true時,MyView本身的onTouchEvent便不被調用。接下來看下View的onTouchEvent代碼:
View.java
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還可以設置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();
//這個注意下,這裏會調用ViewRootImpl內部函數也就是後面的MOVE爲啥知道前面DOWN了
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); } else { // Not inside a scrolling container, so show the feedback right away setPressed(true, x, y);//這個去檢查是否有長按事件
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事件的處理,這裏會先處理按鈕自身的一些事件,具體事件見如下代碼: /**
* 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;
}
然後判斷當前View的父View是否在滾動,如果不在滾動就調用postDelayed:View.java
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延遲一段時間,用於後續判斷(是否長按事件,後續MOVE事件,UP事件)。