Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.view; |
| 18 | |
| 19 | import android.content.Context; |
Adam Powell | 3307958 | 2012-10-09 11:20:39 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 21 | import android.os.Build; |
| 22 | import android.os.Handler; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 23 | import android.os.SystemClock; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 24 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 25 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 26 | * Detects scaling transformation gestures using the supplied {@link MotionEvent}s. |
| 27 | * The {@link OnScaleGestureListener} callback will notify users when a particular |
| 28 | * gesture event has occurred. |
| 29 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 30 | * This class should only be used with {@link MotionEvent}s reported via touch. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 31 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 32 | * To use this class: |
| 33 | * <ul> |
| 34 | * <li>Create an instance of the {@code ScaleGestureDetector} for your |
| 35 | * {@link View} |
| 36 | * <li>In the {@link View#onTouchEvent(MotionEvent)} method ensure you call |
| 37 | * {@link #onTouchEvent(MotionEvent)}. The methods defined in your |
| 38 | * callback will be executed when the events occur. |
| 39 | * </ul> |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 40 | */ |
| 41 | public class ScaleGestureDetector { |
Adam Powell | 0818020 | 2011-03-07 16:48:29 -0800 | [diff] [blame] | 42 | private static final String TAG = "ScaleGestureDetector"; |
| 43 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 44 | /** |
| 45 | * The listener for receiving notifications when gestures occur. |
| 46 | * If you want to listen for all the different gestures then implement |
| 47 | * this interface. If you only want to listen for a subset it might |
| 48 | * be easier to extend {@link SimpleOnScaleGestureListener}. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 49 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 50 | * An application will receive events in the following order: |
| 51 | * <ul> |
Adam Powell | ab905c87 | 2010-02-03 11:01:58 -0800 | [diff] [blame] | 52 | * <li>One {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)} |
| 53 | * <li>Zero or more {@link OnScaleGestureListener#onScale(ScaleGestureDetector)} |
| 54 | * <li>One {@link OnScaleGestureListener#onScaleEnd(ScaleGestureDetector)} |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 55 | * </ul> |
| 56 | */ |
| 57 | public interface OnScaleGestureListener { |
| 58 | /** |
| 59 | * Responds to scaling events for a gesture in progress. |
| 60 | * Reported by pointer motion. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 61 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 62 | * @param detector The detector reporting the event - use this to |
| 63 | * retrieve extended info about event state. |
| 64 | * @return Whether or not the detector should consider this event |
| 65 | * as handled. If an event was not handled, the detector |
| 66 | * will continue to accumulate movement until an event is |
| 67 | * handled. This can be useful if an application, for example, |
| 68 | * only wants to update scaling factors if the change is |
| 69 | * greater than 0.01. |
| 70 | */ |
| 71 | public boolean onScale(ScaleGestureDetector detector); |
| 72 | |
| 73 | /** |
| 74 | * Responds to the beginning of a scaling gesture. Reported by |
| 75 | * new pointers going down. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 76 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 77 | * @param detector The detector reporting the event - use this to |
| 78 | * retrieve extended info about event state. |
| 79 | * @return Whether or not the detector should continue recognizing |
| 80 | * this gesture. For example, if a gesture is beginning |
| 81 | * with a focal point outside of a region where it makes |
| 82 | * sense, onScaleBegin() may return false to ignore the |
| 83 | * rest of the gesture. |
| 84 | */ |
| 85 | public boolean onScaleBegin(ScaleGestureDetector detector); |
| 86 | |
| 87 | /** |
| 88 | * Responds to the end of a scale gesture. Reported by existing |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 89 | * pointers going up. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 90 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 91 | * Once a scale has ended, {@link ScaleGestureDetector#getFocusX()} |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 92 | * and {@link ScaleGestureDetector#getFocusY()} will return focal point |
| 93 | * of the pointers remaining on the screen. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 94 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 95 | * @param detector The detector reporting the event - use this to |
| 96 | * retrieve extended info about event state. |
| 97 | */ |
| 98 | public void onScaleEnd(ScaleGestureDetector detector); |
| 99 | } |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 100 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 101 | /** |
| 102 | * A convenience class to extend when you only want to listen for a subset |
| 103 | * of scaling-related events. This implements all methods in |
| 104 | * {@link OnScaleGestureListener} but does nothing. |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 105 | * {@link OnScaleGestureListener#onScale(ScaleGestureDetector)} returns |
| 106 | * {@code false} so that a subclass can retrieve the accumulated scale |
| 107 | * factor in an overridden onScaleEnd. |
| 108 | * {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)} returns |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 109 | * {@code true}. |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 110 | */ |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 111 | public static class SimpleOnScaleGestureListener implements OnScaleGestureListener { |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 112 | |
| 113 | public boolean onScale(ScaleGestureDetector detector) { |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 114 | return false; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | public boolean onScaleBegin(ScaleGestureDetector detector) { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | public void onScaleEnd(ScaleGestureDetector detector) { |
| 122 | // Intentionally empty |
| 123 | } |
| 124 | } |
| 125 | |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 126 | private final Context mContext; |
| 127 | private final OnScaleGestureListener mListener; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 128 | |
| 129 | private float mFocusX; |
| 130 | private float mFocusY; |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 131 | |
Mindy Pereira | 9f1221f | 2013-09-16 11:36:12 -0700 | [diff] [blame] | 132 | private boolean mQuickScaleEnabled; |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 133 | private boolean mStylusScaleEnabled; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 134 | |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 135 | private float mCurrSpan; |
| 136 | private float mPrevSpan; |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 137 | private float mInitialSpan; |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 138 | private float mCurrSpanX; |
| 139 | private float mCurrSpanY; |
| 140 | private float mPrevSpanX; |
| 141 | private float mPrevSpanY; |
| 142 | private long mCurrTime; |
| 143 | private long mPrevTime; |
| 144 | private boolean mInProgress; |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 145 | private int mSpanSlop; |
Adam Powell | 828e56e | 2012-09-14 18:56:16 -0700 | [diff] [blame] | 146 | private int mMinSpan; |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 147 | |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 148 | // Bounds for recently seen values |
| 149 | private float mTouchUpper; |
| 150 | private float mTouchLower; |
| 151 | private float mTouchHistoryLastAccepted; |
| 152 | private int mTouchHistoryDirection; |
| 153 | private long mTouchHistoryLastAcceptedTime; |
| 154 | private int mTouchMinMajor; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 155 | private final Handler mHandler; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 156 | |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 157 | private float mAnchoredScaleStartX; |
| 158 | private float mAnchoredScaleStartY; |
| 159 | private int mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE; |
| 160 | |
Adam Powell | d736d20 | 2012-09-25 19:30:44 -0700 | [diff] [blame] | 161 | private static final long TOUCH_STABILIZE_TIME = 128; // ms |
Mindy Pereira | 24870ce | 2013-09-09 15:56:23 -0700 | [diff] [blame] | 162 | private static final float SCALE_FACTOR = .5f; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 163 | private static final int ANCHORED_SCALE_MODE_NONE = 0; |
| 164 | private static final int ANCHORED_SCALE_MODE_DOUBLE_TAP = 1; |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 165 | private static final int ANCHORED_SCALE_MODE_STYLUS = 2; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 166 | |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 167 | |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Consistency verifier for debugging purposes. |
| 170 | */ |
| 171 | private final InputEventConsistencyVerifier mInputEventConsistencyVerifier = |
| 172 | InputEventConsistencyVerifier.isInstrumentationEnabled() ? |
| 173 | new InputEventConsistencyVerifier(this, 0) : null; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 174 | private GestureDetector mGestureDetector; |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 175 | |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 176 | private boolean mEventBeforeOrAboveStartingGestureEvent; |
| 177 | |
| 178 | /** |
| 179 | * Creates a ScaleGestureDetector with the supplied listener. |
| 180 | * You may only use this constructor from a {@link android.os.Looper Looper} thread. |
| 181 | * |
| 182 | * @param context the application's context |
| 183 | * @param listener the listener invoked for all the callbacks, this must |
| 184 | * not be null. |
| 185 | * |
| 186 | * @throws NullPointerException if {@code listener} is null. |
| 187 | */ |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 188 | public ScaleGestureDetector(Context context, OnScaleGestureListener listener) { |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 189 | this(context, listener, null); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Creates a ScaleGestureDetector with the supplied listener. |
| 194 | * @see android.os.Handler#Handler() |
| 195 | * |
| 196 | * @param context the application's context |
| 197 | * @param listener the listener invoked for all the callbacks, this must |
| 198 | * not be null. |
| 199 | * @param handler the handler to use for running deferred listener events. |
| 200 | * |
| 201 | * @throws NullPointerException if {@code listener} is null. |
| 202 | */ |
| 203 | public ScaleGestureDetector(Context context, OnScaleGestureListener listener, |
Mindy Pereira | 24870ce | 2013-09-09 15:56:23 -0700 | [diff] [blame] | 204 | Handler handler) { |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 205 | mContext = context; |
| 206 | mListener = listener; |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 207 | mSpanSlop = ViewConfiguration.get(context).getScaledTouchSlop() * 2; |
Adam Powell | 3307958 | 2012-10-09 11:20:39 -0700 | [diff] [blame] | 208 | |
| 209 | final Resources res = context.getResources(); |
| 210 | mTouchMinMajor = res.getDimensionPixelSize( |
| 211 | com.android.internal.R.dimen.config_minScalingTouchMajor); |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 212 | mMinSpan = res.getDimensionPixelSize(com.android.internal.R.dimen.config_minScalingSpan); |
| 213 | mHandler = handler; |
| 214 | // Quick scale is enabled by default after JB_MR2 |
Mady Mellor | 7c36a68 | 2015-04-22 11:52:31 -0700 | [diff] [blame] | 215 | final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion; |
| 216 | if (targetSdkVersion > Build.VERSION_CODES.JELLY_BEAN_MR2) { |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 217 | setQuickScaleEnabled(true); |
| 218 | } |
Mady Mellor | 7c36a68 | 2015-04-22 11:52:31 -0700 | [diff] [blame] | 219 | // Stylus scale is enabled by default after LOLLIPOP_MR1 |
| 220 | if (targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 221 | setStylusScaleEnabled(true); |
| 222 | } |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 225 | /** |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 226 | * The touchMajor/touchMinor elements of a MotionEvent can flutter/jitter on |
| 227 | * some hardware/driver combos. Smooth it out to get kinder, gentler behavior. |
| 228 | * @param ev MotionEvent to add to the ongoing history |
| 229 | */ |
| 230 | private void addTouchHistory(MotionEvent ev) { |
| 231 | final long currentTime = SystemClock.uptimeMillis(); |
| 232 | final int count = ev.getPointerCount(); |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 233 | boolean accept = currentTime - mTouchHistoryLastAcceptedTime >= TOUCH_STABILIZE_TIME; |
| 234 | float total = 0; |
| 235 | int sampleCount = 0; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 236 | for (int i = 0; i < count; i++) { |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 237 | final boolean hasLastAccepted = !Float.isNaN(mTouchHistoryLastAccepted); |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 238 | final int historySize = ev.getHistorySize(); |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 239 | final int pointerSampleCount = historySize + 1; |
| 240 | for (int h = 0; h < pointerSampleCount; h++) { |
| 241 | float major; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 242 | if (h < historySize) { |
| 243 | major = ev.getHistoricalTouchMajor(i, h); |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 244 | } else { |
| 245 | major = ev.getTouchMajor(i); |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 246 | } |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 247 | if (major < mTouchMinMajor) major = mTouchMinMajor; |
| 248 | total += major; |
| 249 | |
| 250 | if (Float.isNaN(mTouchUpper) || major > mTouchUpper) { |
| 251 | mTouchUpper = major; |
| 252 | } |
| 253 | if (Float.isNaN(mTouchLower) || major < mTouchLower) { |
| 254 | mTouchLower = major; |
| 255 | } |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 256 | |
| 257 | if (hasLastAccepted) { |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 258 | final int directionSig = (int) Math.signum(major - mTouchHistoryLastAccepted); |
| 259 | if (directionSig != mTouchHistoryDirection || |
| 260 | (directionSig == 0 && mTouchHistoryDirection == 0)) { |
| 261 | mTouchHistoryDirection = directionSig; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 262 | final long time = h < historySize ? ev.getHistoricalEventTime(h) |
| 263 | : ev.getEventTime(); |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 264 | mTouchHistoryLastAcceptedTime = time; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 265 | accept = false; |
| 266 | } |
| 267 | } |
| 268 | } |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 269 | sampleCount += pointerSampleCount; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 270 | } |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 271 | |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 272 | final float avg = total / sampleCount; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 273 | |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 274 | if (accept) { |
| 275 | float newAccepted = (mTouchUpper + mTouchLower + avg) / 3; |
| 276 | mTouchUpper = (mTouchUpper + newAccepted) / 2; |
| 277 | mTouchLower = (mTouchLower + newAccepted) / 2; |
| 278 | mTouchHistoryLastAccepted = newAccepted; |
| 279 | mTouchHistoryDirection = 0; |
| 280 | mTouchHistoryLastAcceptedTime = ev.getEventTime(); |
Adam Powell | f3a2bf8 | 2012-09-28 12:05:10 -0700 | [diff] [blame] | 281 | } |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Clear all touch history tracking. Useful in ACTION_CANCEL or ACTION_UP. |
| 286 | * @see #addTouchHistory(MotionEvent) |
| 287 | */ |
| 288 | private void clearTouchHistory() { |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 289 | mTouchUpper = Float.NaN; |
| 290 | mTouchLower = Float.NaN; |
| 291 | mTouchHistoryLastAccepted = Float.NaN; |
| 292 | mTouchHistoryDirection = 0; |
| 293 | mTouchHistoryLastAcceptedTime = 0; |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 297 | * Accepts MotionEvents and dispatches events to a {@link OnScaleGestureListener} |
| 298 | * when appropriate. |
| 299 | * |
| 300 | * <p>Applications should pass a complete and consistent event stream to this method. |
| 301 | * A complete and consistent event stream involves all MotionEvents from the initial |
| 302 | * ACTION_DOWN to the final ACTION_UP or ACTION_CANCEL.</p> |
| 303 | * |
| 304 | * @param event The event to process |
| 305 | * @return true if the event was processed and the detector wants to receive the |
| 306 | * rest of the MotionEvents in this event stream. |
| 307 | */ |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 308 | public boolean onTouchEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 309 | if (mInputEventConsistencyVerifier != null) { |
| 310 | mInputEventConsistencyVerifier.onTouchEvent(event, 0); |
| 311 | } |
| 312 | |
Adam Powell | 7232b0a | 2012-11-28 18:29:22 -0800 | [diff] [blame] | 313 | mCurrTime = event.getEventTime(); |
| 314 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 315 | final int action = event.getActionMasked(); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 316 | |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 317 | // Forward the event to check for double tap gesture |
Mindy Pereira | 9f1221f | 2013-09-16 11:36:12 -0700 | [diff] [blame] | 318 | if (mQuickScaleEnabled) { |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 319 | mGestureDetector.onTouchEvent(event); |
| 320 | } |
| 321 | |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 322 | final int count = event.getPointerCount(); |
Mady Mellor | 772fcb9 | 2015-05-21 17:46:58 -0700 | [diff] [blame] | 323 | final boolean isStylusButtonDown = |
| 324 | (event.getButtonState() & MotionEvent.BUTTON_STYLUS_PRIMARY) != 0; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 325 | |
| 326 | final boolean anchoredScaleCancelled = |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 327 | mAnchoredScaleMode == ANCHORED_SCALE_MODE_STYLUS && !isStylusButtonDown; |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 328 | final boolean streamComplete = action == MotionEvent.ACTION_UP || |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 329 | action == MotionEvent.ACTION_CANCEL || anchoredScaleCancelled; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 330 | |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 331 | if (action == MotionEvent.ACTION_DOWN || streamComplete) { |
| 332 | // Reset any scale in progress with the listener. |
| 333 | // If it's an ACTION_DOWN we're beginning a new event stream. |
| 334 | // This means the app probably didn't give us all the events. Shame on it. |
| 335 | if (mInProgress) { |
Adam Powell | d0197f3 | 2011-03-17 00:09:03 -0700 | [diff] [blame] | 336 | mListener.onScaleEnd(this); |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 337 | mInProgress = false; |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 338 | mInitialSpan = 0; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 339 | mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE; |
| 340 | } else if (inAnchoredScaleMode() && streamComplete) { |
Mindy Pereira | 5823352 | 2013-11-14 17:01:25 -0800 | [diff] [blame] | 341 | mInProgress = false; |
| 342 | mInitialSpan = 0; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 343 | mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE; |
Adam Powell | d0197f3 | 2011-03-17 00:09:03 -0700 | [diff] [blame] | 344 | } |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 345 | |
| 346 | if (streamComplete) { |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 347 | clearTouchHistory(); |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 348 | return true; |
| 349 | } |
Adam Powell | d0197f3 | 2011-03-17 00:09:03 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 352 | if (!mInProgress && mStylusScaleEnabled && !inAnchoredScaleMode() |
| 353 | && !streamComplete && isStylusButtonDown) { |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 354 | // Start of a button scale gesture |
| 355 | mAnchoredScaleStartX = event.getX(); |
| 356 | mAnchoredScaleStartY = event.getY(); |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 357 | mAnchoredScaleMode = ANCHORED_SCALE_MODE_STYLUS; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 358 | mInitialSpan = 0; |
| 359 | } |
| 360 | |
Adam Powell | abde042 | 2012-09-26 17:12:50 -0700 | [diff] [blame] | 361 | final boolean configChanged = action == MotionEvent.ACTION_DOWN || |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 362 | action == MotionEvent.ACTION_POINTER_UP || |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 363 | action == MotionEvent.ACTION_POINTER_DOWN || anchoredScaleCancelled; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 364 | |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 365 | final boolean pointerUp = action == MotionEvent.ACTION_POINTER_UP; |
| 366 | final int skipIndex = pointerUp ? event.getActionIndex() : -1; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 367 | |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 368 | // Determine focal point |
| 369 | float sumX = 0, sumY = 0; |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 370 | final int div = pointerUp ? count - 1 : count; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 371 | final float focusX; |
| 372 | final float focusY; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 373 | if (inAnchoredScaleMode()) { |
| 374 | // In anchored scale mode, the focal pt is always where the double tap |
| 375 | // or button down gesture started |
| 376 | focusX = mAnchoredScaleStartX; |
| 377 | focusY = mAnchoredScaleStartY; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 378 | if (event.getY() < focusY) { |
| 379 | mEventBeforeOrAboveStartingGestureEvent = true; |
| 380 | } else { |
| 381 | mEventBeforeOrAboveStartingGestureEvent = false; |
| 382 | } |
| 383 | } else { |
| 384 | for (int i = 0; i < count; i++) { |
| 385 | if (skipIndex == i) continue; |
| 386 | sumX += event.getX(i); |
| 387 | sumY += event.getY(i); |
| 388 | } |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 389 | |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 390 | focusX = sumX / div; |
| 391 | focusY = sumY / div; |
| 392 | } |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 393 | |
| 394 | addTouchHistory(event); |
Adam Powell | a4ce6ae | 2012-09-24 20:13:23 -0700 | [diff] [blame] | 395 | |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 396 | // Determine average deviation from focal point |
| 397 | float devSumX = 0, devSumY = 0; |
| 398 | for (int i = 0; i < count; i++) { |
| 399 | if (skipIndex == i) continue; |
Adam Powell | 828e56e | 2012-09-14 18:56:16 -0700 | [diff] [blame] | 400 | |
Adam Powell | 5b5c414 | 2012-10-02 16:25:30 -0700 | [diff] [blame] | 401 | // Convert the resulting diameter into a radius. |
| 402 | final float touchSize = mTouchHistoryLastAccepted / 2; |
Adam Powell | 828e56e | 2012-09-14 18:56:16 -0700 | [diff] [blame] | 403 | devSumX += Math.abs(event.getX(i) - focusX) + touchSize; |
| 404 | devSumY += Math.abs(event.getY(i) - focusY) + touchSize; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 405 | } |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 406 | final float devX = devSumX / div; |
| 407 | final float devY = devSumY / div; |
| 408 | |
| 409 | // Span is the average distance between touch points through the focal point; |
| 410 | // i.e. the diameter of the circle with a radius of the average deviation from |
| 411 | // the focal point. |
| 412 | final float spanX = devX * 2; |
| 413 | final float spanY = devY * 2; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 414 | final float span; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 415 | if (inAnchoredScaleMode()) { |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 416 | span = spanY; |
| 417 | } else { |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 418 | span = (float) Math.hypot(spanX, spanY); |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 419 | } |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 420 | |
| 421 | // Dispatch begin/end events as needed. |
| 422 | // If the configuration changes, notify the app to reset its current state by beginning |
| 423 | // a fresh scale event stream. |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 424 | final boolean wasInProgress = mInProgress; |
| 425 | mFocusX = focusX; |
| 426 | mFocusY = focusY; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 427 | if (!inAnchoredScaleMode() && mInProgress && (span < mMinSpan || configChanged)) { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 428 | mListener.onScaleEnd(this); |
| 429 | mInProgress = false; |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 430 | mInitialSpan = span; |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 431 | } |
| 432 | if (configChanged) { |
| 433 | mPrevSpanX = mCurrSpanX = spanX; |
| 434 | mPrevSpanY = mCurrSpanY = spanY; |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 435 | mInitialSpan = mPrevSpan = mCurrSpan = span; |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 436 | } |
Mindy Pereira | 24870ce | 2013-09-09 15:56:23 -0700 | [diff] [blame] | 437 | |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 438 | final int minSpan = inAnchoredScaleMode() ? mSpanSlop : mMinSpan; |
Mindy Pereira | 24870ce | 2013-09-09 15:56:23 -0700 | [diff] [blame] | 439 | if (!mInProgress && span >= minSpan && |
Adam Powell | 47ec2fb | 2012-08-31 17:15:32 -0700 | [diff] [blame] | 440 | (wasInProgress || Math.abs(span - mInitialSpan) > mSpanSlop)) { |
| 441 | mPrevSpanX = mCurrSpanX = spanX; |
| 442 | mPrevSpanY = mCurrSpanY = spanY; |
| 443 | mPrevSpan = mCurrSpan = span; |
Adam Powell | 7232b0a | 2012-11-28 18:29:22 -0800 | [diff] [blame] | 444 | mPrevTime = mCurrTime; |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 445 | mInProgress = mListener.onScaleBegin(this); |
| 446 | } |
| 447 | |
| 448 | // Handle motion; focal point and span/scale factor are changing. |
| 449 | if (action == MotionEvent.ACTION_MOVE) { |
| 450 | mCurrSpanX = spanX; |
| 451 | mCurrSpanY = spanY; |
| 452 | mCurrSpan = span; |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 453 | |
| 454 | boolean updatePrev = true; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 455 | |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 456 | if (mInProgress) { |
| 457 | updatePrev = mListener.onScale(this); |
| 458 | } |
| 459 | |
| 460 | if (updatePrev) { |
| 461 | mPrevSpanX = mCurrSpanX; |
| 462 | mPrevSpanY = mCurrSpanY; |
| 463 | mPrevSpan = mCurrSpan; |
Adam Powell | 7232b0a | 2012-11-28 18:29:22 -0800 | [diff] [blame] | 464 | mPrevTime = mCurrTime; |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | return true; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 471 | private boolean inAnchoredScaleMode() { |
| 472 | return mAnchoredScaleMode != ANCHORED_SCALE_MODE_NONE; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Set whether the associated {@link OnScaleGestureListener} should receive onScale callbacks |
| 477 | * when the user performs a doubleTap followed by a swipe. Note that this is enabled by default |
| 478 | * if the app targets API 19 and newer. |
| 479 | * @param scales true to enable quick scaling, false to disable |
| 480 | */ |
| 481 | public void setQuickScaleEnabled(boolean scales) { |
Mindy Pereira | 9f1221f | 2013-09-16 11:36:12 -0700 | [diff] [blame] | 482 | mQuickScaleEnabled = scales; |
| 483 | if (mQuickScaleEnabled && mGestureDetector == null) { |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 484 | GestureDetector.SimpleOnGestureListener gestureListener = |
| 485 | new GestureDetector.SimpleOnGestureListener() { |
| 486 | @Override |
| 487 | public boolean onDoubleTap(MotionEvent e) { |
| 488 | // Double tap: start watching for a swipe |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 489 | mAnchoredScaleStartX = e.getX(); |
| 490 | mAnchoredScaleStartY = e.getY(); |
| 491 | mAnchoredScaleMode = ANCHORED_SCALE_MODE_DOUBLE_TAP; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 492 | return true; |
| 493 | } |
Mindy Pereira | 24870ce | 2013-09-09 15:56:23 -0700 | [diff] [blame] | 494 | }; |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 495 | mGestureDetector = new GestureDetector(mContext, gestureListener, mHandler); |
| 496 | } |
| 497 | } |
| 498 | |
Mindy Pereira | 9f1221f | 2013-09-16 11:36:12 -0700 | [diff] [blame] | 499 | /** |
| 500 | * Return whether the quick scale gesture, in which the user performs a double tap followed by a |
| 501 | * swipe, should perform scaling. {@see #setQuickScaleEnabled(boolean)}. |
| 502 | */ |
| 503 | public boolean isQuickScaleEnabled() { |
| 504 | return mQuickScaleEnabled; |
| 505 | } |
| 506 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 507 | /** |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 508 | * Sets whether the associates {@link OnScaleGestureListener} should receive |
| 509 | * onScale callbacks when the user uses a stylus and presses the button. |
| 510 | * Note that this is enabled by default if the app targets API 23 and newer. |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 511 | * |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 512 | * @param scales true to enable stylus scaling, false to disable. |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 513 | */ |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 514 | public void setStylusScaleEnabled(boolean scales) { |
| 515 | mStylusScaleEnabled = scales; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
Mady Mellor | d9ff4df | 2015-06-04 18:03:49 -0700 | [diff] [blame] | 519 | * Return whether the stylus scale gesture, in which the user uses a stylus and presses the |
| 520 | * button, should perform scaling. {@see #setStylusScaleEnabled(boolean)} |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 521 | */ |
Mady Mellor | 6895518 | 2015-04-16 12:50:53 -0700 | [diff] [blame] | 522 | public boolean isStylusScaleEnabled() { |
| 523 | return mStylusScaleEnabled; |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 527 | * Returns {@code true} if a scale gesture is in progress. |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 528 | */ |
| 529 | public boolean isInProgress() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 530 | return mInProgress; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Get the X coordinate of the current gesture's focal point. |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 535 | * If a gesture is in progress, the focal point is between |
| 536 | * each of the pointers forming the gesture. |
| 537 | * |
Adam Powell | ab905c87 | 2010-02-03 11:01:58 -0800 | [diff] [blame] | 538 | * If {@link #isInProgress()} would return false, the result of this |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 539 | * function is undefined. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 540 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 541 | * @return X coordinate of the focal point in pixels. |
| 542 | */ |
| 543 | public float getFocusX() { |
| 544 | return mFocusX; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Get the Y coordinate of the current gesture's focal point. |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 549 | * If a gesture is in progress, the focal point is between |
| 550 | * each of the pointers forming the gesture. |
| 551 | * |
Adam Powell | ab905c87 | 2010-02-03 11:01:58 -0800 | [diff] [blame] | 552 | * If {@link #isInProgress()} would return false, the result of this |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 553 | * function is undefined. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 554 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 555 | * @return Y coordinate of the focal point in pixels. |
| 556 | */ |
| 557 | public float getFocusY() { |
| 558 | return mFocusY; |
| 559 | } |
| 560 | |
| 561 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 562 | * Return the average distance between each of the pointers forming the |
| 563 | * gesture in progress through the focal point. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 564 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 565 | * @return Distance between pointers in pixels. |
| 566 | */ |
| 567 | public float getCurrentSpan() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 568 | return mCurrSpan; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 572 | * Return the average X distance between each of the pointers forming the |
| 573 | * gesture in progress through the focal point. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 574 | * |
| 575 | * @return Distance between pointers in pixels. |
| 576 | */ |
| 577 | public float getCurrentSpanX() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 578 | return mCurrSpanX; |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 582 | * Return the average Y distance between each of the pointers forming the |
| 583 | * gesture in progress through the focal point. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 584 | * |
| 585 | * @return Distance between pointers in pixels. |
| 586 | */ |
| 587 | public float getCurrentSpanY() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 588 | return mCurrSpanY; |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 592 | * Return the previous average distance between each of the pointers forming the |
| 593 | * gesture in progress through the focal point. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 594 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 595 | * @return Previous distance between pointers in pixels. |
| 596 | */ |
| 597 | public float getPreviousSpan() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 598 | return mPrevSpan; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 602 | * Return the previous average X distance between each of the pointers forming the |
| 603 | * gesture in progress through the focal point. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 604 | * |
| 605 | * @return Previous distance between pointers in pixels. |
| 606 | */ |
| 607 | public float getPreviousSpanX() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 608 | return mPrevSpanX; |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /** |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 612 | * Return the previous average Y distance between each of the pointers forming the |
| 613 | * gesture in progress through the focal point. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 614 | * |
| 615 | * @return Previous distance between pointers in pixels. |
| 616 | */ |
| 617 | public float getPreviousSpanY() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 618 | return mPrevSpanY; |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /** |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 622 | * Return the scaling factor from the previous scale event to the current |
| 623 | * event. This value is defined as |
Adam Powell | ab905c87 | 2010-02-03 11:01:58 -0800 | [diff] [blame] | 624 | * ({@link #getCurrentSpan()} / {@link #getPreviousSpan()}). |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 625 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 626 | * @return The current scaling factor. |
| 627 | */ |
| 628 | public float getScaleFactor() { |
Mady Mellor | 847d17f | 2015-03-30 09:42:12 -0700 | [diff] [blame] | 629 | if (inAnchoredScaleMode()) { |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 630 | // Drag is moving up; the further away from the gesture |
| 631 | // start, the smaller the span should be, the closer, |
| 632 | // the larger the span, and therefore the larger the scale |
Mindy Pereira | 24870ce | 2013-09-09 15:56:23 -0700 | [diff] [blame] | 633 | final boolean scaleUp = |
| 634 | (mEventBeforeOrAboveStartingGestureEvent && (mCurrSpan < mPrevSpan)) || |
| 635 | (!mEventBeforeOrAboveStartingGestureEvent && (mCurrSpan > mPrevSpan)); |
| 636 | final float spanDiff = (Math.abs(1 - (mCurrSpan / mPrevSpan)) * SCALE_FACTOR); |
| 637 | return mPrevSpan <= 0 ? 1 : scaleUp ? (1 + spanDiff) : (1 - spanDiff); |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 638 | } |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 639 | return mPrevSpan > 0 ? mCurrSpan / mPrevSpan : 1; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 640 | } |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 641 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 642 | /** |
| 643 | * Return the time difference in milliseconds between the previous |
| 644 | * accepted scaling event and the current scaling event. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 645 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 646 | * @return Time difference since the last scaling event in milliseconds. |
| 647 | */ |
| 648 | public long getTimeDelta() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 649 | return mCurrTime - mPrevTime; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 650 | } |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 651 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 652 | /** |
| 653 | * Return the event time of the current event being processed. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 654 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 655 | * @return Current event time in milliseconds. |
| 656 | */ |
| 657 | public long getEventTime() { |
Adam Powell | 618cbea | 2012-08-27 17:44:59 -0700 | [diff] [blame] | 658 | return mCurrTime; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 659 | } |
Mindy Pereira | e8ce8ba | 2013-08-21 15:59:36 -0700 | [diff] [blame] | 660 | } |