Android Open Source - GestureViews Smooth View Pager Scroller






From Project

Back to project page GestureViews.

License

The source code is released under:

Apache License

If you think the Android project GestureViews listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.alexvasilkov.gestures.utils;
//from   ww  w. j a va2s.c om
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Scroller;
import com.alexvasilkov.gestures.BuildConfig;

import java.lang.reflect.Field;

public class SmoothViewPagerScroller extends Scroller {

    private static final int BASE_DURATION = 500;

    private final int mDuration;
    private final float mDurationFactor;
    private boolean mIsFixedDuration;

    /**
     * Creates and applies SmoothViewPagerScroller to given ViewPager
     */
    public static SmoothViewPagerScroller applySmoothScroller(ViewPager pager, int duration) {
        try {
            Field scrollerField = ViewPager.class.getDeclaredField("mScroller");
            scrollerField.setAccessible(true);
            SmoothViewPagerScroller scroller = new SmoothViewPagerScroller(pager.getContext(),
                    new DecelerateInterpolator(), duration, duration / (float) BASE_DURATION);
            scrollerField.set(pager, scroller);
            return scroller;
        } catch (NoSuchFieldException e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
        } catch (IllegalArgumentException e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
        } catch (IllegalAccessException e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
        }
        return null;
    }

    private SmoothViewPagerScroller(Context context, Interpolator interpolator, int duration, float durationFactor) {
        super(context, interpolator);
        mDuration = duration;
        mDurationFactor = durationFactor;
    }

    public void setFixedDuration(boolean isFixedDuration) {
        mIsFixedDuration = isFixedDuration;
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        // Ignores received duration, uses fixed one instead
        int d = mIsFixedDuration ? mDuration : Math.round(Math.max(duration, mDuration) * mDurationFactor);
        super.startScroll(startX, startY, dx, dy, d);
    }

}




Java Source Code List

com.alexvasilkov.gestures.GesturesAdapter.java
com.alexvasilkov.gestures.GesturesControllerPagerFix.java
com.alexvasilkov.gestures.GesturesController.java
com.alexvasilkov.gestures.Settings.java
com.alexvasilkov.gestures.StateController.java
com.alexvasilkov.gestures.State.java
com.alexvasilkov.gestures.detectors.RotationGestureDetector.java
com.alexvasilkov.gestures.detectors.ScaleGestureDetectorFixed.java
com.alexvasilkov.gestures.sample.activities.BaseActivity.java
com.alexvasilkov.gestures.sample.activities.ImageCroppingActivity.java
com.alexvasilkov.gestures.sample.activities.ImageSnapshotActivity.java
com.alexvasilkov.gestures.sample.activities.ImagesPagerActivity.java
com.alexvasilkov.gestures.sample.activities.LayoutPagerActivity.java
com.alexvasilkov.gestures.sample.activities.MainActivity.java
com.alexvasilkov.gestures.sample.activities.TextViewActivity.java
com.alexvasilkov.gestures.sample.items.Painting.java
com.alexvasilkov.gestures.sample.items.PaintingsImagesAdapter.java
com.alexvasilkov.gestures.sample.items.PaintingsLayoutsAdapter.java
com.alexvasilkov.gestures.sample.utils.PicassoHelper.java
com.alexvasilkov.gestures.utils.FloatScroller.java
com.alexvasilkov.gestures.utils.MovementBounds.java
com.alexvasilkov.gestures.utils.SmoothViewPagerScroller.java
com.alexvasilkov.gestures.utils.Snapshot.java
com.alexvasilkov.gestures.widgets.GestureImageView.java
com.alexvasilkov.gestures.widgets.GestureLayout.java
com.alexvasilkov.gestures.widgets.GestureTextView.java