1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.support.design.widget;
18 
19 import android.graphics.Matrix;
20 import android.graphics.Rect;
21 import android.graphics.RectF;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.view.ViewParent;
25 
26 class ViewGroupUtilsHoneycomb {
27     private static final ThreadLocal<Matrix> sMatrix = new ThreadLocal<>();
28     private static final ThreadLocal<RectF> sRectF = new ThreadLocal<>();
29     private static final Matrix IDENTITY = new Matrix();
30 
offsetDescendantRect(ViewGroup group, View child, Rect rect)31     public static void offsetDescendantRect(ViewGroup group, View child, Rect rect) {
32         Matrix m = sMatrix.get();
33         if (m == null) {
34             m = new Matrix();
35             sMatrix.set(m);
36         } else {
37             m.set(IDENTITY);
38         }
39 
40         offsetDescendantMatrix(group, child, m);
41 
42         RectF rectF = sRectF.get();
43         if (rectF == null) {
44             rectF = new RectF();
45         }
46         rectF.set(rect);
47         m.mapRect(rectF);
48         rect.set((int) (rectF.left + 0.5f), (int) (rectF.top + 0.5f),
49                 (int) (rectF.right + 0.5f), (int) (rectF.bottom + 0.5f));
50     }
51 
offsetDescendantMatrix(ViewParent target, View view, Matrix m)52     static void offsetDescendantMatrix(ViewParent target, View view, Matrix m) {
53         final ViewParent parent = view.getParent();
54         if (parent instanceof View && parent != target) {
55             final View vp = (View) parent;
56             offsetDescendantMatrix(target, vp, m);
57             m.preTranslate(-vp.getScrollX(), -vp.getScrollY());
58         }
59 
60         m.preTranslate(view.getLeft(), view.getTop());
61 
62         if (!view.getMatrix().isIdentity()) {
63             m.preConcat(view.getMatrix());
64         }
65     }
66 }
67