From 1cc48e1d0c0bd30bc59cc37c4d49ee78b01424f0 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Thu, 15 Sep 2016 12:32:28 -0700 Subject: [PATCH] Adding support for extending the touch target in the touchDelegate Bug: 31458312 Change-Id: Ic819bdede2776ff63ec17053cc1326415edc1ca0 --- .../launcher3/util/TransformingTouchDelegate.java | 29 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/com/android/launcher3/util/TransformingTouchDelegate.java b/src/com/android/launcher3/util/TransformingTouchDelegate.java index da1de5e77..3197ba946 100644 --- a/src/com/android/launcher3/util/TransformingTouchDelegate.java +++ b/src/com/android/launcher3/util/TransformingTouchDelegate.java @@ -33,6 +33,10 @@ public class TransformingTouchDelegate extends TouchDelegate { private final RectF mBounds; + private final RectF mTouchCheckBounds; + private float mTouchExtension; + private boolean mWasTouchOutsideBounds; + private View mDelegateView; private boolean mDelegateTargeted; @@ -41,10 +45,22 @@ public class TransformingTouchDelegate extends TouchDelegate { mDelegateView = delegateView; mBounds = new RectF(); + mTouchCheckBounds = new RectF(); } public void setBounds(int left, int top, int right, int bottom) { mBounds.set(left, top, right, bottom); + updateTouchBounds(); + } + + public void extendTouchBounds(float extension) { + mTouchExtension = extension; + updateTouchBounds(); + } + + private void updateTouchBounds() { + mTouchCheckBounds.set(mBounds); + mTouchCheckBounds.inset(-mTouchExtension, -mTouchExtension); } public void setDelegateView(View view) { @@ -62,8 +78,9 @@ public class TransformingTouchDelegate extends TouchDelegate { boolean sendToDelegate = false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - mDelegateTargeted = mBounds.contains(event.getX(), event.getY()); + mDelegateTargeted = mTouchCheckBounds.contains(event.getX(), event.getY()); if (mDelegateTargeted) { + mWasTouchOutsideBounds = !mBounds.contains(event.getX(), event.getY()); sendToDelegate = true; } break; @@ -78,9 +95,15 @@ public class TransformingTouchDelegate extends TouchDelegate { } boolean handled = false; if (sendToDelegate) { - event.offsetLocation(-mBounds.left, -mBounds.top); + float x = event.getX(); + float y = event.getY(); + if (mWasTouchOutsideBounds) { + event.setLocation(mBounds.centerX(), mBounds.centerY()); + } else { + event.offsetLocation(-mBounds.left, -mBounds.top); + } handled = mDelegateView.dispatchTouchEvent(event); - event.offsetLocation(mBounds.left, mBounds.top); + event.setLocation(x, y); } return handled; } -- 2.11.0