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 | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 20 | import android.util.DisplayMetrics; |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 21 | import android.util.FloatMath; |
Adam Powell | 0818020 | 2011-03-07 16:48:29 -0800 | [diff] [blame] | 22 | import android.util.Log; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Detects transformation gestures involving more than one pointer ("multitouch") |
| 26 | * using the supplied {@link MotionEvent}s. The {@link OnScaleGestureListener} |
| 27 | * callback will notify users when a particular gesture event has occurred. |
| 28 | * This class should only be used with {@link MotionEvent}s reported via touch. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 29 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 30 | * To use this class: |
| 31 | * <ul> |
| 32 | * <li>Create an instance of the {@code ScaleGestureDetector} for your |
| 33 | * {@link View} |
| 34 | * <li>In the {@link View#onTouchEvent(MotionEvent)} method ensure you call |
| 35 | * {@link #onTouchEvent(MotionEvent)}. The methods defined in your |
| 36 | * callback will be executed when the events occur. |
| 37 | * </ul> |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 38 | */ |
| 39 | public class ScaleGestureDetector { |
Adam Powell | 0818020 | 2011-03-07 16:48:29 -0800 | [diff] [blame] | 40 | private static final String TAG = "ScaleGestureDetector"; |
| 41 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 42 | /** |
| 43 | * The listener for receiving notifications when gestures occur. |
| 44 | * If you want to listen for all the different gestures then implement |
| 45 | * this interface. If you only want to listen for a subset it might |
| 46 | * be easier to extend {@link SimpleOnScaleGestureListener}. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 47 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 48 | * An application will receive events in the following order: |
| 49 | * <ul> |
Adam Powell | ab905c87 | 2010-02-03 11:01:58 -0800 | [diff] [blame] | 50 | * <li>One {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)} |
| 51 | * <li>Zero or more {@link OnScaleGestureListener#onScale(ScaleGestureDetector)} |
| 52 | * <li>One {@link OnScaleGestureListener#onScaleEnd(ScaleGestureDetector)} |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 53 | * </ul> |
| 54 | */ |
| 55 | public interface OnScaleGestureListener { |
| 56 | /** |
| 57 | * Responds to scaling events for a gesture in progress. |
| 58 | * Reported by pointer motion. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 59 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 60 | * @param detector The detector reporting the event - use this to |
| 61 | * retrieve extended info about event state. |
| 62 | * @return Whether or not the detector should consider this event |
| 63 | * as handled. If an event was not handled, the detector |
| 64 | * will continue to accumulate movement until an event is |
| 65 | * handled. This can be useful if an application, for example, |
| 66 | * only wants to update scaling factors if the change is |
| 67 | * greater than 0.01. |
| 68 | */ |
| 69 | public boolean onScale(ScaleGestureDetector detector); |
| 70 | |
| 71 | /** |
| 72 | * Responds to the beginning of a scaling gesture. Reported by |
| 73 | * new pointers going down. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 74 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 75 | * @param detector The detector reporting the event - use this to |
| 76 | * retrieve extended info about event state. |
| 77 | * @return Whether or not the detector should continue recognizing |
| 78 | * this gesture. For example, if a gesture is beginning |
| 79 | * with a focal point outside of a region where it makes |
| 80 | * sense, onScaleBegin() may return false to ignore the |
| 81 | * rest of the gesture. |
| 82 | */ |
| 83 | public boolean onScaleBegin(ScaleGestureDetector detector); |
| 84 | |
| 85 | /** |
| 86 | * Responds to the end of a scale gesture. Reported by existing |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 87 | * pointers going up. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 88 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 89 | * Once a scale has ended, {@link ScaleGestureDetector#getFocusX()} |
| 90 | * and {@link ScaleGestureDetector#getFocusY()} will return the location |
| 91 | * of the pointer remaining on the screen. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 92 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 93 | * @param detector The detector reporting the event - use this to |
| 94 | * retrieve extended info about event state. |
| 95 | */ |
| 96 | public void onScaleEnd(ScaleGestureDetector detector); |
| 97 | } |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 98 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 99 | /** |
| 100 | * A convenience class to extend when you only want to listen for a subset |
| 101 | * of scaling-related events. This implements all methods in |
| 102 | * {@link OnScaleGestureListener} but does nothing. |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 103 | * {@link OnScaleGestureListener#onScale(ScaleGestureDetector)} returns |
| 104 | * {@code false} so that a subclass can retrieve the accumulated scale |
| 105 | * factor in an overridden onScaleEnd. |
| 106 | * {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)} returns |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 107 | * {@code true}. |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 108 | */ |
Adam Powell | 216bccf | 2010-02-01 15:03:17 -0800 | [diff] [blame] | 109 | public static class SimpleOnScaleGestureListener implements OnScaleGestureListener { |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 110 | |
| 111 | public boolean onScale(ScaleGestureDetector detector) { |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 112 | return false; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public boolean onScaleBegin(ScaleGestureDetector detector) { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | public void onScaleEnd(ScaleGestureDetector detector) { |
| 120 | // Intentionally empty |
| 121 | } |
| 122 | } |
| 123 | |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 124 | /** |
| 125 | * This value is the threshold ratio between our previous combined pressure |
| 126 | * and the current combined pressure. We will only fire an onScale event if |
| 127 | * the computed ratio between the current and previous event pressures is |
| 128 | * greater than this value. When pressure decreases rapidly between events |
| 129 | * the position values can often be imprecise, as it usually indicates |
| 130 | * that the user is in the process of lifting a pointer off of the device. |
| 131 | * Its value was tuned experimentally. |
| 132 | */ |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 133 | private static final float PRESSURE_THRESHOLD = 0.67f; |
| 134 | |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 135 | private final Context mContext; |
| 136 | private final OnScaleGestureListener mListener; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 137 | private boolean mGestureInProgress; |
| 138 | |
| 139 | private MotionEvent mPrevEvent; |
| 140 | private MotionEvent mCurrEvent; |
| 141 | |
| 142 | private float mFocusX; |
| 143 | private float mFocusY; |
| 144 | private float mPrevFingerDiffX; |
| 145 | private float mPrevFingerDiffY; |
| 146 | private float mCurrFingerDiffX; |
| 147 | private float mCurrFingerDiffY; |
| 148 | private float mCurrLen; |
| 149 | private float mPrevLen; |
| 150 | private float mScaleFactor; |
| 151 | private float mCurrPressure; |
| 152 | private float mPrevPressure; |
| 153 | private long mTimeDelta; |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 154 | |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 155 | private final float mEdgeSlop; |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 156 | private float mRightSlopEdge; |
| 157 | private float mBottomSlopEdge; |
| 158 | private boolean mSloppyGesture; |
Adam Powell | d0197f3 | 2011-03-17 00:09:03 -0700 | [diff] [blame] | 159 | private boolean mInvalidGesture; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 160 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 161 | // Pointer IDs currently responsible for the two fingers controlling the gesture |
| 162 | private int mActiveId0; |
| 163 | private int mActiveId1; |
| 164 | private boolean mActive0MostRecent; |
| 165 | |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 166 | /** |
| 167 | * Consistency verifier for debugging purposes. |
| 168 | */ |
| 169 | private final InputEventConsistencyVerifier mInputEventConsistencyVerifier = |
| 170 | InputEventConsistencyVerifier.isInstrumentationEnabled() ? |
| 171 | new InputEventConsistencyVerifier(this, 0) : null; |
| 172 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 173 | public ScaleGestureDetector(Context context, OnScaleGestureListener listener) { |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 174 | ViewConfiguration config = ViewConfiguration.get(context); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 175 | mContext = context; |
| 176 | mListener = listener; |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 177 | mEdgeSlop = config.getScaledEdgeSlop(); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | public boolean onTouchEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 181 | if (mInputEventConsistencyVerifier != null) { |
| 182 | mInputEventConsistencyVerifier.onTouchEvent(event, 0); |
| 183 | } |
| 184 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 185 | final int action = event.getActionMasked(); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 186 | |
Adam Powell | 0818020 | 2011-03-07 16:48:29 -0800 | [diff] [blame] | 187 | if (action == MotionEvent.ACTION_DOWN) { |
| 188 | reset(); // Start fresh |
| 189 | } |
| 190 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 191 | boolean handled = true; |
| 192 | if (mInvalidGesture) { |
| 193 | handled = false; |
| 194 | } else if (!mGestureInProgress) { |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 195 | switch (action) { |
| 196 | case MotionEvent.ACTION_DOWN: { |
| 197 | mActiveId0 = event.getPointerId(0); |
| 198 | mActive0MostRecent = true; |
| 199 | } |
| 200 | break; |
| 201 | |
| 202 | case MotionEvent.ACTION_UP: |
| 203 | reset(); |
| 204 | break; |
| 205 | |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 206 | case MotionEvent.ACTION_POINTER_DOWN: { |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 207 | // We have a new multi-finger gesture |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 208 | |
Grace Kloba | d5ada83 | 2010-01-20 23:06:43 -0800 | [diff] [blame] | 209 | // as orientation can change, query the metrics in touch down |
| 210 | DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); |
| 211 | mRightSlopEdge = metrics.widthPixels - mEdgeSlop; |
| 212 | mBottomSlopEdge = metrics.heightPixels - mEdgeSlop; |
| 213 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 214 | if (mPrevEvent != null) mPrevEvent.recycle(); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 215 | mPrevEvent = MotionEvent.obtain(event); |
| 216 | mTimeDelta = 0; |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 217 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 218 | int index1 = event.getActionIndex(); |
| 219 | int index0 = event.findPointerIndex(mActiveId0); |
| 220 | mActiveId1 = event.getPointerId(index1); |
Adam Powell | 0818020 | 2011-03-07 16:48:29 -0800 | [diff] [blame] | 221 | if (index0 < 0 || index0 == index1) { |
| 222 | // Probably someone sending us a broken event stream. |
| 223 | index0 = findNewActiveIndex(event, index0 == index1 ? -1 : mActiveId1, index0); |
| 224 | mActiveId0 = event.getPointerId(index0); |
| 225 | } |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 226 | mActive0MostRecent = false; |
| 227 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 228 | setContext(event); |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 229 | |
| 230 | // Check if we have a sloppy gesture. If so, delay |
| 231 | // the beginning of the gesture until we're sure that's |
| 232 | // what the user wanted. Sloppy gestures can happen if the |
| 233 | // edge of the user's hand is touching the screen, for example. |
| 234 | final float edgeSlop = mEdgeSlop; |
| 235 | final float rightSlop = mRightSlopEdge; |
| 236 | final float bottomSlop = mBottomSlopEdge; |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 237 | float x0 = getRawX(event, index0); |
| 238 | float y0 = getRawY(event, index0); |
| 239 | float x1 = getRawX(event, index1); |
| 240 | float y1 = getRawY(event, index1); |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 241 | |
Grace Kloba | 8f9fbb0 | 2010-01-20 14:10:31 -0800 | [diff] [blame] | 242 | boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop |
| 243 | || x0 > rightSlop || y0 > bottomSlop; |
| 244 | boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop |
| 245 | || x1 > rightSlop || y1 > bottomSlop; |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 246 | |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 247 | if (p0sloppy && p1sloppy) { |
Grace Kloba | 8f9fbb0 | 2010-01-20 14:10:31 -0800 | [diff] [blame] | 248 | mFocusX = -1; |
| 249 | mFocusY = -1; |
| 250 | mSloppyGesture = true; |
| 251 | } else if (p0sloppy) { |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 252 | mFocusX = event.getX(index1); |
| 253 | mFocusY = event.getY(index1); |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 254 | mSloppyGesture = true; |
| 255 | } else if (p1sloppy) { |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 256 | mFocusX = event.getX(index0); |
| 257 | mFocusY = event.getY(index0); |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 258 | mSloppyGesture = true; |
| 259 | } else { |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 260 | mSloppyGesture = false; |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 261 | mGestureInProgress = mListener.onScaleBegin(this); |
| 262 | } |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 263 | } |
| 264 | break; |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 265 | |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 266 | case MotionEvent.ACTION_MOVE: |
| 267 | if (mSloppyGesture) { |
| 268 | // Initiate sloppy gestures if we've moved outside of the slop area. |
| 269 | final float edgeSlop = mEdgeSlop; |
| 270 | final float rightSlop = mRightSlopEdge; |
| 271 | final float bottomSlop = mBottomSlopEdge; |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 272 | int index0 = event.findPointerIndex(mActiveId0); |
| 273 | int index1 = event.findPointerIndex(mActiveId1); |
Adam Powell | 0fe4a13 | 2011-02-28 13:32:17 -0800 | [diff] [blame] | 274 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 275 | float x0 = getRawX(event, index0); |
| 276 | float y0 = getRawY(event, index0); |
| 277 | float x1 = getRawX(event, index1); |
| 278 | float y1 = getRawY(event, index1); |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 279 | |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 280 | boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 281 | || x0 > rightSlop || y0 > bottomSlop; |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 282 | boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 283 | || x1 > rightSlop || y1 > bottomSlop; |
| 284 | |
| 285 | if (p0sloppy) { |
| 286 | // Do we have a different pointer that isn't sloppy? |
| 287 | int index = findNewActiveIndex(event, mActiveId1, index0); |
| 288 | if (index >= 0) { |
| 289 | index0 = index; |
| 290 | mActiveId0 = event.getPointerId(index); |
| 291 | x0 = getRawX(event, index); |
| 292 | y0 = getRawY(event, index); |
| 293 | p0sloppy = false; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (p1sloppy) { |
| 298 | // Do we have a different pointer that isn't sloppy? |
| 299 | int index = findNewActiveIndex(event, mActiveId0, index1); |
| 300 | if (index >= 0) { |
| 301 | index1 = index; |
| 302 | mActiveId1 = event.getPointerId(index); |
| 303 | x1 = getRawX(event, index); |
| 304 | y1 = getRawY(event, index); |
| 305 | p1sloppy = false; |
| 306 | } |
| 307 | } |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 308 | |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 309 | if(p0sloppy && p1sloppy) { |
| 310 | mFocusX = -1; |
| 311 | mFocusY = -1; |
| 312 | } else if (p0sloppy) { |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 313 | mFocusX = event.getX(index1); |
| 314 | mFocusY = event.getY(index1); |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 315 | } else if (p1sloppy) { |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 316 | mFocusX = event.getX(index0); |
| 317 | mFocusY = event.getY(index0); |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 318 | } else { |
| 319 | mSloppyGesture = false; |
| 320 | mGestureInProgress = mListener.onScaleBegin(this); |
| 321 | } |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 322 | } |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 323 | break; |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 324 | |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 325 | case MotionEvent.ACTION_POINTER_UP: |
| 326 | if (mSloppyGesture) { |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 327 | final int pointerCount = event.getPointerCount(); |
| 328 | final int actionIndex = event.getActionIndex(); |
| 329 | final int actionId = event.getPointerId(actionIndex); |
| 330 | |
| 331 | if (pointerCount > 2) { |
| 332 | if (actionId == mActiveId0) { |
| 333 | final int newIndex = findNewActiveIndex(event, mActiveId1, actionIndex); |
| 334 | if (newIndex >= 0) mActiveId0 = event.getPointerId(newIndex); |
| 335 | } else if (actionId == mActiveId1) { |
| 336 | final int newIndex = findNewActiveIndex(event, mActiveId0, actionIndex); |
| 337 | if (newIndex >= 0) mActiveId1 = event.getPointerId(newIndex); |
| 338 | } |
| 339 | } else { |
| 340 | // Set focus point to the remaining finger |
| 341 | final int index = event.findPointerIndex(actionId == mActiveId0 ? |
| 342 | mActiveId1 : mActiveId0); |
| 343 | mActiveId0 = event.getPointerId(index); |
Adam Powell | 0818020 | 2011-03-07 16:48:29 -0800 | [diff] [blame] | 344 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 345 | mActive0MostRecent = true; |
| 346 | mActiveId1 = -1; |
| 347 | mFocusX = event.getX(index); |
| 348 | mFocusY = event.getY(index); |
| 349 | } |
Adam Powell | f5bcc6a | 2010-03-02 10:42:16 -0800 | [diff] [blame] | 350 | } |
| 351 | break; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 352 | } |
| 353 | } else { |
| 354 | // Transform gesture in progress - attempt to handle it |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 355 | switch (action) { |
| 356 | case MotionEvent.ACTION_POINTER_DOWN: { |
| 357 | // End the old gesture and begin a new one with the most recent two fingers. |
| 358 | mListener.onScaleEnd(this); |
| 359 | final int oldActive0 = mActiveId0; |
| 360 | final int oldActive1 = mActiveId1; |
| 361 | reset(); |
| 362 | |
| 363 | mPrevEvent = MotionEvent.obtain(event); |
| 364 | mActiveId0 = mActive0MostRecent ? oldActive0 : oldActive1; |
| 365 | mActiveId1 = event.getPointerId(event.getActionIndex()); |
| 366 | mActive0MostRecent = false; |
| 367 | |
Adam Powell | 0818020 | 2011-03-07 16:48:29 -0800 | [diff] [blame] | 368 | int index0 = event.findPointerIndex(mActiveId0); |
| 369 | if (index0 < 0 || mActiveId0 == mActiveId1) { |
| 370 | // Probably someone sending us a broken event stream. |
| 371 | Log.e(TAG, "Got " + MotionEvent.actionToString(action) + |
| 372 | " with bad state while a gesture was in progress. " + |
| 373 | "Did you forget to pass an event to " + |
| 374 | "ScaleGestureDetector#onTouchEvent?"); |
| 375 | index0 = findNewActiveIndex(event, |
| 376 | mActiveId0 == mActiveId1 ? -1 : mActiveId1, index0); |
| 377 | mActiveId0 = event.getPointerId(index0); |
| 378 | } |
| 379 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 380 | setContext(event); |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 381 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 382 | mGestureInProgress = mListener.onScaleBegin(this); |
| 383 | } |
| 384 | break; |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 385 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 386 | case MotionEvent.ACTION_POINTER_UP: { |
| 387 | final int pointerCount = event.getPointerCount(); |
| 388 | final int actionIndex = event.getActionIndex(); |
| 389 | final int actionId = event.getPointerId(actionIndex); |
| 390 | |
| 391 | boolean gestureEnded = false; |
| 392 | if (pointerCount > 2) { |
| 393 | if (actionId == mActiveId0) { |
| 394 | final int newIndex = findNewActiveIndex(event, mActiveId1, actionIndex); |
| 395 | if (newIndex >= 0) { |
Adam Powell | 0fe4a13 | 2011-02-28 13:32:17 -0800 | [diff] [blame] | 396 | mListener.onScaleEnd(this); |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 397 | mActiveId0 = event.getPointerId(newIndex); |
Adam Powell | 0fe4a13 | 2011-02-28 13:32:17 -0800 | [diff] [blame] | 398 | mActive0MostRecent = true; |
| 399 | mPrevEvent = MotionEvent.obtain(event); |
| 400 | setContext(event); |
| 401 | mGestureInProgress = mListener.onScaleBegin(this); |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 402 | } else { |
| 403 | gestureEnded = true; |
| 404 | } |
| 405 | } else if (actionId == mActiveId1) { |
| 406 | final int newIndex = findNewActiveIndex(event, mActiveId0, actionIndex); |
| 407 | if (newIndex >= 0) { |
Adam Powell | 0fe4a13 | 2011-02-28 13:32:17 -0800 | [diff] [blame] | 408 | mListener.onScaleEnd(this); |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 409 | mActiveId1 = event.getPointerId(newIndex); |
Adam Powell | 0fe4a13 | 2011-02-28 13:32:17 -0800 | [diff] [blame] | 410 | mActive0MostRecent = false; |
| 411 | mPrevEvent = MotionEvent.obtain(event); |
| 412 | setContext(event); |
| 413 | mGestureInProgress = mListener.onScaleBegin(this); |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 414 | } else { |
| 415 | gestureEnded = true; |
| 416 | } |
| 417 | } |
| 418 | mPrevEvent.recycle(); |
| 419 | mPrevEvent = MotionEvent.obtain(event); |
| 420 | setContext(event); |
| 421 | } else { |
| 422 | gestureEnded = true; |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 423 | } |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 424 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 425 | if (gestureEnded) { |
| 426 | // Gesture ended |
| 427 | setContext(event); |
| 428 | |
| 429 | // Set focus point to the remaining finger |
| 430 | final int activeId = actionId == mActiveId0 ? mActiveId1 : mActiveId0; |
| 431 | final int index = event.findPointerIndex(activeId); |
| 432 | mFocusX = event.getX(index); |
| 433 | mFocusY = event.getY(index); |
| 434 | |
| 435 | mListener.onScaleEnd(this); |
| 436 | reset(); |
| 437 | mActiveId0 = activeId; |
| 438 | mActive0MostRecent = true; |
| 439 | } |
| 440 | } |
| 441 | break; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 442 | |
| 443 | case MotionEvent.ACTION_CANCEL: |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 444 | mListener.onScaleEnd(this); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 445 | reset(); |
| 446 | break; |
| 447 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 448 | case MotionEvent.ACTION_UP: |
| 449 | reset(); |
| 450 | break; |
| 451 | |
| 452 | case MotionEvent.ACTION_MOVE: { |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 453 | setContext(event); |
| 454 | |
| 455 | // Only accept the event if our relative pressure is within |
| 456 | // a certain limit - this can help filter shaky data as a |
| 457 | // finger is lifted. |
| 458 | if (mCurrPressure / mPrevPressure > PRESSURE_THRESHOLD) { |
| 459 | final boolean updatePrevious = mListener.onScale(this); |
| 460 | |
| 461 | if (updatePrevious) { |
| 462 | mPrevEvent.recycle(); |
| 463 | mPrevEvent = MotionEvent.obtain(event); |
| 464 | } |
| 465 | } |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 466 | } |
| 467 | break; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 470 | |
| 471 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 472 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 0); |
| 473 | } |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 474 | return handled; |
| 475 | } |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 476 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 477 | private int findNewActiveIndex(MotionEvent ev, int otherActiveId, int oldIndex) { |
| 478 | final int pointerCount = ev.getPointerCount(); |
| 479 | |
| 480 | // It's ok if this isn't found and returns -1, it simply won't match. |
| 481 | final int otherActiveIndex = ev.findPointerIndex(otherActiveId); |
| 482 | int newActiveIndex = -1; |
| 483 | |
| 484 | // Pick a new id and update tracking state. Only pick pointers not on the slop edges. |
| 485 | for (int i = 0; i < pointerCount; i++) { |
| 486 | if (i != oldIndex && i != otherActiveIndex) { |
| 487 | final float edgeSlop = mEdgeSlop; |
| 488 | final float rightSlop = mRightSlopEdge; |
| 489 | final float bottomSlop = mBottomSlopEdge; |
| 490 | float x = getRawX(ev, i); |
| 491 | float y = getRawY(ev, i); |
| 492 | if (x >= edgeSlop && y >= edgeSlop && x <= rightSlop && y <= bottomSlop) { |
| 493 | newActiveIndex = i; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return newActiveIndex; |
| 500 | } |
| 501 | |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 502 | /** |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 503 | * MotionEvent has no getRawX(int) method; simulate it pending future API approval. |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 504 | */ |
| 505 | private static float getRawX(MotionEvent event, int pointerIndex) { |
Adam Powell | 0fe4a13 | 2011-02-28 13:32:17 -0800 | [diff] [blame] | 506 | if (pointerIndex < 0) return Float.MIN_VALUE; |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 507 | if (pointerIndex == 0) return event.getRawX(); |
Adam Powell | 9bccdb7 | 2010-08-10 17:50:11 -0700 | [diff] [blame] | 508 | float offset = event.getRawX() - event.getX(); |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 509 | return event.getX(pointerIndex) + offset; |
| 510 | } |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 511 | |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 512 | /** |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 513 | * MotionEvent has no getRawY(int) method; simulate it pending future API approval. |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 514 | */ |
| 515 | private static float getRawY(MotionEvent event, int pointerIndex) { |
Adam Powell | 0fe4a13 | 2011-02-28 13:32:17 -0800 | [diff] [blame] | 516 | if (pointerIndex < 0) return Float.MIN_VALUE; |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 517 | if (pointerIndex == 0) return event.getRawY(); |
Adam Powell | 9bccdb7 | 2010-08-10 17:50:11 -0700 | [diff] [blame] | 518 | float offset = event.getRawY() - event.getY(); |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 519 | return event.getY(pointerIndex) + offset; |
| 520 | } |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 521 | |
| 522 | private void setContext(MotionEvent curr) { |
| 523 | if (mCurrEvent != null) { |
| 524 | mCurrEvent.recycle(); |
| 525 | } |
| 526 | mCurrEvent = MotionEvent.obtain(curr); |
| 527 | |
| 528 | mCurrLen = -1; |
| 529 | mPrevLen = -1; |
| 530 | mScaleFactor = -1; |
| 531 | |
| 532 | final MotionEvent prev = mPrevEvent; |
| 533 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 534 | final int prevIndex0 = prev.findPointerIndex(mActiveId0); |
| 535 | final int prevIndex1 = prev.findPointerIndex(mActiveId1); |
| 536 | final int currIndex0 = curr.findPointerIndex(mActiveId0); |
| 537 | final int currIndex1 = curr.findPointerIndex(mActiveId1); |
| 538 | |
Adam Powell | d0197f3 | 2011-03-17 00:09:03 -0700 | [diff] [blame] | 539 | if (prevIndex0 < 0 || prevIndex1 < 0 || currIndex0 < 0 || currIndex1 < 0) { |
| 540 | mInvalidGesture = true; |
| 541 | Log.e(TAG, "Invalid MotionEvent stream detected.", new Throwable()); |
| 542 | if (mGestureInProgress) { |
| 543 | mListener.onScaleEnd(this); |
| 544 | } |
| 545 | return; |
| 546 | } |
| 547 | |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 548 | final float px0 = prev.getX(prevIndex0); |
| 549 | final float py0 = prev.getY(prevIndex0); |
| 550 | final float px1 = prev.getX(prevIndex1); |
| 551 | final float py1 = prev.getY(prevIndex1); |
| 552 | final float cx0 = curr.getX(currIndex0); |
| 553 | final float cy0 = curr.getY(currIndex0); |
| 554 | final float cx1 = curr.getX(currIndex1); |
| 555 | final float cy1 = curr.getY(currIndex1); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 556 | |
| 557 | final float pvx = px1 - px0; |
| 558 | final float pvy = py1 - py0; |
| 559 | final float cvx = cx1 - cx0; |
| 560 | final float cvy = cy1 - cy0; |
| 561 | mPrevFingerDiffX = pvx; |
| 562 | mPrevFingerDiffY = pvy; |
| 563 | mCurrFingerDiffX = cvx; |
| 564 | mCurrFingerDiffY = cvy; |
| 565 | |
| 566 | mFocusX = cx0 + cvx * 0.5f; |
| 567 | mFocusY = cy0 + cvy * 0.5f; |
| 568 | mTimeDelta = curr.getEventTime() - prev.getEventTime(); |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 569 | mCurrPressure = curr.getPressure(currIndex0) + curr.getPressure(currIndex1); |
| 570 | mPrevPressure = prev.getPressure(prevIndex0) + prev.getPressure(prevIndex1); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | private void reset() { |
| 574 | if (mPrevEvent != null) { |
| 575 | mPrevEvent.recycle(); |
| 576 | mPrevEvent = null; |
| 577 | } |
| 578 | if (mCurrEvent != null) { |
| 579 | mCurrEvent.recycle(); |
| 580 | mCurrEvent = null; |
| 581 | } |
Adam Powell | 380b525 | 2010-01-19 16:33:46 -0800 | [diff] [blame] | 582 | mSloppyGesture = false; |
| 583 | mGestureInProgress = false; |
Adam Powell | e33cef8 | 2011-02-23 16:51:20 -0800 | [diff] [blame] | 584 | mActiveId0 = -1; |
| 585 | mActiveId1 = -1; |
Adam Powell | d0197f3 | 2011-03-17 00:09:03 -0700 | [diff] [blame] | 586 | mInvalidGesture = false; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Returns {@code true} if a two-finger scale gesture is in progress. |
| 591 | * @return {@code true} if a scale gesture is in progress, {@code false} otherwise. |
| 592 | */ |
| 593 | public boolean isInProgress() { |
| 594 | return mGestureInProgress; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Get the X coordinate of the current gesture's focal point. |
| 599 | * If a gesture is in progress, the focal point is directly between |
| 600 | * the two pointers forming the gesture. |
| 601 | * If a gesture is ending, the focal point is the location of the |
| 602 | * remaining pointer on the screen. |
Adam Powell | ab905c87 | 2010-02-03 11:01:58 -0800 | [diff] [blame] | 603 | * If {@link #isInProgress()} would return false, the result of this |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 604 | * function is undefined. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 605 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 606 | * @return X coordinate of the focal point in pixels. |
| 607 | */ |
| 608 | public float getFocusX() { |
| 609 | return mFocusX; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Get the Y coordinate of the current gesture's focal point. |
| 614 | * If a gesture is in progress, the focal point is directly between |
| 615 | * the two pointers forming the gesture. |
| 616 | * If a gesture is ending, the focal point is the location of the |
| 617 | * remaining pointer on the screen. |
Adam Powell | ab905c87 | 2010-02-03 11:01:58 -0800 | [diff] [blame] | 618 | * If {@link #isInProgress()} would return false, the result of this |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 619 | * function is undefined. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 620 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 621 | * @return Y coordinate of the focal point in pixels. |
| 622 | */ |
| 623 | public float getFocusY() { |
| 624 | return mFocusY; |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Return the current distance between the two pointers forming the |
| 629 | * gesture in progress. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 630 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 631 | * @return Distance between pointers in pixels. |
| 632 | */ |
| 633 | public float getCurrentSpan() { |
| 634 | if (mCurrLen == -1) { |
| 635 | final float cvx = mCurrFingerDiffX; |
| 636 | final float cvy = mCurrFingerDiffY; |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 637 | mCurrLen = FloatMath.sqrt(cvx*cvx + cvy*cvy); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 638 | } |
| 639 | return mCurrLen; |
| 640 | } |
| 641 | |
| 642 | /** |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 643 | * Return the current x distance between the two pointers forming the |
| 644 | * gesture in progress. |
| 645 | * |
| 646 | * @return Distance between pointers in pixels. |
| 647 | */ |
| 648 | public float getCurrentSpanX() { |
| 649 | return mCurrFingerDiffX; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Return the current y distance between the two pointers forming the |
| 654 | * gesture in progress. |
| 655 | * |
| 656 | * @return Distance between pointers in pixels. |
| 657 | */ |
| 658 | public float getCurrentSpanY() { |
| 659 | return mCurrFingerDiffY; |
| 660 | } |
| 661 | |
| 662 | /** |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 663 | * Return the previous distance between the two pointers forming the |
| 664 | * gesture in progress. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 665 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 666 | * @return Previous distance between pointers in pixels. |
| 667 | */ |
| 668 | public float getPreviousSpan() { |
| 669 | if (mPrevLen == -1) { |
| 670 | final float pvx = mPrevFingerDiffX; |
| 671 | final float pvy = mPrevFingerDiffY; |
Adam Powell | 346c8fb | 2010-03-12 10:52:35 -0800 | [diff] [blame] | 672 | mPrevLen = FloatMath.sqrt(pvx*pvx + pvy*pvy); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 673 | } |
| 674 | return mPrevLen; |
| 675 | } |
| 676 | |
| 677 | /** |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 678 | * Return the previous x distance between the two pointers forming the |
| 679 | * gesture in progress. |
| 680 | * |
| 681 | * @return Previous distance between pointers in pixels. |
| 682 | */ |
| 683 | public float getPreviousSpanX() { |
| 684 | return mPrevFingerDiffX; |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Return the previous y distance between the two pointers forming the |
| 689 | * gesture in progress. |
| 690 | * |
| 691 | * @return Previous distance between pointers in pixels. |
| 692 | */ |
| 693 | public float getPreviousSpanY() { |
| 694 | return mPrevFingerDiffY; |
| 695 | } |
| 696 | |
| 697 | /** |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 698 | * Return the scaling factor from the previous scale event to the current |
| 699 | * event. This value is defined as |
Adam Powell | ab905c87 | 2010-02-03 11:01:58 -0800 | [diff] [blame] | 700 | * ({@link #getCurrentSpan()} / {@link #getPreviousSpan()}). |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 701 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 702 | * @return The current scaling factor. |
| 703 | */ |
| 704 | public float getScaleFactor() { |
| 705 | if (mScaleFactor == -1) { |
| 706 | mScaleFactor = getCurrentSpan() / getPreviousSpan(); |
| 707 | } |
| 708 | return mScaleFactor; |
| 709 | } |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 710 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 711 | /** |
| 712 | * Return the time difference in milliseconds between the previous |
| 713 | * accepted scaling event and the current scaling event. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 714 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 715 | * @return Time difference since the last scaling event in milliseconds. |
| 716 | */ |
| 717 | public long getTimeDelta() { |
| 718 | return mTimeDelta; |
| 719 | } |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 720 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 721 | /** |
| 722 | * Return the event time of the current event being processed. |
Erik | 47c41e8 | 2010-09-01 15:39:08 -0700 | [diff] [blame] | 723 | * |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 724 | * @return Current event time in milliseconds. |
| 725 | */ |
| 726 | public long getEventTime() { |
| 727 | return mCurrEvent.getEventTime(); |
| 728 | } |
| 729 | } |