Android Open Source - GestureViews Scale Gesture Detector Fixed






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.detectors;
/* w  w  w. j av  a  2  s. c  o m*/
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

/**
 * 'Double tap and swipe' mode works bad for fast gestures. This class tries to fix this issue.
 */
public class ScaleGestureDetectorFixed extends ScaleGestureDetector {

    private float mCurrY, mPrevY;
    private float mLastScaleFactor;

    public ScaleGestureDetectorFixed(Context context, OnScaleGestureListener listener) {
        super(context, listener);
        warmUpScaleDetector();
    }

    /**
     * Scale detector is a little buggy when first time scale is occurred.
     * So we will feed it with fake motion event to warm it up.
     */
    private void warmUpScaleDetector() {
        long time = System.currentTimeMillis();
        MotionEvent event = MotionEvent.obtain(time, time, MotionEvent.ACTION_CANCEL, 0f, 0f, 0);
        onTouchEvent(event);
        event.recycle();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean result = super.onTouchEvent(event);

        mPrevY = mCurrY;
        mCurrY = event.getY();

        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            mPrevY = event.getY();
            mLastScaleFactor = 1f;
        }

        return result;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private boolean isInDoubleTapMode() {
        // Indirectly determine double tap mode
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && isQuickScaleEnabled() && getCurrentSpan() == getCurrentSpanY();
    }

    @Override
    public float getScaleFactor() {
        float factor = super.getScaleFactor();
        float lastFactor = mLastScaleFactor;
        mLastScaleFactor = factor;

        if (isInDoubleTapMode()) {
            return (mCurrY > mPrevY && factor > 1f) || (mCurrY < mPrevY && factor < 1f) ? factor : lastFactor;
        }

        return factor;
    }

}




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