安卓事件分发学习之TouchTarget源码阅读

背景

今天开始阅读Android里触摸消息传递的源码,这篇文章记录一下ViewGroup的内部类之一TouchTarget类的源码阅读。

这个类应该是把一个事件流(从down到up或cancel等)以链表的形式存储下来


源代码

    private static final class TouchTarget {
        private static final int MAX_RECYCLED = 32; // 链表最大容量
        private static final Object sRecycleLock = new Object[0]; // 锁
        private static TouchTarget sRecycleBin; // 事件流链表头结点
        private static int sRecycledCount; // 当前链表长度

        public static final int ALL_POINTER_IDS = -1; // all ones

        // The touched child view.
        public View child; // 被触摸的子view

        // The combined bit mask of pointer ids for all pointers captured by the target.
        public int pointerIdBits;

        // The next target in the target list.
        public TouchTarget next; // 头结点的后继

        private TouchTarget() {
        }

        // 从表头获取target
        public static TouchTarget obtain(@NonNull View child, int pointerIdBits) {
            if (child == null) {
                throw new IllegalArgumentException("child must be non-null");
            }

            final TouchTarget target;
            synchronized (sRecycleLock) {
                if (sRecycleBin == null) { // 表头为空,new一个本类对象
                    target = new TouchTarget();
                } else { // 否则就从表头获取
                    target = sRecycleBin;
                    sRecycleBin = target.next;
                    sRecycledCount--; // 链表长度--
                    target.next = null;
                }
            }
            target.child = child; // 赋值子view和id
            target.pointerIdBits = pointerIdBits;
            return target;
        }

        // 把this做为表头,老的表头赋给this的后继。头插法
        public void recycle() {
            if (child == null) {
                throw new IllegalStateException("already recycled once");
            }

            synchronized (sRecycleLock) {
                if (sRecycledCount < MAX_RECYCLED) {
                    next = sRecycleBin; // 老表头做为当前结点的后继
                    sRecycleBin = this; // 当前结点做为表头
                    sRecycledCount += 1;
                } else { // 链表已满,则不会加入到链表中
                    next = null;
                }
                child = null;
            }
        }
    }


结语

代码很简单,做一个记录,以后会用到

你可能感兴趣的