From 6fb3b9eb28fad2447d8231356ee557b428ac55e5 Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Wed, 14 Aug 2013 14:57:13 -0700 Subject: [PATCH] Add motion event forwarding hidden APIs to View Change-Id: Ia7ab5496f8064c96b34912b5f5e9af6fd0978b34 --- core/java/android/view/View.java | 84 ++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 20938f5127337..542a1a882e545 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -15425,6 +15425,90 @@ public class View implements Drawable.Callback, KeyEvent.Callback, return parent; } + /** + * Transforms a motion event from view-local coordinates to on-screen + * coordinates. + * + * @param ev the view-local motion event + * @return false if the transformation could not be applied + * @hide + */ + public boolean toGlobalMotionEvent(MotionEvent ev) { + final AttachInfo info = mAttachInfo; + if (info == null) { + return false; + } + + transformMotionEventToGlobal(ev); + ev.offsetLocation(info.mWindowLeft, info.mWindowTop); + return true; + } + + /** + * Transforms a motion event from on-screen coordinates to view-local + * coordinates. + * + * @param ev the on-screen motion event + * @return false if the transformation could not be applied + * @hide + */ + public boolean toLocalMotionEvent(MotionEvent ev) { + final AttachInfo info = mAttachInfo; + if (info == null) { + return false; + } + + ev.offsetLocation(-info.mWindowLeft, -info.mWindowTop); + transformMotionEventToLocal(ev); + return true; + } + + /** + * Recursive helper method that applies transformations in post-order. + * + * @param ev the on-screen motion event + */ + private void transformMotionEventToLocal(MotionEvent ev) { + final ViewParent parent = mParent; + if (parent instanceof View) { + final View vp = (View) parent; + vp.transformMotionEventToLocal(ev); + ev.offsetLocation(vp.mScrollX, vp.mScrollY); + } else if (parent instanceof ViewRootImpl) { + final ViewRootImpl vr = (ViewRootImpl) parent; + ev.offsetLocation(0, vr.mCurScrollY); + } + + ev.offsetLocation(-mLeft, -mTop); + + if (!hasIdentityMatrix()) { + ev.transform(getInverseMatrix()); + } + } + + /** + * Recursive helper method that applies transformations in pre-order. + * + * @param ev the on-screen motion event + */ + private void transformMotionEventToGlobal(MotionEvent ev) { + if (!hasIdentityMatrix()) { + ev.transform(getMatrix()); + } + + ev.offsetLocation(mLeft, mTop); + + final ViewParent parent = mParent; + if (parent instanceof View) { + final View vp = (View) parent; + ev.offsetLocation(-vp.mScrollX, -vp.mScrollY); + vp.transformMotionEventToGlobal(ev); + } else if (parent instanceof ViewRootImpl) { + final ViewRootImpl vr = (ViewRootImpl) parent; + ev.offsetLocation(0, -vr.mCurScrollY); + } + } + /** *

Computes the coordinates of this view on the screen. The argument * must be an array of two integers. After the method returns, the array