View的Touch事件分发(二.源码分析)
Android中Touch事件的分發又分為View和ViewGroup的事件分發,先來看簡單的View的touch事件分發。
主要分析View的dispatchTouchEvent()方法和onTouchEvent()方法。
View的Touch事件分發是從dispatchTouchEvent()方法開始的,父容器將touch事件傳遞給View的dispatchTouchEvent()方法,dispatchTouchEvent()方法源碼如下:
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 gesturestopNestedScroll();}if (onFilterTouchEventForSecurity(event)) {//noinspection SimplifiableIfStatement//view的一個內部類,定義了一些常用監聽器接口的實例//如OnClickListener OnLongClickListener onTouchListener等ListenerInfo li = mListenerInfo;//首先判斷li是否為空,一般情況是不為空//再判斷li.mOnTouchListener是否為空//如果對view設置了setOnTouchListener,不會為空//然后判斷View是否為激活Enabled的狀態//再調用OnTouchListener()監聽器的onTouch()方法,//并根據返回值來決定是否消費這次的touch事件//返回true,表示消費這次的touch事件,返回false不再繼續往下面走了if (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED&& li.mOnTouchListener.onTouch(this, event)) {result = true;}//如果沒有對View設置OnTouchListener()//或者View為disable狀態//或者設置的OnTouchListener()的onTouch()方法返回false//那么會去調用View的onTouchEvent()方法,//由View的onTouchEvent()方法的返回值來決定是否消費此次touch事件if (!result && onTouchEvent(event)) {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;
}
如果OnTouchListener()的onTouch()方法返回false,即onTouchListener的onTouch()方法沒有消費touch事件,同時View的onTouchEvent()方法也返回false,表示view自身沒有消費這次touch事件,那么就直接返回result=false,表示該view沒有消費這次touch事件,會將該touch事件交給其父容器的onTouchEvent()進行處理。
View的onTouchEvent()方法源碼分析如下:
view的onTouchEvent()方法由view的dispatchTouchEvent()方法進行調用
public boolean onTouchEvent(MotionEvent event) {final float x = event.getX();final float y = event.getY();final int viewFlags = mViewFlags;final int action = event.getAction();//首先判斷view的是否可點擊//通過CLICKABLE,LONG_CLICKABLE,CONTEXT_CLICKABLE三個flag,//來決定clickable的值//以上的三個分別表示view是否可以點擊以及長點擊final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;//判斷view是否是DISABLED未激活的狀態if ((viewFlags & ENABLED_MASK) == DISABLED) {if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {setPressed(false);}mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;// A disabled view that is clickable still consumes the touch// events, it just doesn't respond to them.//如果View是未激活DISABLED的狀態,//但是只要該view的viewFlags為//CLICKABLE LONG_CLICKABLE CONTEXT_CLICKABLE中的一種的話//那么該view還是可以消費掉這次touch事件的,//只是不會響應我們設置的OnCLickListener的onClick()方法 //onLongClickListener的onLongClick()方法等等return clickable;}if (mTouchDelegate != null) {//如果設置了TouchDelegate代理對象,//就將touch事件交給TouchDelegate對象處理直接返回trueif (mTouchDelegate.onTouchEvent(event)) {return true;}}//根據ACTION類型處理事件if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {switch (action) {case MotionEvent.ACTION_UP:mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;if ((viewFlags & TOOLTIP) == TOOLTIP) {handleTooltipUp();}if (!clickable) {removeTapCallback();removeLongPressCallback();mInContextButtonPress = false;mHasPerformedLongPress = false;mIgnoreNextUpEvent = false;break;}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 checkremoveLongPressCallback();// Only perform take click actions if we were in the pressed stateif (!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)) {//會去執行OnClickListener的onClick()方法performClick();}}}if (mUnsetPressedState == null) {mUnsetPressedState = new UnsetPressedState();}if (prepressed) {postDelayed(mUnsetPressedState,ViewConfiguration.getPressedStateDuration());} else if (!post(mUnsetPressedState)) {// If the post failed, unpress right nowmUnsetPressedState.run();}removeTapCallback();}mIgnoreNextUpEvent = false;break;case MotionEvent.ACTION_DOWN:if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {mPrivateFlags3 |= PFLAG3_FINGER_DOWN;}mHasPerformedLongPress = false;if (!clickable) {checkForLongClick(0, x, y);break;}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();postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());} else {// Not inside a scrolling container, so show the feedback right awaysetPressed(true, x, y);checkForLongClick(0, x, y);}break;case MotionEvent.ACTION_CANCEL:if (clickable) {setPressed(false);}removeTapCallback();removeLongPressCallback();mInContextButtonPress = false;mHasPerformedLongPress = false;mIgnoreNextUpEvent = false;mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;break;case MotionEvent.ACTION_MOVE:if (clickable) {drawableHotspotChanged(x, y);}// Be lenient about moving outside of buttonsif (!pointInView(x, y, mTouchSlop)) {// Outside button// Remove any future long press/tap checksremoveTapCallback();removeLongPressCallback();if ((mPrivateFlags & PFLAG_PRESSED) != 0) {setPressed(false);}mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;}break;}//只要clickable為true,即view只要是可點擊的,//就會消費掉touch事件返回truereturn true;}//不消費touch事件返回falsereturn false;
}
結論:
1.OnTouchListener()的onTouch()方法的執行是優先于View的OnTouchEvent()方法。
2.OnTouchListener的onTouch()方法返回了true,表示消費了touch事件,后續View的onTouchEvent(),onClick(),onLongClick()也就不會再執行了。
3.onClick(),onLongClick()等方法都是在onTouchEvnet()方法中進行執行的,ACTION_DOWN中處理長點擊onLongClick(),ACTION_UP中處理點擊onClick()。
4.如果View是未激活的,即處于DISABLED狀態但是是可點擊的(CLICKABLE LONG_CLICKABLE CONTEXT_CLICKALE),此時view也會消費掉touch事件,但是不會響應OnClickListener的onClick()方法,onLongClickListener的onLongCLick()方法
5.只要是View可點擊的并且處于ENABLED狀態,就一定返回true,即一定會消費touch事件。
6.onTouch(),onTouchEvent()中事件是否被消費了,由方法的返回值來決定,而不是由我們是否在方法中使用了touch事件MotionEvent來決定的。
總結
以上是生活随笔為你收集整理的View的Touch事件分发(二.源码分析)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: View的Touch事件分发(一.初步了
- 下一篇: ViewGroup的Touch事件分发(