blob: 1ff633a87a5a410af8e3cabc65eb299936d6f7a4 [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
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
17package com.android.gallery3d.ui;
18
Owen Lina2fba682011-08-17 22:07:43 +080019import android.content.Context;
20import android.view.ViewConfiguration;
21import android.widget.OverScroller;
22
Owen Lin73a04ff2012-03-14 17:27:24 +080023import com.android.gallery3d.common.Utils;
24
Owen Lina2fba682011-08-17 22:07:43 +080025public class ScrollerHelper {
26 private OverScroller mScroller;
27 private int mOverflingDistance;
28 private boolean mOverflingEnabled;
29
30 public ScrollerHelper(Context context) {
31 mScroller = new OverScroller(context);
32 ViewConfiguration configuration = ViewConfiguration.get(context);
33 mOverflingDistance = configuration.getScaledOverflingDistance();
34 }
35
36 public void setOverfling(boolean enabled) {
37 mOverflingEnabled = enabled;
38 }
39
40 /**
41 * Call this when you want to know the new location. The position will be
42 * updated and can be obtained by getPosition(). Returns true if the
43 * animation is not yet finished.
44 */
45 public boolean advanceAnimation(long currentTimeMillis) {
46 return mScroller.computeScrollOffset();
47 }
48
49 public boolean isFinished() {
50 return mScroller.isFinished();
51 }
52
53 public void forceFinished() {
54 mScroller.forceFinished(true);
55 }
56
57 public int getPosition() {
58 return mScroller.getCurrX();
59 }
60
Chih-Chung Changca078522011-09-26 10:40:38 +080061 public float getCurrVelocity() {
62 return mScroller.getCurrVelocity();
63 }
64
Owen Lina2fba682011-08-17 22:07:43 +080065 public void setPosition(int position) {
66 mScroller.startScroll(
67 position, 0, // startX, startY
68 0, 0, 0); // dx, dy, duration
69
70 // This forces the scroller to reach the final position.
71 mScroller.abortAnimation();
72 }
73
74 public void fling(int velocity, int min, int max) {
75 int currX = getPosition();
76 mScroller.fling(
77 currX, 0, // startX, startY
78 velocity, 0, // velocityX, velocityY
79 min, max, // minX, maxX
80 0, 0, // minY, maxY
81 mOverflingEnabled ? mOverflingDistance : 0, 0);
82 }
83
Chih-Chung Changca078522011-09-26 10:40:38 +080084 // Returns the distance that over the scroll limit.
85 public int startScroll(int distance, int min, int max) {
Owen Lina2fba682011-08-17 22:07:43 +080086 int currPosition = mScroller.getCurrX();
Chih-Chung Changb642bea2011-10-25 19:23:19 +080087 int finalPosition = mScroller.isFinished() ? currPosition :
88 mScroller.getFinalX();
Owen Lina2fba682011-08-17 22:07:43 +080089 int newPosition = Utils.clamp(finalPosition + distance, min, max);
90 if (newPosition != currPosition) {
91 mScroller.startScroll(
92 currPosition, 0, // startX, startY
93 newPosition - currPosition, 0, 0); // dx, dy, duration
Owen Lina2fba682011-08-17 22:07:43 +080094 }
Chih-Chung Changca078522011-09-26 10:40:38 +080095 return finalPosition + distance - newPosition;
Owen Lina2fba682011-08-17 22:07:43 +080096 }
97}