package com.mcxtzhang.coordinatordemo;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
/**
* 介绍:
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/24.
*/
public class ViewGroupUtils {
/**
* 获取的任意子View的bound
* Retrieve the transformed bounding rect of an arbitrary descendant view.
* This does not need to be a direct child.
*
* @param descendant descendant view to reference
* @param out rect to set to the bounds of the descendant view
*/
public static void getDescendantRect(ViewGroup parent, View descendant, Rect out) {
out.set(0, 0, descendant.getWidth(), descendant.getHeight());
offsetDescendantRect(parent, descendant, out);
}
private static final ThreadLocal<Matrix> sMatrix = new ThreadLocal<>();
private static final ThreadLocal<RectF> sRectF = new ThreadLocal<>();
static void offsetDescendantRect(ViewGroup group, View child, Rect rect) {
Matrix m = sMatrix.get();
if (m == null) {
m = new Matrix();
sMatrix.set(m);
} else {
m.reset();
}
offsetDescendantMatrix(group, child, m);
RectF rectF = sRectF.get();
if (rectF == null) {
rectF = new RectF();
sRectF.set(rectF);
}
rectF.set(rect);
m.mapRect(rectF);
rect.set((int) (rectF.left + 0.5f), (int) (rectF.top + 0.5f),
(int) (rectF.right + 0.5f), (int) (rectF.bottom + 0.5f));
}
static void offsetDescendantMatrix(ViewParent target, View view, Matrix m) {
final ViewParent parent = view.getParent();
if (parent instanceof View && parent != target) {
final View vp = (View) parent;
offsetDescendantMatrix(target, vp, m);
m.preTranslate(-vp.getScrollX(), -vp.getScrollY());
}
m.preTranslate(view.getLeft(), view.getTop());
if (!view.getMatrix().isIdentity()) {
m.preConcat(view.getMatrix());
}
}
}