Android 多点touch触控事件传递

上篇讲到 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值