android事件是一级一级传递的,假设父控件不拦截。就传给子控件,假设父控件想要消费事件也就是拦截事件的话,须要重写这种方法

public boolean onInterceptTouchEvent(MotionEvent ev),假设返回true,则父控件自己处理,须要再重写onTouchEvent方法。这时候

@Override

    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (mInputEventConsistencyVerifier != null) {

            mInputEventConsistencyVerifier.onTouchEvent(ev, 1);

        }



        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;

            }



            // 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 (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 (childrenCount != 0) {

                        // Find a child that can receive the event.

                        // Scan children from front to back.

                        final View[] children = mChildren;

                        final float x = ev.getX(actionIndex);

                        final float y = ev.getY(actionIndex);



                        final boolean customOrder = isChildrenDrawingOrderEnabled();

                        for (int i = childrenCount - 1; i >= 0; i--) {

                            final int childIndex = customOrder ?

getChildDrawingOrder(childrenCount, i) : i;

                            final View child = children[childIndex];

                            if (!canViewReceivePointerEvents(child)

                                    || !isTransformedTouchPointInView(x, y, child, null)) {

                                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();

                                mLastTouchDownIndex = childIndex;

                                mLastTouchDownX = ev.getX();

                                mLastTouchDownY = ev.getY();

                                newTouchTarget = addTouchTarget(child, idBitsToAssign);

                                alreadyDispatchedToNewTouchTarget = true;

                                break;

                            }

                        }

                    }



                    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;

    }



方法里就会推断是否拦截动作

View事件传递,当传给子View时,子View可能会有OnTouch事件或者OnClick事件。这时就要分情况,假设

 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;

    }

这时会先推断是否有设置OnTouchListener,假设有而且是已点击的。就运行onTouch方法,假设返回true则不运行onTouchEvent方法,这时OnClick事件就没有运行了。

仅仅有onTouch返回false才会运行onTouchEvent方法。这里就能够捕捉down跟up、move事件了。在down时要返回true。

Android事件机制全然解析的更多相关文章

  1. Android 进阶学习:事件分发机制全然解析,带你从源代码的角度彻底理解(上)

    http://blog.csdn.net/guolin_blog/article/details/9097463 事实上我一直准备写一篇关于Android事件分发机制的文章,从我的第一篇博客開始,就零 ...

  2. Android事件机制之二:onTouch详解

    <Android事件机制之一:事件传递和消费>一文总结了Android中的事件传递和消费机制. 在其中对OntachEvent中的总结中,不是很具体.本文将主要对onTach进行总结. o ...

  3. Android事件机制之一:事件传递和消费

    http://www.cnblogs.com/lwbqqyumidi/p/3500997.html 关于Android中的事件机制,用到的地方还是很多的,并且这个知识点还真有点复杂. 在写这篇文章前, ...

  4. Android四大组件全然解析(一)---Activity

    本文參考\android\android\frameworks\base\core\java\android\app\Activity.java文件里的类凝视.以及android/frameworks ...

  5. Android事件机制

    一句话描述: 用户和程序之间的互动机制 什么是事件? 用户和程序交互时触发的程序操作. 只要是事件,必须具备三方面: 1 事件的发生者 2 事件接受者 3 事件触发和传递 事件处理的方法 观察者模式: ...

  6. Android IPC机制全解析<一>

    概要 多进程概念及多进程常见注意事项 IPC基础:Android序列化和Binder 跨进程常见的几种通信方式:Bundle通过Intent传递数据,文件共享,ContentProvider,基于Bi ...

  7. Android IPC机制全解析<二>

    在AIDL文件中并不是所有的数据类型都可以使用,AIDL支持的数据类型如下: 基本数据类型(int.long.char.boolean.double等) String和CharSequence Lis ...

  8. Android 事件分发机制具体解释

    很多其它内容请參照我的个人网站: http://stackvoid.com/ 网上非常多关于Android事件分发机制的解释,大多数描写叙述的都不够清晰,没有吧来龙去脉搞清晰,本文将带你从Touch事 ...

  9. 通俗理解Android事件分发与消费机制

    深入:Android Touch事件传递机制全面解析(从WMS到View树) 通俗理解Android事件分发与消费机制 说起Android滑动冲突,是个很常见的场景,比如SliddingMenu与Li ...

随机推荐

  1. MS Chart 学习心得

    利用Chart控件对学生信息进行统计,最终结果如下: Chart图形控件主要由以下几个部份组成: 1.Annotations --图形注解集合2.ChartAreas  --图表区域集合3.Legen ...

  2. POJ 2115 模线性方程 ax=b(mod n)

    /* (x*c+a)%(2^k)==b →(x*c)%(2^k)==b-a 满足定理: 推论1:方程ax=b(mod n)对于未知量x有解,当且仅当gcd(a,n) | b. 推论2:方程ax=b(m ...

  3. Lowest Common Ancestor of a Binary Tree, with Parent Pointer

    Given a binary tree, find the lowest common ancestor of two given nodes in tree. Each node contains ...

  4. 关于类似于自动填充搜索框的DEMO

    接了个单子,客户要求左边输入时,右边自动到数据库查出对应内容,如果是单个INPUT还好,这个是动态增加INPUT,不过都是一样,关键是思路 这里遇到最郁闷的问题,就是我用的JQ1.9 以前用的JQ1. ...

  5. js函数调用模式总结

    在javascript中一共有四种调用模式:方法调用模式.函数调用模式.构造器调用模式和apply调用模式.这些模式在如何初始化关键参数this上存在差异 方法调用模式 当一个函数被保存为对象的一个属 ...

  6. Qt开发小工具之gif转换器(使用QMovie截取每一帧为QImage,然后用QFile另存为图片文件)

    最近,QQ上好多各种gif表情.每一个都很经典呀..于是我就想把它转换成一张张静态图片...没学过ps.于是写了几行代码.完工.核心代码如下 主要是借助QMovie类.文件读取模式选择QMovie:: ...

  7. json对象的封装与解析

    一.解析json对象 表结构信息对象,json格式,名称为tableObj   *  {   *   "tableName":"t_res",          ...

  8. 在cnblog中使用syntax方法

    <pre name="code" class="brush: cpp;"> 代码 </pre> #include<cstdio&g ...

  9. PhoneGap 开发笔记

    1 调死调活都调不出来的情况下,可以考虑更换下phoneGap 版本,尽量用比较新的版本. 2 form submit 会返回 3 jquery mobile 的4个初始化事件 第一个触发的事件是mo ...

  10. PHP第一章学习——了解PHP(下)

    继续昨天的部分! —————————————————————————————— 首先Ubuntu下安装Apache软件: ubuntu更新源有问题,又要解决半天! 我现在很冷静! 安装Apache教程 ...