Android事件分发机制源码分析上----View事件分发分析

        今天给大家带来一篇事件分发机制的分析。

      Android学习的一道门槛是自定义View&ViewGroup,自定义View中相对重要的是自定义属性,然后重写onMeasure、onLayout、onDraw方法,还包括了解onDraw,dispatchDraw,invalidate,postInvalidate,finishLayout,onSizeChanage,requestlayout等,还有就是自定义View事件冲突的解决,到了这里会有好多疑问,为什么会事件冲突,事件冲突如何解决,onTouch和onTouchEvent什么区别,为什么listView加入滑动功能后,数据列表不能再滚动,广告轮播器(viewPage)图片想要点击为什么要用button而不能用imageView,要很好的解决这些问题,就涉及到了Android的事件分发机制,要了解Android的事件分发机制就要查看android的源码。

     android的两大基础控件类型:View和ViewGroup,所有的控件都继承自View,ViewGroup也继承自View但是却有它自己的特性,这里首先分析View的事件分发机制,从一个简单的例子入手进行分析。

View没有子布局的,ViewGroup继承自View,可以有子控件。而事件即MotionEvent,最重要的有3个:

(1)MotionEvent.ACTION_DOWN  按下View,是所有事件的开始

(2)MotionEvent.ACTION_MOVE   滑动事件

(3)MotionEvent.ACTION_UP       与down对应,表示抬起

其实还有一个ACTION_CANCLE事件,是系统触发的,一般情况是ViewGroup没有拦截Down事件,在拦截Move事件时,会把当前的事件变成一个Cancle事件发送给原来接收该事件的View,后续的事件才能被当前View获取。

View没有拦截函数只有dispatchTouchEvent 和  onTouchEvent,onTouchEvent在dispatchTouchEvent函数内部,onTouchEvent函数的返回值直接影响分发函数。

//只分析正常流程的核心代码 
 public boolean dispatchTouchEvent(MotionEvent event) {

        boolean result = false;


        //如果是Down事件,会重置手势识别
        final int actionMasked = event.getActionMasked();
        if (actionMasked == MotionEvent.ACTION_DOWN) {
            // Defensive cleanup for new gesture
            stopNestedScroll();
        }

        //ENABLE_MASK 控件状态是否可用的值
        if (onFilterTouchEventForSecurity(event)) {
            if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
                result = true;
            }
            //重点1
            //此处进行第一次核心判断,li一般是不为null的,剩余的三个条件分别是
            //是否设置了onTouchListener函数,控件是否可用(一般可用),onTouch函数
            //的返回值是否是true,所以可以知道重点在onTouch函数的返回值,如果都成立则
            //result = true;会直接返回,所以这里可以知道OnTouch事件如果被设置了会先于
//onTouchEvent执行的
            ListenerInfo li = mListenerInfo;
            if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {
                result = true;
            }

    //重点2,进入此处说明上面条件不成立,会走onTouchEvent函数,如果onTouchEvent返回true
     //此处也会直接返回true
            if (!result && onTouchEvent(event)) {
                result = true;
            }
        }
      if (!result && mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
        }
        //可以看到如果result = false,则后面的事件会被中断,如果当前事件时Down,怎后面的move,up事件也就没有了,
         
        if (actionMasked == MotionEvent.ACTION_UP ||
                actionMasked == MotionEvent.ACTION_CANCEL ||
                (actionMasked == MotionEvent.ACTION_DOWN && !result)) {
            stopNestedScroll();
        }
       

        return result;
    }

onTouch事件需要用户复写,所以此处分析onTouchEvent函数,执行到了此处说明onTouchEvent返回了False或用户就没有设置onTouch事件:

//分析代码之前可以先看函数的返回值,要么是true,要么是false,看到吗就可以知道,只要控件clickable就返回true,否则就返回false
public boolean onTouchEvent(MotionEvent event) {
        final float x = event.getX();
        final float y = event.getY();
        final int viewFlags = mViewFlags;
        final int action = event.getAction();

        //是否可点击,类似button等一般都是可点击的
        final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE
                || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
                || (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;

//不可用时返回的依然是clickable的值       
 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.
            return clickable;
        }
        if (mTouchDelegate != null) {
            if (mTouchDelegate.onTouchEvent(event)) {
                return true;
            }
        }

//进入可点击逻辑
        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 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)) {
                                    //执行onClick事件
                                    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:
                    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 away
                        setPressed(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 buttons
                    if (!pointInView(x, y, mTouchSlop)) {
                        // Outside button
                        // Remove any future long press/tap checks
                        removeTapCallback();
                        removeLongPressCallback();
                        if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
                            setPressed(false);
                        }
                        mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
                    }
                    break;
            }

            return true;
        }

        return false;
    }

触摸控件会首先调用控件的dispatchTouchEvent,而且一定会被调用,onTouch事件和onTouchEvent事件在dispatchTouchEvent的内部,dispatchTouchEvent中最先经过的是onTouch方法,如果满足三个条件(前文总结)就会执行onTouch事件,如果在onTouch方法里返回了true,就会让dispatchTouchEvent方法直接返回true,不会再继续往下执行。这里有一点需要注意,onTouch事件的执行会同时触发ACTION_DOWN和ACTION_UP等事件,对于能被点击的控件,无论执行ACTION_DOWN时返回true或者false对会继续执行ACTION_UP等事件,对于不可点击的控件,如果ACTION_DOWN时返回false,则后面的事件不会再被执行。

    onTouch事件没有返回true,事件会继续传递,执行onTouchEvent函数,在其内部执行onClick事件,如果重写了onTouchEvent事件,执行逻辑后直接返回false或者true,则onClick事件也不会被执行,只有返回super.onTouchEvent(event),才会执行调用onclick的逻辑。还有在重写View的dispatchTouchEvent事件,解决事件冲突时特别是利用内部拦截法解决冲突时,要想事件继续传递或者能够接受后来的事件,必须要返回super.。

下一篇文档会介绍ViewGroup事件分发

发布了221 篇原创文章 · 获赞 78 · 访问量 68万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览