Android Open Source - GestureViews Float 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;
// www .  j a v  a2 s. co  m
import android.content.Context;
import android.os.SystemClock;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import com.alexvasilkov.gestures.R;

/**
 * A simple class that animates float values. Functionally similar to a {@link android.widget.Scroller}.
 */
public class FloatScroller {

    private Interpolator mInterpolator;
    private int mAnimationDuration;

    private boolean mFinished = true;

    private float mStartValue, mFinalValue;

    /**
     * Current value computed by {@link #computeScroll()}.
     */
    private float mCurrValue;

    /**
     * The time the animation started, computed using {@link android.os.SystemClock#elapsedRealtime()}.
     */
    private long mStartRTC;

    public FloatScroller(Context context) {
        mInterpolator = new DecelerateInterpolator();
        mAnimationDuration = context.getResources().getInteger(R.integer.gv_animation_duration);
    }

    /**
     * Force the finished field to a particular value.<br/>
     * Unlike {@link #abortAnimation()} the current value isn't set to the final value.
     *
     * @see android.widget.Scroller#forceFinished(boolean)
     */
    public void forceFinished(boolean finished) {
        mFinished = finished;
    }

    /**
     * Aborts the animation, setting the current value to the final value.
     *
     * @see android.widget.Scroller#abortAnimation()
     */
    public void abortAnimation() {
        mFinished = true;
        mCurrValue = mFinalValue;
    }

    /**
     * Starts an animation from startValue to finalValue.
     *
     * @see android.widget.Scroller#startScroll(int, int, int, int)
     */
    public void startScroll(float startValue, float finalValue) {
        mFinished = false;
        mStartRTC = SystemClock.elapsedRealtime();

        mStartValue = startValue;
        mFinalValue = finalValue;
        mCurrValue = startValue;
    }

    /**
     * Computes the current value, returning true if the animation is still active and false if the
     * animation has finished.
     *
     * @see android.widget.Scroller#computeScrollOffset()
     */
    public boolean computeScroll() {
        if (mFinished) {
            return false;
        }

        long elapsed = SystemClock.elapsedRealtime() - mStartRTC;
        if (elapsed >= mAnimationDuration) {
            mFinished = true;
            mCurrValue = mFinalValue;
            return false;
        }

        float time = mInterpolator.getInterpolation((float) elapsed / mAnimationDuration);
        mCurrValue = interpolate(mStartValue, mFinalValue, time);
        return true;
    }

    /**
     * Returns current state.
     *
     * @see android.widget.Scroller#isFinished()
     */
    public boolean isFinished() {
        return mFinished;
    }

    /**
     * Returns starting value.
     *
     * @see android.widget.Scroller#getStartX()
     */
    public float getStart() {
        return mStartValue;
    }

    /**
     * Returns final value.
     *
     * @see android.widget.Scroller#getFinalX()
     */
    public float getFinal() {
        return mFinalValue;
    }

    /**
     * Returns the current value.
     *
     * @see android.widget.Scroller#getCurrX()
     */
    public float getCurr() {
        return mCurrValue;
    }

    private static float interpolate(float x1, float x2, float f) {
        return x1 + (x2 - x1) * f;
    }

}




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