ScrollerHelper.java revision f9a0a4306d589b4a4e20554fed512a603426bfa1
1/*
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
19import com.android.gallery3d.common.Utils;
20
21import android.content.Context;
22import android.view.ViewConfiguration;
23import android.widget.OverScroller;
24
25public 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
61    public void setPosition(int position) {
62        mScroller.startScroll(
63                position, 0,    // startX, startY
64                0, 0, 0);       // dx, dy, duration
65
66        // This forces the scroller to reach the final position.
67        mScroller.abortAnimation();
68    }
69
70    public void fling(int velocity, int min, int max) {
71        int currX = getPosition();
72        mScroller.fling(
73                currX, 0,      // startX, startY
74                velocity, 0,   // velocityX, velocityY
75                min, max,      // minX, maxX
76                0, 0,          // minY, maxY
77                mOverflingEnabled ? mOverflingDistance : 0, 0);
78    }
79
80    public boolean startScroll(int distance, int min, int max) {
81        int currPosition = mScroller.getCurrX();
82        int finalPosition = mScroller.getFinalX();
83        int newPosition = Utils.clamp(finalPosition + distance, min, max);
84        if (newPosition != currPosition) {
85            mScroller.startScroll(
86                currPosition, 0,                    // startX, startY
87                newPosition - currPosition, 0, 0);  // dx, dy, duration
88            return true;
89        } else {
90            return false;
91        }
92    }
93}
94