Determines whether a two pointer gesture is a dragging one. - Android Graphics

Android examples for Graphics:Path Point

Description

Determines whether a two pointer gesture is a dragging one.

Demo Code


import android.util.MathUtils;
import android.view.MotionEvent;

public class Main{
    /**/*from   w w w  . j  av  a 2 s. co  m*/
     * Determines whether a two pointer gesture is a dragging one.
     *
     * @param event The event with the pointer data.
     * @return True if the gesture is a dragging one.
     */
    public static boolean isDraggingGesture(float firstPtrDownX,
            float firstPtrDownY, float secondPtrDownX,
            float secondPtrDownY, float firstPtrX, float firstPtrY,
            float secondPtrX, float secondPtrY, float maxDraggingAngleCos) {

        // Check if the pointers are moving in the same direction.
        final float firstDeltaX = firstPtrX - firstPtrDownX;
        final float firstDeltaY = firstPtrY - firstPtrDownY;

        if (firstDeltaX == 0 && firstDeltaY == 0) {
            return true;
        }

        final float firstMagnitude = (float) Math.hypot(firstDeltaX,
                firstDeltaY);
        final float firstXNormalized = (firstMagnitude > 0) ? firstDeltaX
                / firstMagnitude : firstDeltaX;
        final float firstYNormalized = (firstMagnitude > 0) ? firstDeltaY
                / firstMagnitude : firstDeltaY;

        final float secondDeltaX = secondPtrX - secondPtrDownX;
        final float secondDeltaY = secondPtrY - secondPtrDownY;

        if (secondDeltaX == 0 && secondDeltaY == 0) {
            return true;
        }

        final float secondMagnitude = (float) Math.hypot(secondDeltaX,
                secondDeltaY);
        final float secondXNormalized = (secondMagnitude > 0) ? secondDeltaX
                / secondMagnitude
                : secondDeltaX;
        final float secondYNormalized = (secondMagnitude > 0) ? secondDeltaY
                / secondMagnitude
                : secondDeltaY;

        final float angleCos = firstXNormalized * secondXNormalized
                + firstYNormalized * secondYNormalized;

        if (angleCos < maxDraggingAngleCos) {
            return false;
        }

        return true;
    }
}

Related Tutorials