关于View的dispatchEvent onTouch onTouchEvent

来源:互联网 发布:高性价比围巾 知乎 编辑:程序博客网 时间:2024/06/05 11:15

首先先贴出View类中dispatchEvent 方法的源码。

   public boolean dispatchTouchEvent(MotionEvent event) {        if (mInputEventConsistencyVerifier != null) {            mInputEventConsistencyVerifier.onTouchEvent(event, 0);        }        if (onFilterTouchEventForSecurity(event)) {            //noinspection SimplifiableIfStatement            ListenerInfo li = mListenerInfo;            if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED                    && li.mOnTouchListener.onTouch(this, event)) {                return true;            }            if (onTouchEvent(event)) {                return true;            }        }        if (mInputEventConsistencyVerifier != null) {            mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);        }        return false;    }


在android系统中当控件,例如:Button、ImageView等被触碰时,会调用其dispatchTouchEvent方法,它们的dispatchTouchEvent方法都继承自View的dispatchTouchEvent方法。该方法的返回值决定着对应控件是否会接受接下来的事件。


下面这段代码决定着dispatchTouchEvent的返回值。

若返回true则意味着控件会继续会接收到TOUCH事件后续的UP、MOVE、CANCLE事件。

 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED                    && li.mOnTouchListener.onTouch(this, event)) {                return true;            }            if (onTouchEvent(event)) {                return true;            }

而第一个if判断语句中的

mViewFlags & ENABLED_MASK

则是判断控件的ENABLE属性的状态,该状态决定着控件是否响应onTouch(?) ,若为false则直接进入到下一个if中判断onTouchEvent的返回值。下面时onTouchEvent的源码

 public boolean onTouchEvent(MotionEvent event) {        final int viewFlags = mViewFlags;        if ((viewFlags & ENABLED_MASK) == DISABLED) {            if (event.getAction() == 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));        }        if (mTouchDelegate != null) {            if (mTouchDelegate.onTouchEvent(event)) {                return true;            }        }        if (((viewFlags & CLICKABLE) == CLICKABLE ||                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {            switch (event.getAction()) {                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);                       }                        if (!mHasPerformedLongPress) {                            // 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();                    }                    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();                        }                        postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());                    } else {                        // Not inside a scrolling container, so show the feedback right away                        setPressed(true);                        checkForLongClick(0);                    }                    break;                case MotionEvent.ACTION_CANCEL:                    setPressed(false);                    removeTapCallback();                    removeLongPressCallback();                    break;                case MotionEvent.ACTION_MOVE:                    final int x = (int) event.getX();                    final int y = (int) event.getY();                    // 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;    }

而当控件是textview 或 buton是它们的方法复写了View的dispatchEvent方法,从代码中也可以看见它会调用到View的onTouchEvent方法。所以即使在onTouch中返回false时它也能接收到接下来的UP MOVE事件因为它在onTouchEvent中会可能?返回true,从而接收到接下来的事件.

 public boolean onTouchEvent(MotionEvent event) {        final int action = event.getActionMasked();        if (mEditor != null) mEditor.onTouchEvent(event);        final boolean superResult = super.onTouchEvent(event);        /*         * Don't handle the release after a long press, because it will         * move the selection away from whatever the menu action was         * trying to affect.         */        if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {            mEditor.mDiscardNextActionUp = false;            return superResult;        }        final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) &&                (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();         if ((mMovement != null || onCheckIsTextEditor()) && isEnabled()                && mText instanceof Spannable && mLayout != null) {            boolean handled = false;            if (mMovement != null) {                handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);            }            final boolean textIsSelectable = isTextSelectable();            if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {                // The LinkMovementMethod which should handle taps on links has not been installed                // on non editable text that support text selection.                // We reproduce its behavior here to open links for these.                ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(),                        getSelectionEnd(), ClickableSpan.class);                if (links.length > 0) {                    links[0].onClick(this);                    handled = true;                }            }            if (touchIsFinished && (isTextEditable() || textIsSelectable)) {                // Show the IME, except when selecting in read-only text.                final InputMethodManager imm = InputMethodManager.peekInstance();                viewClicked(imm);                if (!textIsSelectable && mEditor.mShowSoftInputOnFocus) {                    handled |= imm != null && imm.showSoftInput(this, 0);                }                // The above condition ensures that the mEditor is not null                mEditor.onTouchUpEvent(event);                handled = true;            }            if (handled) {                return true;            }        }        return superResult;    }






0 0
原创粉丝点击