ScaleGestureDetector.java revision 5b5c414e31c4a8433a3290b931687a05dadc97b6
1ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell/*
2ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * Copyright (C) 2010 The Android Open Source Project
3ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *
4ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * you may not use this file except in compliance with the License.
6ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * You may obtain a copy of the License at
7ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *
8ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *
10ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * Unless required by applicable law or agreed to in writing, software
11ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * See the License for the specific language governing permissions and
14ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * limitations under the License.
15ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell */
16ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
17ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powellpackage android.view;
18ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
19ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powellimport android.content.Context;
20a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powellimport android.os.SystemClock;
21346c8fb036b99a33756c66dcebeb5d0f67a1df72Adam Powellimport android.util.FloatMath;
22a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
23ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell/**
24618cbea4e746196cbde43746706bec02e14b487bAdam Powell * Detects scaling transformation gestures using the supplied {@link MotionEvent}s.
25618cbea4e746196cbde43746706bec02e14b487bAdam Powell * The {@link OnScaleGestureListener} callback will notify users when a particular
26618cbea4e746196cbde43746706bec02e14b487bAdam Powell * gesture event has occurred.
27618cbea4e746196cbde43746706bec02e14b487bAdam Powell *
28ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * This class should only be used with {@link MotionEvent}s reported via touch.
2947c41e807e36999e4d0d2072e41a82bc45655ff2Erik *
30ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * To use this class:
31ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * <ul>
32ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *  <li>Create an instance of the {@code ScaleGestureDetector} for your
33ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *      {@link View}
34ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *  <li>In the {@link View#onTouchEvent(MotionEvent)} method ensure you call
35ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *          {@link #onTouchEvent(MotionEvent)}. The methods defined in your
36ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell *          callback will be executed when the events occur.
37ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell * </ul>
38ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell */
39ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powellpublic class ScaleGestureDetector {
400818020d7cb04d83d51b71b8262d34bd79a76a95Adam Powell    private static final String TAG = "ScaleGestureDetector";
410818020d7cb04d83d51b71b8262d34bd79a76a95Adam Powell
42ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
43ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * The listener for receiving notifications when gestures occur.
44ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * If you want to listen for all the different gestures then implement
45ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * this interface. If you only want to listen for a subset it might
46ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * be easier to extend {@link SimpleOnScaleGestureListener}.
4747c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
48ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * An application will receive events in the following order:
49ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * <ul>
50ab905c87b7324d15715b78eaae7ef8558ad3bd10Adam Powell     *  <li>One {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)}
51ab905c87b7324d15715b78eaae7ef8558ad3bd10Adam Powell     *  <li>Zero or more {@link OnScaleGestureListener#onScale(ScaleGestureDetector)}
52ab905c87b7324d15715b78eaae7ef8558ad3bd10Adam Powell     *  <li>One {@link OnScaleGestureListener#onScaleEnd(ScaleGestureDetector)}
53ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * </ul>
54ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
55ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public interface OnScaleGestureListener {
56ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        /**
57ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * Responds to scaling events for a gesture in progress.
58ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * Reported by pointer motion.
5947c41e807e36999e4d0d2072e41a82bc45655ff2Erik         *
60ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * @param detector The detector reporting the event - use this to
61ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          retrieve extended info about event state.
62ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * @return Whether or not the detector should consider this event
63ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          as handled. If an event was not handled, the detector
64ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          will continue to accumulate movement until an event is
65ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          handled. This can be useful if an application, for example,
66ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          only wants to update scaling factors if the change is
67ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          greater than 0.01.
68ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         */
69ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        public boolean onScale(ScaleGestureDetector detector);
70ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
71ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        /**
72ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * Responds to the beginning of a scaling gesture. Reported by
73ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * new pointers going down.
7447c41e807e36999e4d0d2072e41a82bc45655ff2Erik         *
75ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * @param detector The detector reporting the event - use this to
76ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          retrieve extended info about event state.
77ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * @return Whether or not the detector should continue recognizing
78ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          this gesture. For example, if a gesture is beginning
79ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          with a focal point outside of a region where it makes
80ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          sense, onScaleBegin() may return false to ignore the
81ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          rest of the gesture.
82ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         */
83ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        public boolean onScaleBegin(ScaleGestureDetector detector);
84ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
85ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        /**
86ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * Responds to the end of a scale gesture. Reported by existing
87216bccf804db9c972b317620a27de6a8adf7fbfeAdam Powell         * pointers going up.
8847c41e807e36999e4d0d2072e41a82bc45655ff2Erik         *
89ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * Once a scale has ended, {@link ScaleGestureDetector#getFocusX()}
9047ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell         * and {@link ScaleGestureDetector#getFocusY()} will return focal point
9147ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell         * of the pointers remaining on the screen.
9247c41e807e36999e4d0d2072e41a82bc45655ff2Erik         *
93ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         * @param detector The detector reporting the event - use this to
94ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         *          retrieve extended info about event state.
95ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell         */
96ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        public void onScaleEnd(ScaleGestureDetector detector);
97ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
9847c41e807e36999e4d0d2072e41a82bc45655ff2Erik
99ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
100ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * A convenience class to extend when you only want to listen for a subset
101ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * of scaling-related events. This implements all methods in
102ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * {@link OnScaleGestureListener} but does nothing.
103346c8fb036b99a33756c66dcebeb5d0f67a1df72Adam Powell     * {@link OnScaleGestureListener#onScale(ScaleGestureDetector)} returns
104346c8fb036b99a33756c66dcebeb5d0f67a1df72Adam Powell     * {@code false} so that a subclass can retrieve the accumulated scale
105346c8fb036b99a33756c66dcebeb5d0f67a1df72Adam Powell     * factor in an overridden onScaleEnd.
106346c8fb036b99a33756c66dcebeb5d0f67a1df72Adam Powell     * {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)} returns
10747c41e807e36999e4d0d2072e41a82bc45655ff2Erik     * {@code true}.
108ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
109216bccf804db9c972b317620a27de6a8adf7fbfeAdam Powell    public static class SimpleOnScaleGestureListener implements OnScaleGestureListener {
110ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
111ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        public boolean onScale(ScaleGestureDetector detector) {
112346c8fb036b99a33756c66dcebeb5d0f67a1df72Adam Powell            return false;
113ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        }
114ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
115ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        public boolean onScaleBegin(ScaleGestureDetector detector) {
116ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell            return true;
117ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        }
118ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
119ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        public void onScaleEnd(ScaleGestureDetector detector) {
120ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell            // Intentionally empty
121ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        }
122ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
123ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
124346c8fb036b99a33756c66dcebeb5d0f67a1df72Adam Powell    private final Context mContext;
125346c8fb036b99a33756c66dcebeb5d0f67a1df72Adam Powell    private final OnScaleGestureListener mListener;
126ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
127ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    private float mFocusX;
128ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    private float mFocusY;
129618cbea4e746196cbde43746706bec02e14b487bAdam Powell
130618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private float mCurrSpan;
131618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private float mPrevSpan;
13247ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell    private float mInitialSpan;
133618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private float mCurrSpanX;
134618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private float mCurrSpanY;
135618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private float mPrevSpanX;
136618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private float mPrevSpanY;
137618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private long mCurrTime;
138618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private long mPrevTime;
139618cbea4e746196cbde43746706bec02e14b487bAdam Powell    private boolean mInProgress;
14047ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell    private int mSpanSlop;
141828e56ea6e8f4d5aa650052580f1f7873ad8296dAdam Powell    private int mMinSpan;
142e33cef8037cb87386e17bcf8701a47452d262fa6Adam Powell
1435b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell    // Bounds for recently seen values
1445b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell    private float mTouchUpper;
1455b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell    private float mTouchLower;
1465b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell    private float mTouchHistoryLastAccepted;
1475b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell    private int mTouchHistoryDirection;
1485b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell    private long mTouchHistoryLastAcceptedTime;
1495b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell    private int mTouchMinMajor;
150a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
151d736d2069b94348b519089348f4a85eb482db668Adam Powell    private static final long TOUCH_STABILIZE_TIME = 128; // ms
1525b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell    private static final int TOUCH_MIN_MAJOR = 48; // dp
153a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
15421bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown    /**
15521bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown     * Consistency verifier for debugging purposes.
15621bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown     */
15721bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown    private final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
15821bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown            InputEventConsistencyVerifier.isInstrumentationEnabled() ?
15921bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown                    new InputEventConsistencyVerifier(this, 0) : null;
16021bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown
161ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public ScaleGestureDetector(Context context, OnScaleGestureListener listener) {
162ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        mContext = context;
163ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        mListener = listener;
16447ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell        mSpanSlop = ViewConfiguration.get(context).getScaledTouchSlop() * 2;
1655b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        mTouchMinMajor =
1665b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                (int) (context.getResources().getDisplayMetrics().density * TOUCH_MIN_MAJOR + 0.5f);
167828e56ea6e8f4d5aa650052580f1f7873ad8296dAdam Powell        mMinSpan = context.getResources().getDimensionPixelSize(
168828e56ea6e8f4d5aa650052580f1f7873ad8296dAdam Powell                com.android.internal.R.dimen.config_minScalingSpan);
169ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
170ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
171618cbea4e746196cbde43746706bec02e14b487bAdam Powell    /**
172a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell     * The touchMajor/touchMinor elements of a MotionEvent can flutter/jitter on
173a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell     * some hardware/driver combos. Smooth it out to get kinder, gentler behavior.
174a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell     * @param ev MotionEvent to add to the ongoing history
175a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell     */
176a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell    private void addTouchHistory(MotionEvent ev) {
177a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell        final long currentTime = SystemClock.uptimeMillis();
178a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell        final int count = ev.getPointerCount();
1795b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        boolean accept = currentTime - mTouchHistoryLastAcceptedTime >= TOUCH_STABILIZE_TIME;
1805b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        float total = 0;
1815b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        int sampleCount = 0;
182a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell        for (int i = 0; i < count; i++) {
1835b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            final boolean hasLastAccepted = !Float.isNaN(mTouchHistoryLastAccepted);
184a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell            final int historySize = ev.getHistorySize();
1855b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            final int pointerSampleCount = historySize + 1;
1865b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            for (int h = 0; h < pointerSampleCount; h++) {
1875b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                float major;
188a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                if (h < historySize) {
189a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                    major = ev.getHistoricalTouchMajor(i, h);
190a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                } else {
191a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                    major = ev.getTouchMajor(i);
192a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                }
1935b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                if (major < mTouchMinMajor) major = mTouchMinMajor;
1945b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                total += major;
1955b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell
1965b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                if (Float.isNaN(mTouchUpper) || major > mTouchUpper) {
1975b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                    mTouchUpper = major;
1985b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                }
1995b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                if (Float.isNaN(mTouchLower) || major < mTouchLower) {
2005b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                    mTouchLower = major;
2015b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                }
202a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
203a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                if (hasLastAccepted) {
2045b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                    final int directionSig = (int) Math.signum(major - mTouchHistoryLastAccepted);
2055b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                    if (directionSig != mTouchHistoryDirection ||
2065b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                            (directionSig == 0 && mTouchHistoryDirection == 0)) {
2075b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                        mTouchHistoryDirection = directionSig;
208a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                        final long time = h < historySize ? ev.getHistoricalEventTime(h)
209a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                                : ev.getEventTime();
2105b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell                        mTouchHistoryLastAcceptedTime = time;
211a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                        accept = false;
212a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                    }
213a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                }
214a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell            }
2155b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            sampleCount += pointerSampleCount;
216a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell        }
217a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
2185b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        final float avg = total / sampleCount;
219a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
2205b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        if (accept) {
2215b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            float newAccepted = (mTouchUpper + mTouchLower + avg) / 3;
2225b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            mTouchUpper = (mTouchUpper + newAccepted) / 2;
2235b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            mTouchLower = (mTouchLower + newAccepted) / 2;
2245b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            mTouchHistoryLastAccepted = newAccepted;
2255b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            mTouchHistoryDirection = 0;
2265b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            mTouchHistoryLastAcceptedTime = ev.getEventTime();
227f3a2bf8edd2e070a24f0d7c105d78b38576e14acAdam Powell        }
228a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell    }
229a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
230a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell    /**
231a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell     * Clear all touch history tracking. Useful in ACTION_CANCEL or ACTION_UP.
232a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell     * @see #addTouchHistory(MotionEvent)
233a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell     */
234a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell    private void clearTouchHistory() {
2355b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        mTouchUpper = Float.NaN;
2365b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        mTouchLower = Float.NaN;
2375b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        mTouchHistoryLastAccepted = Float.NaN;
2385b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        mTouchHistoryDirection = 0;
2395b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        mTouchHistoryLastAcceptedTime = 0;
240a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell    }
241a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
242a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell    /**
243618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * Accepts MotionEvents and dispatches events to a {@link OnScaleGestureListener}
244618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * when appropriate.
245618cbea4e746196cbde43746706bec02e14b487bAdam Powell     *
246618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * <p>Applications should pass a complete and consistent event stream to this method.
247618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * A complete and consistent event stream involves all MotionEvents from the initial
248618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * ACTION_DOWN to the final ACTION_UP or ACTION_CANCEL.</p>
249618cbea4e746196cbde43746706bec02e14b487bAdam Powell     *
250618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * @param event The event to process
251618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * @return true if the event was processed and the detector wants to receive the
252618cbea4e746196cbde43746706bec02e14b487bAdam Powell     *         rest of the MotionEvents in this event stream.
253618cbea4e746196cbde43746706bec02e14b487bAdam Powell     */
254ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public boolean onTouchEvent(MotionEvent event) {
25521bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown        if (mInputEventConsistencyVerifier != null) {
25621bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown            mInputEventConsistencyVerifier.onTouchEvent(event, 0);
25721bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown        }
25821bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown
259e33cef8037cb87386e17bcf8701a47452d262fa6Adam Powell        final int action = event.getActionMasked();
260ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
261618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final boolean streamComplete = action == MotionEvent.ACTION_UP ||
262618cbea4e746196cbde43746706bec02e14b487bAdam Powell                action == MotionEvent.ACTION_CANCEL;
263618cbea4e746196cbde43746706bec02e14b487bAdam Powell        if (action == MotionEvent.ACTION_DOWN || streamComplete) {
264618cbea4e746196cbde43746706bec02e14b487bAdam Powell            // Reset any scale in progress with the listener.
265618cbea4e746196cbde43746706bec02e14b487bAdam Powell            // If it's an ACTION_DOWN we're beginning a new event stream.
266618cbea4e746196cbde43746706bec02e14b487bAdam Powell            // This means the app probably didn't give us all the events. Shame on it.
267618cbea4e746196cbde43746706bec02e14b487bAdam Powell            if (mInProgress) {
268618cbea4e746196cbde43746706bec02e14b487bAdam Powell                mListener.onScaleEnd(this);
269618cbea4e746196cbde43746706bec02e14b487bAdam Powell                mInProgress = false;
27047ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell                mInitialSpan = 0;
271ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell            }
272618cbea4e746196cbde43746706bec02e14b487bAdam Powell
273618cbea4e746196cbde43746706bec02e14b487bAdam Powell            if (streamComplete) {
274a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell                clearTouchHistory();
275618cbea4e746196cbde43746706bec02e14b487bAdam Powell                return true;
276ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell            }
277ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        }
278bbdc50b102faf52768ac3028bc49e027ff140656Jeff Brown
279abde042a824c3fc2f3537ef136017dfa006258b9Adam Powell        final boolean configChanged = action == MotionEvent.ACTION_DOWN ||
280618cbea4e746196cbde43746706bec02e14b487bAdam Powell                action == MotionEvent.ACTION_POINTER_UP ||
281618cbea4e746196cbde43746706bec02e14b487bAdam Powell                action == MotionEvent.ACTION_POINTER_DOWN;
282618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final boolean pointerUp = action == MotionEvent.ACTION_POINTER_UP;
283618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final int skipIndex = pointerUp ? event.getActionIndex() : -1;
284618cbea4e746196cbde43746706bec02e14b487bAdam Powell
285618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // Determine focal point
286618cbea4e746196cbde43746706bec02e14b487bAdam Powell        float sumX = 0, sumY = 0;
287618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final int count = event.getPointerCount();
288618cbea4e746196cbde43746706bec02e14b487bAdam Powell        for (int i = 0; i < count; i++) {
289618cbea4e746196cbde43746706bec02e14b487bAdam Powell            if (skipIndex == i) continue;
290618cbea4e746196cbde43746706bec02e14b487bAdam Powell            sumX += event.getX(i);
291618cbea4e746196cbde43746706bec02e14b487bAdam Powell            sumY += event.getY(i);
292bbdc50b102faf52768ac3028bc49e027ff140656Jeff Brown        }
293618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final int div = pointerUp ? count - 1 : count;
294618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final float focusX = sumX / div;
295618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final float focusY = sumY / div;
296618cbea4e746196cbde43746706bec02e14b487bAdam Powell
2975b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell
2985b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell        addTouchHistory(event);
299a4ce6ae0d379c8866697d064cfd1661ea156405eAdam Powell
300618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // Determine average deviation from focal point
301618cbea4e746196cbde43746706bec02e14b487bAdam Powell        float devSumX = 0, devSumY = 0;
302618cbea4e746196cbde43746706bec02e14b487bAdam Powell        for (int i = 0; i < count; i++) {
303618cbea4e746196cbde43746706bec02e14b487bAdam Powell            if (skipIndex == i) continue;
304828e56ea6e8f4d5aa650052580f1f7873ad8296dAdam Powell
3055b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            // Convert the resulting diameter into a radius.
3065b5c414e31c4a8433a3290b931687a05dadc97b6Adam Powell            final float touchSize = mTouchHistoryLastAccepted / 2;
307828e56ea6e8f4d5aa650052580f1f7873ad8296dAdam Powell            devSumX += Math.abs(event.getX(i) - focusX) + touchSize;
308828e56ea6e8f4d5aa650052580f1f7873ad8296dAdam Powell            devSumY += Math.abs(event.getY(i) - focusY) + touchSize;
309e33cef8037cb87386e17bcf8701a47452d262fa6Adam Powell        }
310618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final float devX = devSumX / div;
311618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final float devY = devSumY / div;
312618cbea4e746196cbde43746706bec02e14b487bAdam Powell
313618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // Span is the average distance between touch points through the focal point;
314618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // i.e. the diameter of the circle with a radius of the average deviation from
315618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // the focal point.
316618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final float spanX = devX * 2;
317618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final float spanY = devY * 2;
318618cbea4e746196cbde43746706bec02e14b487bAdam Powell        final float span = FloatMath.sqrt(spanX * spanX + spanY * spanY);
319618cbea4e746196cbde43746706bec02e14b487bAdam Powell
320618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // Dispatch begin/end events as needed.
321618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // If the configuration changes, notify the app to reset its current state by beginning
322618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // a fresh scale event stream.
32347ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell        final boolean wasInProgress = mInProgress;
32447ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell        mFocusX = focusX;
32547ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell        mFocusY = focusY;
326828e56ea6e8f4d5aa650052580f1f7873ad8296dAdam Powell        if (mInProgress && (span < mMinSpan || configChanged)) {
327618cbea4e746196cbde43746706bec02e14b487bAdam Powell            mListener.onScaleEnd(this);
328618cbea4e746196cbde43746706bec02e14b487bAdam Powell            mInProgress = false;
32947ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell            mInitialSpan = span;
330618cbea4e746196cbde43746706bec02e14b487bAdam Powell        }
331618cbea4e746196cbde43746706bec02e14b487bAdam Powell        if (configChanged) {
332618cbea4e746196cbde43746706bec02e14b487bAdam Powell            mPrevSpanX = mCurrSpanX = spanX;
333618cbea4e746196cbde43746706bec02e14b487bAdam Powell            mPrevSpanY = mCurrSpanY = spanY;
33447ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell            mInitialSpan = mPrevSpan = mCurrSpan = span;
335618cbea4e746196cbde43746706bec02e14b487bAdam Powell        }
336b1861c3e89c3e869c95c5c01b78320a1dcef26adAdam Powell        if (!mInProgress && span >= mMinSpan &&
33747ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell                (wasInProgress || Math.abs(span - mInitialSpan) > mSpanSlop)) {
33847ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell            mPrevSpanX = mCurrSpanX = spanX;
33947ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell            mPrevSpanY = mCurrSpanY = spanY;
34047ec2fb37046797bebc82b505a13c552021b9ff3Adam Powell            mPrevSpan = mCurrSpan = span;
341618cbea4e746196cbde43746706bec02e14b487bAdam Powell            mInProgress = mListener.onScaleBegin(this);
342ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        }
343ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
344618cbea4e746196cbde43746706bec02e14b487bAdam Powell        // Handle motion; focal point and span/scale factor are changing.
345618cbea4e746196cbde43746706bec02e14b487bAdam Powell        if (action == MotionEvent.ACTION_MOVE) {
346618cbea4e746196cbde43746706bec02e14b487bAdam Powell            mCurrSpanX = spanX;
347618cbea4e746196cbde43746706bec02e14b487bAdam Powell            mCurrSpanY = spanY;
348618cbea4e746196cbde43746706bec02e14b487bAdam Powell            mCurrSpan = span;
349618cbea4e746196cbde43746706bec02e14b487bAdam Powell
350618cbea4e746196cbde43746706bec02e14b487bAdam Powell            boolean updatePrev = true;
351618cbea4e746196cbde43746706bec02e14b487bAdam Powell            if (mInProgress) {
352618cbea4e746196cbde43746706bec02e14b487bAdam Powell                updatePrev = mListener.onScale(this);
353618cbea4e746196cbde43746706bec02e14b487bAdam Powell            }
354e33cef8037cb87386e17bcf8701a47452d262fa6Adam Powell
355618cbea4e746196cbde43746706bec02e14b487bAdam Powell            if (updatePrev) {
356618cbea4e746196cbde43746706bec02e14b487bAdam Powell                mPrevSpanX = mCurrSpanX;
357618cbea4e746196cbde43746706bec02e14b487bAdam Powell                mPrevSpanY = mCurrSpanY;
358618cbea4e746196cbde43746706bec02e14b487bAdam Powell                mPrevSpan = mCurrSpan;
359d0197f3669efda060c7ee2069ff41bd970fd6d9cAdam Powell            }
360d0197f3669efda060c7ee2069ff41bd970fd6d9cAdam Powell        }
361d0197f3669efda060c7ee2069ff41bd970fd6d9cAdam Powell
362618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return true;
363ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
364ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
365ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
366618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * Returns {@code true} if a scale gesture is in progress.
367ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
368ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public boolean isInProgress() {
369618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mInProgress;
370ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
371ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
372ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
373ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * Get the X coordinate of the current gesture's focal point.
374618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * If a gesture is in progress, the focal point is between
375618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * each of the pointers forming the gesture.
376618cbea4e746196cbde43746706bec02e14b487bAdam Powell     *
377ab905c87b7324d15715b78eaae7ef8558ad3bd10Adam Powell     * If {@link #isInProgress()} would return false, the result of this
378ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * function is undefined.
37947c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
380ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * @return X coordinate of the focal point in pixels.
381ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
382ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public float getFocusX() {
383ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        return mFocusX;
384ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
385ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
386ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
387ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * Get the Y coordinate of the current gesture's focal point.
388618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * If a gesture is in progress, the focal point is between
389618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * each of the pointers forming the gesture.
390618cbea4e746196cbde43746706bec02e14b487bAdam Powell     *
391ab905c87b7324d15715b78eaae7ef8558ad3bd10Adam Powell     * If {@link #isInProgress()} would return false, the result of this
392ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * function is undefined.
39347c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
394ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * @return Y coordinate of the focal point in pixels.
395ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
396ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public float getFocusY() {
397ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell        return mFocusY;
398ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
399ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
400ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
401618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * Return the average distance between each of the pointers forming the
402618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * gesture in progress through the focal point.
40347c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
404ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * @return Distance between pointers in pixels.
405ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
406ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public float getCurrentSpan() {
407618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mCurrSpan;
408ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
409ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
410ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
411618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * Return the average X distance between each of the pointers forming the
412618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * gesture in progress through the focal point.
41347c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
41447c41e807e36999e4d0d2072e41a82bc45655ff2Erik     * @return Distance between pointers in pixels.
41547c41e807e36999e4d0d2072e41a82bc45655ff2Erik     */
41647c41e807e36999e4d0d2072e41a82bc45655ff2Erik    public float getCurrentSpanX() {
417618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mCurrSpanX;
41847c41e807e36999e4d0d2072e41a82bc45655ff2Erik    }
41947c41e807e36999e4d0d2072e41a82bc45655ff2Erik
42047c41e807e36999e4d0d2072e41a82bc45655ff2Erik    /**
421618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * Return the average Y distance between each of the pointers forming the
422618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * gesture in progress through the focal point.
42347c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
42447c41e807e36999e4d0d2072e41a82bc45655ff2Erik     * @return Distance between pointers in pixels.
42547c41e807e36999e4d0d2072e41a82bc45655ff2Erik     */
42647c41e807e36999e4d0d2072e41a82bc45655ff2Erik    public float getCurrentSpanY() {
427618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mCurrSpanY;
42847c41e807e36999e4d0d2072e41a82bc45655ff2Erik    }
42947c41e807e36999e4d0d2072e41a82bc45655ff2Erik
43047c41e807e36999e4d0d2072e41a82bc45655ff2Erik    /**
431618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * Return the previous average distance between each of the pointers forming the
432618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * gesture in progress through the focal point.
43347c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
434ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * @return Previous distance between pointers in pixels.
435ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
436ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public float getPreviousSpan() {
437618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mPrevSpan;
438ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
439ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell
440ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
441618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * Return the previous average X distance between each of the pointers forming the
442618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * gesture in progress through the focal point.
44347c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
44447c41e807e36999e4d0d2072e41a82bc45655ff2Erik     * @return Previous distance between pointers in pixels.
44547c41e807e36999e4d0d2072e41a82bc45655ff2Erik     */
44647c41e807e36999e4d0d2072e41a82bc45655ff2Erik    public float getPreviousSpanX() {
447618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mPrevSpanX;
44847c41e807e36999e4d0d2072e41a82bc45655ff2Erik    }
44947c41e807e36999e4d0d2072e41a82bc45655ff2Erik
45047c41e807e36999e4d0d2072e41a82bc45655ff2Erik    /**
451618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * Return the previous average Y distance between each of the pointers forming the
452618cbea4e746196cbde43746706bec02e14b487bAdam Powell     * gesture in progress through the focal point.
45347c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
45447c41e807e36999e4d0d2072e41a82bc45655ff2Erik     * @return Previous distance between pointers in pixels.
45547c41e807e36999e4d0d2072e41a82bc45655ff2Erik     */
45647c41e807e36999e4d0d2072e41a82bc45655ff2Erik    public float getPreviousSpanY() {
457618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mPrevSpanY;
45847c41e807e36999e4d0d2072e41a82bc45655ff2Erik    }
45947c41e807e36999e4d0d2072e41a82bc45655ff2Erik
46047c41e807e36999e4d0d2072e41a82bc45655ff2Erik    /**
461ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * Return the scaling factor from the previous scale event to the current
462ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * event. This value is defined as
463ab905c87b7324d15715b78eaae7ef8558ad3bd10Adam Powell     * ({@link #getCurrentSpan()} / {@link #getPreviousSpan()}).
46447c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
465ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * @return The current scaling factor.
466ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
467ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public float getScaleFactor() {
468618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mPrevSpan > 0 ? mCurrSpan / mPrevSpan : 1;
469ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
47047c41e807e36999e4d0d2072e41a82bc45655ff2Erik
471ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
472ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * Return the time difference in milliseconds between the previous
473ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * accepted scaling event and the current scaling event.
47447c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
475ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * @return Time difference since the last scaling event in milliseconds.
476ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
477ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public long getTimeDelta() {
478618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mCurrTime - mPrevTime;
479ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
48047c41e807e36999e4d0d2072e41a82bc45655ff2Erik
481ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    /**
482ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * Return the event time of the current event being processed.
48347c41e807e36999e4d0d2072e41a82bc45655ff2Erik     *
484ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     * @return Current event time in milliseconds.
485ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell     */
486ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    public long getEventTime() {
487618cbea4e746196cbde43746706bec02e14b487bAdam Powell        return mCurrTime;
488ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell    }
489ae542ff055301a4c3c8a18e8da1739df3a771958Adam Powell}
490