1 /* 2 * Copyright (C) 2011 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 com.android.gallery3d.ui; 18 19 import com.android.gallery3d.common.Utils; 20 21 // This is a customized version of Scroller, with a interface similar to 22 // android.widget.Scroller. It does fling only, not scroll. 23 // 24 // The differences between the this Scroller and the system one are: 25 // 26 // (1) The velocity does not change because of min/max limit. 27 // (2) The duration is different. 28 // (3) The deceleration curve is different. 29 class FlingScroller { 30 private static final String TAG = "FlingController"; 31 32 // The fling duration (in milliseconds) when velocity is 1 pixel/second 33 private static final float FLING_DURATION_PARAM = 50f; 34 private static final int DECELERATED_FACTOR = 4; 35 36 private int mStartX, mStartY; 37 private int mMinX, mMinY, mMaxX, mMaxY; 38 private double mSinAngle; 39 private double mCosAngle; 40 private int mDuration; 41 private int mDistance; 42 private int mFinalX, mFinalY; 43 44 private int mCurrX, mCurrY; 45 private double mCurrV; 46 getFinalX()47 public int getFinalX() { 48 return mFinalX; 49 } 50 getFinalY()51 public int getFinalY() { 52 return mFinalY; 53 } 54 getDuration()55 public int getDuration() { 56 return mDuration; 57 } 58 getCurrX()59 public int getCurrX() { 60 return mCurrX; 61 62 } 63 getCurrY()64 public int getCurrY() { 65 return mCurrY; 66 } 67 getCurrVelocityX()68 public int getCurrVelocityX() { 69 return (int)Math.round(mCurrV * mCosAngle); 70 } 71 getCurrVelocityY()72 public int getCurrVelocityY() { 73 return (int)Math.round(mCurrV * mSinAngle); 74 } 75 fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)76 public void fling(int startX, int startY, int velocityX, int velocityY, 77 int minX, int maxX, int minY, int maxY) { 78 mStartX = startX; 79 mStartY = startY; 80 mMinX = minX; 81 mMinY = minY; 82 mMaxX = maxX; 83 mMaxY = maxY; 84 85 double velocity = Math.hypot(velocityX, velocityY); 86 mSinAngle = velocityY / velocity; 87 mCosAngle = velocityX / velocity; 88 // 89 // The position formula: x(t) = s + (e - s) * (1 - (1 - t / T) ^ d) 90 // velocity formula: v(t) = d * (e - s) * (1 - t / T) ^ (d - 1) / T 91 // Thus, 92 // v0 = d * (e - s) / T => (e - s) = v0 * T / d 93 // 94 95 // Ta = T_ref * (Va / V_ref) ^ (1 / (d - 1)); V_ref = 1 pixel/second; 96 mDuration = (int)Math.round(FLING_DURATION_PARAM 97 * Math.pow(Math.abs(velocity), 1.0 / (DECELERATED_FACTOR - 1))); 98 99 // (e - s) = v0 * T / d 100 mDistance = (int)Math.round( 101 velocity * mDuration / DECELERATED_FACTOR / 1000); 102 103 mFinalX = getX(1.0f); 104 mFinalY = getY(1.0f); 105 } 106 computeScrollOffset(float progress)107 public void computeScrollOffset(float progress) { 108 progress = Math.min(progress, 1); 109 float f = 1 - progress; 110 f = 1 - (float) Math.pow(f, DECELERATED_FACTOR); 111 mCurrX = getX(f); 112 mCurrY = getY(f); 113 mCurrV = getV(progress); 114 } 115 getX(float f)116 private int getX(float f) { 117 return (int) Utils.clamp( 118 Math.round(mStartX + f * mDistance * mCosAngle), mMinX, mMaxX); 119 } 120 getY(float f)121 private int getY(float f) { 122 return (int) Utils.clamp( 123 Math.round(mStartY + f * mDistance * mSinAngle), mMinY, mMaxY); 124 } 125 getV(float progress)126 private double getV(float progress) { 127 // velocity formula: v(t) = d * (e - s) * (1 - t / T) ^ (d - 1) / T 128 return DECELERATED_FACTOR * mDistance * 1000 * 129 Math.pow(1 - progress, DECELERATED_FACTOR - 1) / mDuration; 130 } 131 } 132