2017-07-14 00:02:27 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2017-07-06 12:35:55 -07:00
|
|
|
package com.android.launcher3.touch;
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2017-07-14 13:34:55 -07:00
|
|
|
import static android.view.MotionEvent.INVALID_POINTER_ID;
|
2016-06-06 14:19:02 -07:00
|
|
|
import android.content.Context;
|
2017-07-14 00:02:27 -07:00
|
|
|
import android.graphics.PointF;
|
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
import android.support.annotation.VisibleForTesting;
|
2016-06-06 14:19:02 -07:00
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.view.ViewConfiguration;
|
2017-03-20 16:11:54 -07:00
|
|
|
import android.view.animation.Interpolator;
|
2016-06-06 14:19:02 -07:00
|
|
|
|
|
|
|
|
/**
|
2017-07-06 12:35:55 -07:00
|
|
|
* One dimensional scroll/drag/swipe gesture detector.
|
2017-07-14 00:02:27 -07:00
|
|
|
*
|
|
|
|
|
* Definition of swipe is different from android system in that this detector handles
|
|
|
|
|
* 'swipe to dismiss', 'swiping up/down a container' but also keeps scrolling state before
|
|
|
|
|
* swipe action happens
|
2016-06-06 14:19:02 -07:00
|
|
|
*/
|
2017-07-06 12:35:55 -07:00
|
|
|
public class SwipeDetector {
|
2016-06-06 14:19:02 -07:00
|
|
|
|
|
|
|
|
private static final boolean DBG = false;
|
2017-07-06 12:35:55 -07:00
|
|
|
private static final String TAG = "SwipeDetector";
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
private int mScrollConditions;
|
2017-07-14 00:02:27 -07:00
|
|
|
public static final int DIRECTION_POSITIVE = 1 << 0;
|
|
|
|
|
public static final int DIRECTION_NEGATIVE = 1 << 1;
|
|
|
|
|
public static final int DIRECTION_BOTH = DIRECTION_NEGATIVE | DIRECTION_POSITIVE;
|
2016-06-21 16:37:13 -07:00
|
|
|
|
2017-03-20 16:11:54 -07:00
|
|
|
private static final float ANIMATION_DURATION = 1200;
|
|
|
|
|
private static final float FAST_FLING_PX_MS = 10;
|
|
|
|
|
|
2017-07-14 13:34:55 -07:00
|
|
|
protected int mActivePointerId = INVALID_POINTER_ID;
|
|
|
|
|
|
2016-06-06 14:19:02 -07:00
|
|
|
/**
|
|
|
|
|
* The minimum release velocity in pixels per millisecond that triggers fling..
|
|
|
|
|
*/
|
2016-07-27 12:48:09 -07:00
|
|
|
public static final float RELEASE_VELOCITY_PX_MS = 1.0f;
|
2016-06-06 14:19:02 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The time constant used to calculate dampening in the low-pass filter of scroll velocity.
|
|
|
|
|
* Cutoff frequency is set at 10 Hz.
|
|
|
|
|
*/
|
|
|
|
|
public static final float SCROLL_VELOCITY_DAMPENING_RC = 1000f / (2f * (float) Math.PI * 10);
|
|
|
|
|
|
|
|
|
|
/* Scroll state, this is set to true during dragging and animation. */
|
2016-06-30 17:22:26 -07:00
|
|
|
private ScrollState mState = ScrollState.IDLE;
|
2016-06-21 16:37:13 -07:00
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
enum ScrollState {
|
|
|
|
|
IDLE,
|
|
|
|
|
DRAGGING, // onDragStart, onDrag
|
|
|
|
|
SETTLING // onDragEnd
|
2016-07-14 15:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-14 00:02:27 -07:00
|
|
|
public static abstract class Direction {
|
|
|
|
|
|
|
|
|
|
abstract float getDisplacement(MotionEvent ev, int pointerIndex, PointF refPoint);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Distance in pixels a touch can wander before we think the user is scrolling.
|
|
|
|
|
*/
|
|
|
|
|
abstract float getActiveTouchSlop(MotionEvent ev, int pointerIndex, PointF downPos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static final Direction VERTICAL = new Direction() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
float getDisplacement(MotionEvent ev, int pointerIndex, PointF refPoint) {
|
|
|
|
|
return ev.getY(pointerIndex) - refPoint.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
float getActiveTouchSlop(MotionEvent ev, int pointerIndex, PointF downPos) {
|
|
|
|
|
return Math.abs(ev.getX(pointerIndex) - downPos.x);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static final Direction HORIZONTAL = new Direction() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
float getDisplacement(MotionEvent ev, int pointerIndex, PointF refPoint) {
|
|
|
|
|
return ev.getX(pointerIndex) - refPoint.x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
float getActiveTouchSlop(MotionEvent ev, int pointerIndex, PointF downPos) {
|
|
|
|
|
return Math.abs(ev.getY(pointerIndex) - downPos.y);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
//------------------- ScrollState transition diagram -----------------------------------
|
2016-06-21 16:37:13 -07:00
|
|
|
//
|
2016-06-30 17:22:26 -07:00
|
|
|
// IDLE -> (mDisplacement > mTouchSlop) -> DRAGGING
|
|
|
|
|
// DRAGGING -> (MotionEvent#ACTION_UP, MotionEvent#ACTION_CANCEL) -> SETTLING
|
|
|
|
|
// SETTLING -> (MotionEvent#ACTION_DOWN) -> DRAGGING
|
|
|
|
|
// SETTLING -> (View settled) -> IDLE
|
2016-06-13 12:38:32 -07:00
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
private void setState(ScrollState newState) {
|
2016-06-13 12:38:32 -07:00
|
|
|
if (DBG) {
|
2016-06-21 16:37:13 -07:00
|
|
|
Log.d(TAG, "setState:" + mState + "->" + newState);
|
2016-06-13 12:38:32 -07:00
|
|
|
}
|
2016-06-30 17:22:26 -07:00
|
|
|
// onDragStart and onDragEnd is reported ONLY on state transition
|
|
|
|
|
if (newState == ScrollState.DRAGGING) {
|
|
|
|
|
initializeDragging();
|
|
|
|
|
if (mState == ScrollState.IDLE) {
|
|
|
|
|
reportDragStart(false /* recatch */);
|
|
|
|
|
} else if (mState == ScrollState.SETTLING) {
|
|
|
|
|
reportDragStart(true /* recatch */);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (newState == ScrollState.SETTLING) {
|
|
|
|
|
reportDragEnd();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 12:38:32 -07:00
|
|
|
mState = newState;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 10:55:51 -07:00
|
|
|
public boolean isDraggingOrSettling() {
|
2016-06-30 17:22:26 -07:00
|
|
|
return mState == ScrollState.DRAGGING || mState == ScrollState.SETTLING;
|
2016-06-13 12:38:32 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 16:37:13 -07:00
|
|
|
/**
|
|
|
|
|
* There's no touch and there's no animation.
|
|
|
|
|
*/
|
2016-06-30 17:22:26 -07:00
|
|
|
public boolean isIdleState() {
|
|
|
|
|
return mState == ScrollState.IDLE;
|
2016-06-13 12:38:32 -07:00
|
|
|
}
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
public boolean isSettlingState() {
|
|
|
|
|
return mState == ScrollState.SETTLING;
|
2016-06-21 16:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-27 12:48:09 -07:00
|
|
|
public boolean isDraggingState() {
|
|
|
|
|
return mState == ScrollState.DRAGGING;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 00:02:27 -07:00
|
|
|
private final PointF mDownPos = new PointF();
|
|
|
|
|
private final PointF mLastPos = new PointF();
|
|
|
|
|
private final Direction mDir;
|
|
|
|
|
|
|
|
|
|
private final float mTouchSlop;
|
|
|
|
|
|
|
|
|
|
/* Client of this gesture detector can register a callback. */
|
|
|
|
|
private final Listener mListener;
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2016-08-02 13:31:22 -07:00
|
|
|
private long mCurrentMillis;
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2016-06-10 12:00:02 -07:00
|
|
|
private float mVelocity;
|
2017-07-14 00:02:27 -07:00
|
|
|
private float mLastDisplacement;
|
|
|
|
|
private float mDisplacement;
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2016-06-21 16:37:13 -07:00
|
|
|
private float mSubtractDisplacement;
|
2016-06-30 17:22:26 -07:00
|
|
|
private boolean mIgnoreSlopWhenSettling;
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2017-03-20 16:11:54 -07:00
|
|
|
public interface Listener {
|
2016-06-30 17:22:26 -07:00
|
|
|
void onDragStart(boolean start);
|
2016-07-14 15:09:11 -07:00
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
boolean onDrag(float displacement, float velocity);
|
2016-07-14 15:09:11 -07:00
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
void onDragEnd(float velocity, boolean fling);
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-14 00:02:27 -07:00
|
|
|
public SwipeDetector(@NonNull Context context, @NonNull Listener l, @NonNull Direction dir) {
|
|
|
|
|
this(ViewConfiguration.get(context).getScaledTouchSlop(), l, dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
protected SwipeDetector(float touchSlope, @NonNull Listener l, @NonNull Direction dir) {
|
|
|
|
|
mTouchSlop = touchSlope;
|
|
|
|
|
mListener = l;
|
|
|
|
|
mDir = dir;
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
public void setDetectableScrollConditions(int scrollDirectionFlags, boolean ignoreSlop) {
|
|
|
|
|
mScrollConditions = scrollDirectionFlags;
|
|
|
|
|
mIgnoreSlopWhenSettling = ignoreSlop;
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-14 00:02:27 -07:00
|
|
|
private boolean shouldScrollStart(MotionEvent ev, int pointerIndex) {
|
|
|
|
|
// reject cases where the angle or slop condition is not met.
|
|
|
|
|
if (Math.max(mDir.getActiveTouchSlop(ev, pointerIndex, mDownPos), mTouchSlop)
|
|
|
|
|
> Math.abs(mDisplacement)) {
|
2016-06-21 16:37:13 -07:00
|
|
|
return false;
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 16:37:13 -07:00
|
|
|
// Check if the client is interested in scroll in current direction.
|
2017-07-14 00:02:27 -07:00
|
|
|
if (((mScrollConditions & DIRECTION_NEGATIVE) > 0 && mDisplacement > 0) ||
|
|
|
|
|
((mScrollConditions & DIRECTION_POSITIVE) > 0 && mDisplacement < 0)) {
|
2016-06-13 12:38:32 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-06 14:19:02 -07:00
|
|
|
public boolean onTouchEvent(MotionEvent ev) {
|
2017-07-14 13:34:55 -07:00
|
|
|
switch (ev.getActionMasked()) {
|
2016-06-06 14:19:02 -07:00
|
|
|
case MotionEvent.ACTION_DOWN:
|
2017-07-14 13:34:55 -07:00
|
|
|
mActivePointerId = ev.getPointerId(0);
|
2017-07-14 00:02:27 -07:00
|
|
|
mDownPos.set(ev.getX(), ev.getY());
|
|
|
|
|
mLastPos.set(mDownPos);
|
|
|
|
|
mLastDisplacement = 0;
|
|
|
|
|
mDisplacement = 0;
|
2016-06-06 14:19:02 -07:00
|
|
|
mVelocity = 0;
|
2016-06-21 16:37:13 -07:00
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
if (mState == ScrollState.SETTLING && mIgnoreSlopWhenSettling) {
|
|
|
|
|
setState(ScrollState.DRAGGING);
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
break;
|
2017-07-14 13:34:55 -07:00
|
|
|
//case MotionEvent.ACTION_POINTER_DOWN:
|
|
|
|
|
case MotionEvent.ACTION_POINTER_UP:
|
2017-07-14 00:02:27 -07:00
|
|
|
int ptrIdx = ev.getActionIndex();
|
2017-07-14 13:34:55 -07:00
|
|
|
int ptrId = ev.getPointerId(ptrIdx);
|
|
|
|
|
if (ptrId == mActivePointerId) {
|
|
|
|
|
final int newPointerIdx = ptrIdx == 0 ? 1 : 0;
|
2017-07-14 00:02:27 -07:00
|
|
|
mDownPos.set(
|
|
|
|
|
ev.getX(newPointerIdx) - (mLastPos.x - mDownPos.x),
|
|
|
|
|
ev.getY(newPointerIdx) - (mLastPos.y - mDownPos.y));
|
|
|
|
|
mLastPos.set(ev.getX(newPointerIdx), ev.getY(newPointerIdx));
|
2017-07-14 13:34:55 -07:00
|
|
|
mActivePointerId = ev.getPointerId(newPointerIdx);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-06-06 14:19:02 -07:00
|
|
|
case MotionEvent.ACTION_MOVE:
|
2017-07-14 13:34:55 -07:00
|
|
|
int pointerIndex = ev.findPointerIndex(mActivePointerId);
|
|
|
|
|
if (pointerIndex == INVALID_POINTER_ID) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-14 00:02:27 -07:00
|
|
|
mDisplacement = mDir.getDisplacement(ev, pointerIndex, mDownPos);
|
|
|
|
|
computeVelocity(mDir.getDisplacement(ev, pointerIndex, mLastPos),
|
|
|
|
|
ev.getEventTime());
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2016-06-21 16:37:13 -07:00
|
|
|
// handle state and listener calls.
|
2017-07-14 00:02:27 -07:00
|
|
|
if (mState != ScrollState.DRAGGING && shouldScrollStart(ev, pointerIndex)) {
|
2016-06-30 17:22:26 -07:00
|
|
|
setState(ScrollState.DRAGGING);
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
2016-06-30 17:22:26 -07:00
|
|
|
if (mState == ScrollState.DRAGGING) {
|
|
|
|
|
reportDragging();
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
2017-07-14 00:02:27 -07:00
|
|
|
mLastPos.set(ev.getX(pointerIndex), ev.getY(pointerIndex));
|
2016-06-06 14:19:02 -07:00
|
|
|
break;
|
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
// These are synthetic events and there is no need to update internal values.
|
2016-06-30 17:22:26 -07:00
|
|
|
if (mState == ScrollState.DRAGGING) {
|
|
|
|
|
setState(ScrollState.SETTLING);
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void finishedScrolling() {
|
2016-06-30 17:22:26 -07:00
|
|
|
setState(ScrollState.IDLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean reportDragStart(boolean recatch) {
|
|
|
|
|
mListener.onDragStart(!recatch);
|
|
|
|
|
if (DBG) {
|
|
|
|
|
Log.d(TAG, "onDragStart recatch:" + recatch);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
private void initializeDragging() {
|
|
|
|
|
if (mState == ScrollState.SETTLING && mIgnoreSlopWhenSettling) {
|
|
|
|
|
mSubtractDisplacement = 0;
|
|
|
|
|
}
|
2017-07-14 00:02:27 -07:00
|
|
|
if (mDisplacement > 0) {
|
2016-06-21 16:37:13 -07:00
|
|
|
mSubtractDisplacement = mTouchSlop;
|
|
|
|
|
} else {
|
|
|
|
|
mSubtractDisplacement = -mTouchSlop;
|
|
|
|
|
}
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
private boolean reportDragging() {
|
2017-07-14 00:02:27 -07:00
|
|
|
if (mDisplacement != mLastDisplacement) {
|
2016-06-06 14:19:02 -07:00
|
|
|
if (DBG) {
|
2016-06-30 17:22:26 -07:00
|
|
|
Log.d(TAG, String.format("onDrag disp=%.1f, velocity=%.1f",
|
2017-07-14 00:02:27 -07:00
|
|
|
mDisplacement, mVelocity));
|
2016-06-08 16:29:32 -07:00
|
|
|
}
|
2016-06-21 16:37:13 -07:00
|
|
|
|
2017-07-14 00:02:27 -07:00
|
|
|
mLastDisplacement = mDisplacement;
|
|
|
|
|
return mListener.onDrag(mDisplacement - mSubtractDisplacement, mVelocity);
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 17:22:26 -07:00
|
|
|
private void reportDragEnd() {
|
2016-06-06 14:19:02 -07:00
|
|
|
if (DBG) {
|
2016-10-05 16:27:48 -07:00
|
|
|
Log.d(TAG, String.format("onScrollEnd disp=%.1f, velocity=%.1f",
|
2017-07-14 00:02:27 -07:00
|
|
|
mDisplacement, mVelocity));
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
2016-06-30 17:22:26 -07:00
|
|
|
mListener.onDragEnd(mVelocity, Math.abs(mVelocity) > RELEASE_VELOCITY_PX_MS);
|
2016-06-21 16:37:13 -07:00
|
|
|
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
2016-07-14 15:09:11 -07:00
|
|
|
|
2016-06-06 14:19:02 -07:00
|
|
|
/**
|
2017-07-14 00:02:27 -07:00
|
|
|
* Computes the damped velocity.
|
2016-06-06 14:19:02 -07:00
|
|
|
*/
|
2016-08-02 13:31:22 -07:00
|
|
|
public float computeVelocity(float delta, long currentMillis) {
|
|
|
|
|
long previousMillis = mCurrentMillis;
|
|
|
|
|
mCurrentMillis = currentMillis;
|
2016-06-06 14:19:02 -07:00
|
|
|
|
2016-08-02 13:31:22 -07:00
|
|
|
float deltaTimeMillis = mCurrentMillis - previousMillis;
|
2016-06-06 14:19:02 -07:00
|
|
|
float velocity = (deltaTimeMillis > 0) ? (delta / deltaTimeMillis) : 0;
|
2016-08-02 13:31:22 -07:00
|
|
|
if (Math.abs(mVelocity) < 0.001f) {
|
|
|
|
|
mVelocity = velocity;
|
|
|
|
|
} else {
|
|
|
|
|
float alpha = computeDampeningFactor(deltaTimeMillis);
|
|
|
|
|
mVelocity = interpolate(mVelocity, velocity, alpha);
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
2016-08-02 13:31:22 -07:00
|
|
|
return mVelocity;
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a time-dependent dampening factor using delta time.
|
|
|
|
|
*/
|
|
|
|
|
private static float computeDampeningFactor(float deltaTime) {
|
|
|
|
|
return deltaTime / (SCROLL_VELOCITY_DAMPENING_RC + deltaTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the linear interpolation between two values
|
|
|
|
|
*/
|
|
|
|
|
private static float interpolate(float from, float to, float alpha) {
|
|
|
|
|
return (1.0f - alpha) * from + alpha * to;
|
|
|
|
|
}
|
2017-03-20 16:11:54 -07:00
|
|
|
|
2017-07-14 00:02:27 -07:00
|
|
|
public static long calculateDuration(float velocity, float progressNeeded) {
|
2017-03-20 16:11:54 -07:00
|
|
|
// TODO: make these values constants after tuning.
|
|
|
|
|
float velocityDivisor = Math.max(2f, Math.abs(0.5f * velocity));
|
|
|
|
|
float travelDistance = Math.max(0.2f, progressNeeded);
|
|
|
|
|
long duration = (long) Math.max(100, ANIMATION_DURATION / velocityDivisor * travelDistance);
|
|
|
|
|
if (DBG) {
|
|
|
|
|
Log.d(TAG, String.format("calculateDuration=%d, v=%f, d=%f", duration, velocity, progressNeeded));
|
|
|
|
|
}
|
|
|
|
|
return duration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ScrollInterpolator implements Interpolator {
|
|
|
|
|
|
|
|
|
|
boolean mSteeper;
|
|
|
|
|
|
|
|
|
|
public void setVelocityAtZero(float velocity) {
|
|
|
|
|
mSteeper = velocity > FAST_FLING_PX_MS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getInterpolation(float t) {
|
|
|
|
|
t -= 1.0f;
|
|
|
|
|
float output = t * t * t;
|
|
|
|
|
if (mSteeper) {
|
|
|
|
|
output *= t * t; // Make interpolation initial slope steeper
|
|
|
|
|
}
|
|
|
|
|
return output + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|