Android 多点touch触控事件传递

来源:互联网 发布:炉石传说淘宝买卡包 编辑:程序博客网 时间:2024/04/30 12:14

上篇讲到 viewgroup的事件分发,纯粹是按单点触控来说明,
在看 onDisptchTouchEvent 的时候,发现他是用了一个 mFirstTouchTarget 成员变量 来记住当前是否为第一个触控点,然后下个触摸点下来的时候 会检查 mFirstTouchTarget 是不是已经存在,如果已经存在 那么他就会调用 dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS); 来分发这次触摸事件,有趣的是dispatchTransformedTouchEvent ,他会根据 传进的第三个参数 child进行判断是否为null,如果为空那么说明他已经没有子view了。他就会直接调用 super.dispatchTouchEvent 来让他的父类方法,即View.dispatchTouchEvent 来判断是否 需要拦截,或者处理。出现这种情况的话 就会最终调用 这个viewgroup的ontouch事件,或者是onTouchListenner

 /**     * 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;        }

最后会在 如果event事件为cancel的时候,去调用 resetTouchState(); 重置掉 mFirstTouchTarget

0 0
原创粉丝点击