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 17package android.support.design.widget; 18 19import android.graphics.Rect; 20import android.os.Build; 21import android.view.View; 22import android.view.ViewGroup; 23 24class ViewGroupUtils { 25 26 private interface ViewGroupUtilsImpl { 27 void offsetDescendantRect(ViewGroup parent, View child, Rect rect); 28 } 29 30 private static class ViewGroupUtilsImplBase implements ViewGroupUtilsImpl { 31 @Override 32 public void offsetDescendantRect(ViewGroup parent, View child, Rect rect) { 33 parent.offsetDescendantRectToMyCoords(child, rect); 34 } 35 } 36 37 private static class ViewGroupUtilsImplHoneycomb implements ViewGroupUtilsImpl { 38 @Override 39 public void offsetDescendantRect(ViewGroup parent, View child, Rect rect) { 40 ViewGroupUtilsHoneycomb.offsetDescendantRect(parent, child, rect); 41 } 42 } 43 44 private static final ViewGroupUtilsImpl IMPL; 45 46 static { 47 final int version = Build.VERSION.SDK_INT; 48 if (version >= 11) { 49 IMPL = new ViewGroupUtilsImplHoneycomb(); 50 } else { 51 IMPL = new ViewGroupUtilsImplBase(); 52 } 53 } 54 55 /** 56 * This is a port of the common 57 * {@link ViewGroup#offsetDescendantRectToMyCoords(android.view.View, android.graphics.Rect)} 58 * from the framework, but adapted to take transformations into account. The result 59 * will be the bounding rect of the real transformed rect. 60 * 61 * @param descendant view defining the original coordinate system of rect 62 * @param rect (in/out) the rect to offset from descendant to this view's coordinate system 63 */ 64 static void offsetDescendantRect(ViewGroup parent, View descendant, Rect rect) { 65 IMPL.offsetDescendantRect(parent, descendant, rect); 66 } 67 68 /** 69 * Retrieve the transformed bounding rect of an arbitrary descendant view. 70 * This does not need to be a direct child. 71 * 72 * @param descendant descendant view to reference 73 * @param out rect to set to the bounds of the descendant view 74 */ 75 static void getDescendantRect(ViewGroup parent, View descendant, Rect out) { 76 out.set(0, 0, descendant.getWidth(), descendant.getHeight()); 77 offsetDescendantRect(parent, descendant, out); 78 } 79 80} 81