57 lines
2.1 KiB
Java
57 lines
2.1 KiB
Java
package androidx.coordinatorlayout.widget;
|
|
|
|
import android.graphics.Matrix;
|
|
import android.graphics.Rect;
|
|
import android.graphics.RectF;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.ViewParent;
|
|
import androidx.annotation.RestrictTo;
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY})
|
|
public class ViewGroupUtils {
|
|
private static final ThreadLocal<Matrix> sMatrix = new ThreadLocal<>();
|
|
private static final ThreadLocal<RectF> sRectF = new ThreadLocal<>();
|
|
|
|
private ViewGroupUtils() {
|
|
}
|
|
|
|
public static void getDescendantRect(ViewGroup viewGroup, View view, Rect rect) {
|
|
rect.set(0, 0, view.getWidth(), view.getHeight());
|
|
offsetDescendantRect(viewGroup, view, rect);
|
|
}
|
|
|
|
private static void offsetDescendantMatrix(ViewParent viewParent, View view, Matrix matrix) {
|
|
ViewParent parent = view.getParent();
|
|
if ((parent instanceof View) && parent != viewParent) {
|
|
View view2 = (View) parent;
|
|
offsetDescendantMatrix(viewParent, view2, matrix);
|
|
matrix.preTranslate((float) (-view2.getScrollX()), (float) (-view2.getScrollY()));
|
|
}
|
|
matrix.preTranslate((float) view.getLeft(), (float) view.getTop());
|
|
if (!view.getMatrix().isIdentity()) {
|
|
matrix.preConcat(view.getMatrix());
|
|
}
|
|
}
|
|
|
|
public static void offsetDescendantRect(ViewGroup viewGroup, View view, Rect rect) {
|
|
ThreadLocal<Matrix> threadLocal = sMatrix;
|
|
Matrix matrix = threadLocal.get();
|
|
if (matrix == null) {
|
|
matrix = new Matrix();
|
|
threadLocal.set(matrix);
|
|
} else {
|
|
matrix.reset();
|
|
}
|
|
offsetDescendantMatrix(viewGroup, view, matrix);
|
|
ThreadLocal<RectF> threadLocal2 = sRectF;
|
|
RectF rectF = threadLocal2.get();
|
|
if (rectF == null) {
|
|
rectF = new RectF();
|
|
threadLocal2.set(rectF);
|
|
}
|
|
rectF.set(rect);
|
|
matrix.mapRect(rectF);
|
|
rect.set((int) (rectF.left + 0.5f), (int) (rectF.top + 0.5f), (int) (rectF.right + 0.5f), (int) (rectF.bottom + 0.5f));
|
|
}
|
|
}
|