作为一个菜鸟,从今天开始记录2020年的源码阅读,希望能慢慢进步
TouchTarget是ViewGroup中的一个静态内部类,本次记录基于android-28的源码。
从注释看,应该是记录触碰的视图的一系列事件
源码如下
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;
// 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() {
}
//获取链表头的TouchTarget
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) {
//没有链表头,则new一个TouchTarget返回
if (sRecycleBin == null) {
target = new TouchTarget();
} else {//否则 返回链表头
target = sRecycleBin;
//链表头指向下一个
sRecycleBin = target.next;
//长度-1
sRecycledCount--;
target.next = null;
}
}
target.child = child;
target.pointerIdBits = pointerIdBits;
return target;
}
//通过头插法将该节点作为表头,原来的表头作为后继
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;
}
}
}
如图,obtain方法就是获取sRecycleBin指向的TouchTarget(next置为null),sRecycleBin指向next的对象(sRecycleBin不为null时)
recycle则是将当前的TouchTarget作为sRecycleBin插到头部