Home | History | Annotate | Download | only in ui
      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 
     17 package com.android.gallery3d.ui;
     18 
     19 import android.content.Context;
     20 import android.view.ViewConfiguration;
     21 import android.widget.OverScroller;
     22 
     23 import com.android.gallery3d.common.Utils;
     24 
     25 public 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 float getCurrVelocity() {
     62         return mScroller.getCurrVelocity();
     63     }
     64 
     65     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 
     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
     94         }
     95         return finalPosition + distance - newPosition;
     96     }
     97 }
     98