Index: chrome/android/java/src/org/chromium/chrome/browser/util/ViewUtils.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/util/ViewUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/util/ViewUtils.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..da29da5f4deffbff4c59c4f70285a41cade0732e |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/util/ViewUtils.java |
@@ -0,0 +1,79 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.util; |
+ |
+import android.graphics.Canvas; |
+import android.view.View; |
+import android.view.ViewGroup; |
+ |
+/** |
+ * View-related utility methods. |
+ */ |
+public class ViewUtils { |
+ /** |
+ * Invalidates a view and all of its descendants. |
+ */ |
+ private static void recursiveInvalidate(View view) { |
+ view.invalidate(); |
+ if (view instanceof ViewGroup) { |
+ ViewGroup group = (ViewGroup) view; |
+ int childCount = group.getChildCount(); |
+ for (int i = 0; i < childCount; i++) { |
+ View child = group.getChildAt(i); |
+ if (child.getVisibility() == View.VISIBLE) { |
+ recursiveInvalidate(child); |
+ } |
+ } |
+ } |
+ } |
+ |
+ /** |
+ * Captures a bitmap of a View and draws it to a Canvas. |
+ */ |
+ public static void captureBitmap(View view, Canvas canvas) { |
+ // Invalidate all the descendants of view, before calling view.draw(). Otherwise, some of |
+ // the descendant views may optimize away their drawing. http://crbug.com/415251 |
+ recursiveInvalidate(view); |
+ view.draw(canvas); |
+ } |
+ |
+ /** |
+ * Return the position of {@code childView} relative to {@code rootView}. {@code childView} |
+ * must be a child of {@code rootView}. This returns the relative layout position, which does |
+ * not include translations. |
+ * @param rootView The parent of {@code childView} to calculate the position relative to. |
+ * @param childView The {@link View} to calculate the position of. |
+ * @param outPosition The resulting position with the format [x, y]. |
+ */ |
+ public static void getRelativeLayoutPosition(View rootView, View childView, int[] outPosition) { |
+ assert outPosition.length == 2; |
+ outPosition[0] = 0; |
+ outPosition[1] = 0; |
+ while (childView != null && childView != rootView) { |
+ outPosition[0] += childView.getLeft(); |
+ outPosition[1] += childView.getTop(); |
+ childView = (View) childView.getParent(); |
+ } |
+ } |
+ |
+ /** |
+ * Return the position of {@code childView} relative to {@code rootView}. {@code childView} |
+ * must be a child of {@code rootView}. This returns the relative draw position, which includes |
+ * translations. |
+ * @param rootView The parent of {@code childView} to calculate the position relative to. |
+ * @param childView The {@link View} to calculate the position of. |
+ * @param outPosition The resulting position with the format [x, y]. |
+ */ |
+ public static void getRelativeDrawPosition(View rootView, View childView, int[] outPosition) { |
+ assert outPosition.length == 2; |
+ outPosition[0] = 0; |
+ outPosition[1] = 0; |
+ while (childView != null && childView != rootView) { |
+ outPosition[0] += childView.getX(); |
+ outPosition[1] += childView.getY(); |
+ childView = (View) childView.getParent(); |
+ } |
+ } |
+} |