2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.ui;
19 import android.content.Context;
20 import android.view.ViewConfiguration;
21 import android.widget.OverScroller;
23 import com.android.gallery3d.common.Utils;
25 public class ScrollerHelper {
26 private OverScroller mScroller;
27 private int mOverflingDistance;
28 private boolean mOverflingEnabled;
30 public ScrollerHelper(Context context) {
31 mScroller = new OverScroller(context);
32 ViewConfiguration configuration = ViewConfiguration.get(context);
33 mOverflingDistance = configuration.getScaledOverflingDistance();
36 public void setOverfling(boolean enabled) {
37 mOverflingEnabled = enabled;
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.
45 public boolean advanceAnimation(long currentTimeMillis) {
46 return mScroller.computeScrollOffset();
49 public boolean isFinished() {
50 return mScroller.isFinished();
53 public void forceFinished() {
54 mScroller.forceFinished(true);
57 public int getPosition() {
58 return mScroller.getCurrX();
61 public float getCurrVelocity() {
62 return mScroller.getCurrVelocity();
65 public void setPosition(int position) {
66 mScroller.startScroll(
67 position, 0, // startX, startY
68 0, 0, 0); // dx, dy, duration
70 // This forces the scroller to reach the final position.
71 mScroller.abortAnimation();
74 public void fling(int velocity, int min, int max) {
75 int currX = getPosition();
77 currX, 0, // startX, startY
78 velocity, 0, // velocityX, velocityY
79 min, max, // minX, maxX
81 mOverflingEnabled ? mOverflingDistance : 0, 0);
84 // Returns the distance that over the scroll limit.
85 public int startScroll(int distance, int min, int max) {
86 int currPosition = mScroller.getCurrX();
87 int finalPosition = mScroller.isFinished() ? currPosition :
88 mScroller.getFinalX();
89 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
95 return finalPosition + distance - newPosition;