Example usage for android.view MotionEvent getPointerCount

List of usage examples for android.view MotionEvent getPointerCount

Introduction

In this page you can find the example usage for android.view MotionEvent getPointerCount.

Prototype

public final int getPointerCount() 

Source Link

Document

The number of pointers of data contained in this event.

Usage

From source file:me.ccrama.redditslide.Views.SubsamplingScaleImageView.java

/**
 * Handle touch events. One finger pans, and two finger pinch and zoom plus panning.
 *///from  w  w  w.j  a v a 2 s . c o  m
@Override
@SuppressWarnings("deprecation")
public boolean onTouchEvent(@NonNull MotionEvent event) {
    // During non-interruptible anims, ignore all touch events
    if (anim != null && !anim.interruptible) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return true;
    } else {
        if (anim != null && anim.listener != null) {
            try {
                anim.listener.onInterruptedByUser();
            } catch (Exception e) {
                Log.w(TAG, "Error thrown by animation listener", e);
            }
        }
        anim = null;
    }

    // Abort if not ready
    if (vTranslate == null) {
        return true;
    }
    // Detect flings, taps and double taps
    if (!isQuickScaling && (detector == null || detector.onTouchEvent(event))) {
        isZooming = false;
        isPanning = false;
        maxTouchCount = 0;
        return true;
    }

    if (vTranslateStart == null) {
        vTranslateStart = new PointF(0, 0);
    }
    if (vCenterStart == null) {
        vCenterStart = new PointF(0, 0);
    }

    int touchCount = event.getPointerCount();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_1_DOWN:
    case MotionEvent.ACTION_POINTER_2_DOWN:
        anim = null;
        getParent().requestDisallowInterceptTouchEvent(true);
        maxTouchCount = Math.max(maxTouchCount, touchCount);
        if (touchCount >= 2) {
            if (zoomEnabled) {
                // Start pinch to zoom. Calculate distance between touch points and center point of the pinch.
                float distance = distance(event.getX(0), event.getX(1), event.getY(0), event.getY(1));
                scaleStart = scale;
                vDistStart = distance;
                vTranslateStart.set(vTranslate.x, vTranslate.y);
                vCenterStart.set((event.getX(0) + event.getX(1)) / 2, (event.getY(0) + event.getY(1)) / 2);
            } else {
                // Abort all gestures on second touch
                maxTouchCount = 0;
            }
            // Cancel long click timer
            handler.removeMessages(MESSAGE_LONG_CLICK);
        } else if (!isQuickScaling) {
            // Start one-finger pan
            vTranslateStart.set(vTranslate.x, vTranslate.y);
            vCenterStart.set(event.getX(), event.getY());

            // Start long click timer
            handler.sendEmptyMessageDelayed(MESSAGE_LONG_CLICK, 600);
        }
        return true;
    case MotionEvent.ACTION_MOVE:
        boolean consumed = false;
        if (maxTouchCount > 0) {
            if (touchCount >= 2) {
                // Calculate new distance between touch points, to scale and pan relative to start values.
                float vDistEnd = distance(event.getX(0), event.getX(1), event.getY(0), event.getY(1));
                float vCenterEndX = (event.getX(0) + event.getX(1)) / 2;
                float vCenterEndY = (event.getY(0) + event.getY(1)) / 2;

                if (zoomEnabled && (distance(vCenterStart.x, vCenterEndX, vCenterStart.y, vCenterEndY) > 5
                        || Math.abs(vDistEnd - vDistStart) > 5 || isPanning)) {
                    isZooming = true;
                    isPanning = true;
                    consumed = true;

                    setScale(Math.min(maxScale, (vDistEnd / vDistStart) * scaleStart));

                    if (scale <= minScale()) {
                        // Minimum scale reached so don't pan. Adjust start settings so any expand will zoom in.
                        vDistStart = vDistEnd;
                        scaleStart = minScale();
                        vCenterStart.set(vCenterEndX, vCenterEndY);
                        vTranslateStart.set(vTranslate);
                    } else if (panEnabled) {
                        // Translate to place the source image coordinate that was at the center of the pinch at the start
                        // at the center of the pinch now, to give simultaneous pan + zoom.
                        float vLeftStart = vCenterStart.x - vTranslateStart.x;
                        float vTopStart = vCenterStart.y - vTranslateStart.y;
                        float vLeftNow = vLeftStart * (scale / scaleStart);
                        float vTopNow = vTopStart * (scale / scaleStart);
                        vTranslate.x = vCenterEndX - vLeftNow;
                        vTranslate.y = vCenterEndY - vTopNow;
                    } else if (sRequestedCenter != null) {
                        // With a center specified from code, zoom around that point.
                        vTranslate.x = (getWidth() / 2) - (scale * sRequestedCenter.x);
                        vTranslate.y = (getHeight() / 2) - (scale * sRequestedCenter.y);
                    } else {
                        // With no requested center, scale around the image center.
                        vTranslate.x = (getWidth() / 2) - (scale * (sWidth() / 2));
                        vTranslate.y = (getHeight() / 2) - (scale * (sHeight() / 2));
                    }

                    fitToBounds(true);
                    refreshRequiredTiles(false);
                }
            } else if (isQuickScaling) {
                // One finger zoom
                // Stole Google's Magical Formula to make sure it feels the exact same
                float dist = Math.abs(vCenterStart.y - event.getY()) * 2 + quickScaleThreshold;

                if (quickScaleLastDistance == -1F)
                    quickScaleLastDistance = dist;
                boolean isUpwards = event.getY() > quickScaleLastPoint.y;
                quickScaleLastPoint.set(0, event.getY());

                float spanDiff = (Math.abs(1 - (dist / quickScaleLastDistance)) * 0.5F);

                if (spanDiff > 0.03f || quickScaleMoved) {
                    quickScaleMoved = true;

                    float multiplier = 1;
                    if (quickScaleLastDistance > 0) {
                        multiplier = isUpwards ? (1 + spanDiff) : (1 - spanDiff);
                    }

                    setScale(Math.max(minScale(), Math.min(maxScale, scale * multiplier)));

                    if (panEnabled) {
                        float vLeftStart = vCenterStart.x - vTranslateStart.x;
                        float vTopStart = vCenterStart.y - vTranslateStart.y;
                        float vLeftNow = vLeftStart * (scale / scaleStart);
                        float vTopNow = vTopStart * (scale / scaleStart);
                        vTranslate.x = vCenterStart.x - vLeftNow;
                        vTranslate.y = vCenterStart.y - vTopNow;
                    } else if (sRequestedCenter != null) {
                        // With a center specified from code, zoom around that point.
                        vTranslate.x = (getWidth() / 2) - (scale * sRequestedCenter.x);
                        vTranslate.y = (getHeight() / 2) - (scale * sRequestedCenter.y);
                    } else {
                        // With no requested center, scale around the image center.
                        vTranslate.x = (getWidth() / 2) - (scale * (sWidth() / 2));
                        vTranslate.y = (getHeight() / 2) - (scale * (sHeight() / 2));
                    }
                }

                quickScaleLastDistance = dist;

                fitToBounds(true);
                refreshRequiredTiles(false);

                consumed = true;
            } else if (!isZooming) {
                // One finger pan - translate the image. We do this calculation even with pan disabled so click
                // and long click behaviour is preserved.
                float dx = Math.abs(event.getX() - vCenterStart.x);
                float dy = Math.abs(event.getY() - vCenterStart.y);
                if (dx > 5 || dy > 5 || isPanning) {
                    consumed = true;
                    vTranslate.x = vTranslateStart.x + (event.getX() - vCenterStart.x);
                    vTranslate.y = vTranslateStart.y + (event.getY() - vCenterStart.y);

                    float lastX = vTranslate.x;
                    float lastY = vTranslate.y;
                    fitToBounds(true);
                    boolean atXEdge = lastX != vTranslate.x;
                    boolean edgeXSwipe = atXEdge && dx > dy && !isPanning;
                    boolean yPan = lastY == vTranslate.y && dy > 15;
                    if (!edgeXSwipe && (!atXEdge || yPan || isPanning)) {
                        isPanning = true;
                    } else if (dx > 5) {
                        // Haven't panned the image, and we're at the left or right edge. Switch to page swipe.
                        maxTouchCount = 0;
                        handler.removeMessages(MESSAGE_LONG_CLICK);
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }

                    if (!panEnabled) {
                        vTranslate.x = vTranslateStart.x;
                        vTranslate.y = vTranslateStart.y;
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }

                    refreshRequiredTiles(false);
                }
            }
        }
        if (consumed) {
            handler.removeMessages(MESSAGE_LONG_CLICK);
            invalidate();
            return true;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_POINTER_2_UP:
        handler.removeMessages(MESSAGE_LONG_CLICK);
        if (isQuickScaling) {
            isQuickScaling = false;
            if (!quickScaleMoved) {
                doubleTapZoom(quickScaleCenter, vCenterStart);
            }
        }
        if (maxTouchCount > 0 && (isZooming || isPanning)) {
            if (isZooming && touchCount == 2) {
                // Convert from zoom to pan with remaining touch
                isPanning = true;
                vTranslateStart.set(vTranslate.x, vTranslate.y);
                if (event.getActionIndex() == 1) {
                    vCenterStart.set(event.getX(0), event.getY(0));
                } else {
                    vCenterStart.set(event.getX(1), event.getY(1));
                }
            }
            if (touchCount < 3) {
                // End zooming when only one touch point
                isZooming = false;
            }
            if (touchCount < 2) {
                // End panning when no touch points
                isPanning = false;
                maxTouchCount = 0;
            }
            // Trigger load of tiles now required
            refreshRequiredTiles(true);
            return true;
        }
        if (touchCount == 1) {
            isZooming = false;
            isPanning = false;
            maxTouchCount = 0;
        }
        return true;
    }
    return super.onTouchEvent(event);
}

From source file:com.cyanogenmod.filemanager.ui.widgets.ViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will dispatch callback events
 * as needed before returning. The parent view's onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 *//*from ww w .j  a va  2 s.co m*/
public void processTouchEvent(MotionEvent ev) {
    final int action = ev.getActionMasked();
    final int actionIndex = ev.getActionIndex();

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = ev.getPointerId(0);
        final View toCapture = findTopChildUnder((int) x, (int) y);

        saveInitialMotion(x, y, pointerId);

        // Since the parent is already directly processing this touch event,
        // there is no reason to delay for a slop before dragging.
        // Start immediately if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        final int pointerId = ev.getPointerId(actionIndex);
        final float x = ev.getX(actionIndex);
        final float y = ev.getY(actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            // If we're idle we can do anything! Treat it like a normal down event.

            final View toCapture = findTopChildUnder((int) x, (int) y);
            tryCaptureViewForDrag(toCapture, pointerId);

            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (isCapturedViewUnder((int) x, (int) y)) {
            // We're still tracking a captured view. If the same view is under this
            // point, we'll swap to controlling it with this pointer instead.
            // (This will still work if we're "catching" a settling view.)

            tryCaptureViewForDrag(mCapturedView, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_DRAGGING) {
            final int index = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(index);
            final float y = ev.getY(index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = (int) (y - mLastMotionY[mActivePointerId]);

            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);

            saveLastMotion(ev);
        } else {
            // Check to see if any pointer is now over a draggable view.
            final int pointerCount = ev.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                final int pointerId = ev.getPointerId(i);
                final float x = ev.getX(i);
                final float y = ev.getY(i);
                final float dx = x - mInitialMotionX[pointerId];
                final float dy = y - mInitialMotionY[pointerId];

                reportNewEdgeDrags(dx, dy, pointerId);
                if (mDragState == STATE_DRAGGING) {
                    // Callback might have started an edge drag.
                    break;
                }

                final View toCapture = findTopChildUnder((int) x, (int) y);
                if (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
                    break;
                }
            }
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerId = ev.getPointerId(actionIndex);
        if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
            // Try to find another pointer that's still holding on to the captured view.
            int newActivePointer = INVALID_POINTER;
            final int pointerCount = ev.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                final int id = ev.getPointerId(i);
                if (id == mActivePointerId) {
                    // This one's going away, skip.
                    continue;
                }

                final float x = ev.getX(i);
                final float y = ev.getY(i);
                if (findTopChildUnder((int) x, (int) y) == mCapturedView
                        && tryCaptureViewForDrag(mCapturedView, id)) {
                    newActivePointer = mActivePointerId;
                    break;
                }
            }

            if (newActivePointer == INVALID_POINTER) {
                // We didn't find another pointer still touching the view, release it.
                releaseViewForPointerUp();
            }
        }
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mDragState == STATE_DRAGGING) {
            releaseViewForPointerUp();
        }
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mDragState == STATE_DRAGGING) {
            dispatchViewReleased(0, 0);
        }
        cancel();
        break;
    }
    }
}

From source file:jackson.com.slidingmenulib.MyViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will dispatch callback events
 * as needed before returning. The parent view's onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 *///from   w  w  w .ja  v a2s .c  om
public void processTouchEvent(MotionEvent ev) {
    final int action = ev.getActionMasked();
    final int actionIndex = ev.getActionIndex();

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = ev.getPointerId(0);
        final View toCapture = findTopChildUnder((int) x, (int) y);

        saveInitialMotion(x, y, pointerId);

        // Since the parent is already directly processing this touch event,
        // there is no reason to delay for a slop before dragging.
        // Start immediately if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        final int pointerId = ev.getPointerId(actionIndex);
        final float x = ev.getX(actionIndex);
        final float y = ev.getY(actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            // If we're idle we can do anything! Treat it like a normal down event.

            final View toCapture = findTopChildUnder((int) x, (int) y);
            tryCaptureViewForDrag(toCapture, pointerId);

            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (isCapturedViewUnder((int) x, (int) y)) {
            // We're still tracking a captured view. If the same view is under this
            // point, we'll swap to controlling it with this pointer instead.
            // (This will still work if we're "catching" a settling view.)

            tryCaptureViewForDrag(mCapturedView, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_DRAGGING) {
            // If pointer is invalid then skip the ACTION_MOVE.
            if (!isValidPointerForActionMove(mActivePointerId))
                break;

            final int index = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(index);
            final float y = ev.getY(index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = (int) (y - mLastMotionY[mActivePointerId]);

            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);

            saveLastMotion(ev);
        } else {
            // Check to see if any pointer is now over a draggable view.
            final int pointerCount = ev.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                final int pointerId = ev.getPointerId(i);

                // If pointer is invalid then skip the ACTION_MOVE.
                if (!isValidPointerForActionMove(pointerId))
                    continue;

                final float x = ev.getX(i);
                final float y = ev.getY(i);
                final float dx = x - mInitialMotionX[pointerId];
                final float dy = y - mInitialMotionY[pointerId];

                reportNewEdgeDrags(dx, dy, pointerId);
                if (mDragState == STATE_DRAGGING) {
                    // Callback might have started an edge drag.
                    break;
                }

                final View toCapture = findTopChildUnder((int) x, (int) y);
                if (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
                    break;
                }
            }
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerId = ev.getPointerId(actionIndex);
        if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
            // Try to find another pointer that's still holding on to the captured view.
            int newActivePointer = INVALID_POINTER;
            final int pointerCount = ev.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                final int id = ev.getPointerId(i);
                if (id == mActivePointerId) {
                    // This one's going away, skip.
                    continue;
                }

                final float x = ev.getX(i);
                final float y = ev.getY(i);
                if (findTopChildUnder((int) x, (int) y) == mCapturedView
                        && tryCaptureViewForDrag(mCapturedView, id)) {
                    newActivePointer = mActivePointerId;
                    break;
                }
            }

            if (newActivePointer == INVALID_POINTER) {
                // We didn't find another pointer still touching the view, release it.
                releaseViewForPointerUp();
            }
        }
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mDragState == STATE_DRAGGING) {
            releaseViewForPointerUp();
        }
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mDragState == STATE_DRAGGING) {
            dispatchViewReleased(0, 0);
        }
        cancel();
        break;
    }
    }
}

From source file:com.example.SmartBoard.DrawingView.java

public boolean onTouchDragEvent(MotionEvent event) {

    boolean handled = false;

    JSONObject mObjectTouched;/*from w w  w  .ja v a 2s. c o  m*/
    int X;
    int Y;
    int pointerId;
    int actionIndex = event.getActionIndex();

    //get coordinates and make object transparent
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        dragFinished = false;

        //it's the first pointer, so clear all existing pointers data
        mObjectPointers.clear();
        X = (int) event.getX(actionIndex);
        Y = (int) event.getY(actionIndex);
        dragX = X;
        dragY = Y;
        String objectType = "";
        //check if we've touched inside some object
        mObjectTouched = getTouchedObject(X, Y);

        if (mObjectTouched == null) {
            return true;
        } else {
            objectType = getObjectType(mObjectTouched);
        }

        if (objectType.compareTo("Circle") == 0) {
            try {
                mObjectTouched.put("x", X);
                mObjectTouched.put("y", Y);

            } catch (JSONException e) {
                e.printStackTrace();

            }
        } else if (objectType.compareTo("Rectangle") == 0) {
            Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens"));
            tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height());
            try {
                mObjectTouched.put("dimens", tempRect.flattenToString());
            } catch (JSONException e) {

            }

        } else if (objectType.compareTo("Line") == 0) {

            if (lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                    mObjectTouched.optInt("starty")) < lengthOfLine(X, Y, mObjectTouched.optInt("stopx"),
                            mObjectTouched.optInt("stopy"))) {

                try {
                    mObjectTouched.put("startx", X);
                    mObjectTouched.put("starty", Y);
                    mObjectTouched.put("length",
                            lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy")));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dragLineStart = true;
            } else {
                try {
                    mObjectTouched.put("stopx", X);
                    mObjectTouched.put("stopy", Y);
                    mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                            mObjectTouched.optInt("starty")));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dragLineStart = false;
            }

        } else if (objectType.compareTo("Text") == 0) {
            try {
                mObjectTouched.put("x", X);
                mObjectTouched.put("y", Y);
                Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region"));
                tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y);
                mObjectTouched.put("region", tempRect.flattenToString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        mObjectPointers.put(event.getPointerId(0), mObjectTouched);
        if (mObjectTouched != null) {
            mqtt.publishObject(mObjectTouched);
        }
        invalidate();
        handled = true;
        break;

    case MotionEvent.ACTION_POINTER_DOWN:
        break;
    case MotionEvent.ACTION_MOVE:
        dragFinished = false;

        final int pointerCount = event.getPointerCount();

        for (actionIndex = 0; actionIndex < pointerCount; actionIndex++) {
            //some pointer has moved, search it by pointer id
            pointerId = event.getPointerId(actionIndex);
            X = (int) event.getX(actionIndex);
            Y = (int) event.getY(actionIndex);

            dragX = X;
            dragY = Y;

            mObjectTouched = mObjectPointers.get(pointerId);
            if (mObjectTouched == null)
                continue; // if null no object was touched so skip

            if (mObjectTouched.optString("type").compareTo("Circle") == 0) {
                try {
                    mObjectTouched.put("x", X);
                    mObjectTouched.put("y", Y);
                } catch (JSONException e) {
                }
            } else if (mObjectTouched.optString("type").compareTo("Rectangle") == 0) {

                Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens"));
                tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height());
                try {
                    mObjectTouched.put("dimens", tempRect.flattenToString());
                } catch (JSONException e) {

                }

            } else if (mObjectTouched.optString("type").compareTo("Text") == 0) {
                try {
                    mObjectTouched.put("x", X);
                    mObjectTouched.put("y", Y);
                    Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region"));
                    tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y);
                    mObjectTouched.put("region", tempRect.flattenToString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }

            else if (mObjectTouched.optString("type").compareTo("Line") == 0) {

                if (dragLineStart) {
                    try {
                        mObjectTouched.put("startx", X);
                        mObjectTouched.put("starty", Y);
                        mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("stopx"),
                                mObjectTouched.optInt("stopy")));

                        //mObjectTouched.put("stopx", tempStopX);
                        // mObjectTouched.put("stopy", tempStopY);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        mObjectTouched.put("stopx", X);
                        mObjectTouched.put("stopy", Y);
                        mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                                mObjectTouched.optInt("starty")));

                        //mObjectTouched.put("stopx", tempStopX);
                        // mObjectTouched.put("stopy", tempStopY);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }

            if (mObjectTouched != null) {
                mqtt.publishObject(mObjectTouched);
            }
        }

        invalidate();
        handled = true;
        break;
    case MotionEvent.ACTION_UP:
        dragFinished = true;

        mObjectPointers.clear();
        invalidate();
        handled = true;
        break;
    case MotionEvent.ACTION_CANCEL:
        handled = true;
        break;

    default:
        // do nothing
        break;
    }

    return super.onTouchEvent(event) || handled;
}

From source file:kr.wdream.ui.PhotoViewer.java

private boolean onTouchEvent(MotionEvent ev) {
    if (animationInProgress != 0 || animationStartTime != 0) {
        return false;
    }/*from w ww  .j  a  v  a  2  s.co  m*/

    if (currentEditMode == 2) {
        photoFilterView.onTouch(ev);
        return true;
    }

    if (currentEditMode == 1) {
        if (ev.getPointerCount() == 1) {
            if (photoCropView.onTouch(ev)) {
                updateMinMax(scale);
                return true;
            }
        } else {
            photoCropView.onTouch(null);
        }
    }

    if (captionEditText.isPopupShowing() || captionEditText.isKeyboardVisible()) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            closeCaptionEnter(true);
        }
        return true;
    }

    if (currentEditMode == 0 && ev.getPointerCount() == 1 && gestureDetector.onTouchEvent(ev)) {
        if (doubleTap) {
            doubleTap = false;
            moving = false;
            zooming = false;
            checkMinMax(false);
            return true;
        }
    }

    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN
            || ev.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
        if (currentEditMode == 1) {
            photoCropView.cancelAnimationRunnable();
        }
        discardTap = false;
        if (!scroller.isFinished()) {
            scroller.abortAnimation();
        }
        if (!draggingDown && !changingPage) {
            if (canZoom && ev.getPointerCount() == 2) {
                pinchStartDistance = (float) Math.hypot(ev.getX(1) - ev.getX(0), ev.getY(1) - ev.getY(0));
                pinchStartScale = scale;
                pinchCenterX = (ev.getX(0) + ev.getX(1)) / 2.0f;
                pinchCenterY = (ev.getY(0) + ev.getY(1)) / 2.0f;
                pinchStartX = translationX;
                pinchStartY = translationY;
                zooming = true;
                moving = false;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                }
            } else if (ev.getPointerCount() == 1) {
                moveStartX = ev.getX();
                dragY = moveStartY = ev.getY();
                draggingDown = false;
                canDragDown = true;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                }
            }
        }
    } else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
        if (currentEditMode == 1) {
            photoCropView.cancelAnimationRunnable();
        }
        if (canZoom && ev.getPointerCount() == 2 && !draggingDown && zooming && !changingPage) {
            discardTap = true;
            scale = (float) Math.hypot(ev.getX(1) - ev.getX(0), ev.getY(1) - ev.getY(0)) / pinchStartDistance
                    * pinchStartScale;
            translationX = (pinchCenterX - getContainerViewWidth() / 2)
                    - ((pinchCenterX - getContainerViewWidth() / 2) - pinchStartX) * (scale / pinchStartScale);
            translationY = (pinchCenterY - getContainerViewHeight() / 2)
                    - ((pinchCenterY - getContainerViewHeight() / 2) - pinchStartY) * (scale / pinchStartScale);
            updateMinMax(scale);
            containerView.invalidate();
        } else if (ev.getPointerCount() == 1) {
            if (velocityTracker != null) {
                velocityTracker.addMovement(ev);
            }
            float dx = Math.abs(ev.getX() - moveStartX);
            float dy = Math.abs(ev.getY() - dragY);
            if (dx > AndroidUtilities.dp(3) || dy > AndroidUtilities.dp(3)) {
                discardTap = true;
            }
            if (!(placeProvider instanceof EmptyPhotoViewerProvider) && currentEditMode == 0 && canDragDown
                    && !draggingDown && scale == 1 && dy >= AndroidUtilities.dp(30) && dy / 2 > dx) {
                draggingDown = true;
                moving = false;
                dragY = ev.getY();
                if (isActionBarVisible && canShowBottom) {
                    toggleActionBar(false, true);
                } else if (pickerView.getVisibility() == View.VISIBLE) {
                    toggleActionBar(false, true);
                    toggleCheckImageView(false);
                }
                return true;
            } else if (draggingDown) {
                translationY = ev.getY() - dragY;
                containerView.invalidate();
            } else if (!invalidCoords && animationStartTime == 0) {
                float moveDx = moveStartX - ev.getX();
                float moveDy = moveStartY - ev.getY();
                if (moving || currentEditMode != 0
                        || scale == 1 && Math.abs(moveDy) + AndroidUtilities.dp(12) < Math.abs(moveDx)
                        || scale != 1) {
                    if (!moving) {
                        moveDx = 0;
                        moveDy = 0;
                        moving = true;
                        canDragDown = false;
                    }

                    moveStartX = ev.getX();
                    moveStartY = ev.getY();
                    updateMinMax(scale);
                    if (translationX < minX && (currentEditMode != 0 || !rightImage.hasImage())
                            || translationX > maxX && (currentEditMode != 0 || !leftImage.hasImage())) {
                        moveDx /= 3.0f;
                    }
                    if (maxY == 0 && minY == 0 && currentEditMode == 0) {
                        if (translationY - moveDy < minY) {
                            translationY = minY;
                            moveDy = 0;
                        } else if (translationY - moveDy > maxY) {
                            translationY = maxY;
                            moveDy = 0;
                        }
                    } else {
                        if (translationY < minY || translationY > maxY) {
                            moveDy /= 3.0f;
                        }
                    }

                    translationX -= moveDx;
                    if (scale != 1 || currentEditMode != 0) {
                        translationY -= moveDy;
                    }

                    containerView.invalidate();
                }
            } else {
                invalidCoords = false;
                moveStartX = ev.getX();
                moveStartY = ev.getY();
            }
        }
    } else if (ev.getActionMasked() == MotionEvent.ACTION_CANCEL
            || ev.getActionMasked() == MotionEvent.ACTION_UP
            || ev.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
        if (currentEditMode == 1) {
            photoCropView.startAnimationRunnable();
        }
        if (zooming) {
            invalidCoords = true;
            if (scale < 1.0f) {
                updateMinMax(1.0f);
                animateTo(1.0f, 0, 0, true);
            } else if (scale > 3.0f) {
                float atx = (pinchCenterX - getContainerViewWidth() / 2)
                        - ((pinchCenterX - getContainerViewWidth() / 2) - pinchStartX)
                                * (3.0f / pinchStartScale);
                float aty = (pinchCenterY - getContainerViewHeight() / 2)
                        - ((pinchCenterY - getContainerViewHeight() / 2) - pinchStartY)
                                * (3.0f / pinchStartScale);
                updateMinMax(3.0f);
                if (atx < minX) {
                    atx = minX;
                } else if (atx > maxX) {
                    atx = maxX;
                }
                if (aty < minY) {
                    aty = minY;
                } else if (aty > maxY) {
                    aty = maxY;
                }
                animateTo(3.0f, atx, aty, true);
            } else {
                checkMinMax(true);
            }
            zooming = false;
        } else if (draggingDown) {
            if (Math.abs(dragY - ev.getY()) > getContainerViewHeight() / 6.0f) {
                closePhoto(true, false);
            } else {
                if (pickerView.getVisibility() == View.VISIBLE) {
                    toggleActionBar(true, true);
                    toggleCheckImageView(true);
                }
                animateTo(1, 0, 0, false);
            }
            draggingDown = false;
        } else if (moving) {
            float moveToX = translationX;
            float moveToY = translationY;
            updateMinMax(scale);
            moving = false;
            canDragDown = true;
            float velocity = 0;
            if (velocityTracker != null && scale == 1) {
                velocityTracker.computeCurrentVelocity(1000);
                velocity = velocityTracker.getXVelocity();
            }

            if (currentEditMode == 0) {
                if ((translationX < minX - getContainerViewWidth() / 3 || velocity < -AndroidUtilities.dp(650))
                        && rightImage.hasImage()) {
                    goToNext();
                    return true;
                }
                if ((translationX > maxX + getContainerViewWidth() / 3 || velocity > AndroidUtilities.dp(650))
                        && leftImage.hasImage()) {
                    goToPrev();
                    return true;
                }
            }

            if (translationX < minX) {
                moveToX = minX;
            } else if (translationX > maxX) {
                moveToX = maxX;
            }
            if (translationY < minY) {
                moveToY = minY;
            } else if (translationY > maxY) {
                moveToY = maxY;
            }
            animateTo(scale, moveToX, moveToY, false);
        }
    }
    return false;
}

From source file:org.telegram.ui.ArticleViewer.java

private boolean processTouchEvent(MotionEvent ev) {
    if (photoAnimationInProgress != 0 || animationStartTime != 0) {
        return false;
    }/*from   w w w. jav  a  2  s  .  c  om*/

    if (ev.getPointerCount() == 1 && gestureDetector.onTouchEvent(ev) && doubleTap) {
        doubleTap = false;
        moving = false;
        zooming = false;
        checkMinMax(false);
        return true;
    }

    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN
            || ev.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
        discardTap = false;
        if (!scroller.isFinished()) {
            scroller.abortAnimation();
        }
        if (!draggingDown && !changingPage) {
            if (canZoom && ev.getPointerCount() == 2) {
                pinchStartDistance = (float) Math.hypot(ev.getX(1) - ev.getX(0), ev.getY(1) - ev.getY(0));
                pinchStartScale = scale;
                pinchCenterX = (ev.getX(0) + ev.getX(1)) / 2.0f;
                pinchCenterY = (ev.getY(0) + ev.getY(1)) / 2.0f;
                pinchStartX = translationX;
                pinchStartY = translationY;
                zooming = true;
                moving = false;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                }
            } else if (ev.getPointerCount() == 1) {
                moveStartX = ev.getX();
                dragY = moveStartY = ev.getY();
                draggingDown = false;
                canDragDown = true;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                }
            }
        }
    } else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
        if (canZoom && ev.getPointerCount() == 2 && !draggingDown && zooming && !changingPage) {
            discardTap = true;
            scale = (float) Math.hypot(ev.getX(1) - ev.getX(0), ev.getY(1) - ev.getY(0)) / pinchStartDistance
                    * pinchStartScale;
            translationX = (pinchCenterX - getContainerViewWidth() / 2)
                    - ((pinchCenterX - getContainerViewWidth() / 2) - pinchStartX) * (scale / pinchStartScale);
            translationY = (pinchCenterY - getContainerViewHeight() / 2)
                    - ((pinchCenterY - getContainerViewHeight() / 2) - pinchStartY) * (scale / pinchStartScale);
            updateMinMax(scale);
            photoContainerView.invalidate();
        } else if (ev.getPointerCount() == 1) {
            if (velocityTracker != null) {
                velocityTracker.addMovement(ev);
            }
            float dx = Math.abs(ev.getX() - moveStartX);
            float dy = Math.abs(ev.getY() - dragY);
            if (dx > AndroidUtilities.dp(3) || dy > AndroidUtilities.dp(3)) {
                discardTap = true;
            }
            if (canDragDown && !draggingDown && scale == 1 && dy >= AndroidUtilities.dp(30) && dy / 2 > dx) {
                draggingDown = true;
                moving = false;
                dragY = ev.getY();
                if (isActionBarVisible) {
                    toggleActionBar(false, true);
                }
                return true;
            } else if (draggingDown) {
                translationY = ev.getY() - dragY;
                photoContainerView.invalidate();
            } else if (!invalidCoords && animationStartTime == 0) {
                float moveDx = moveStartX - ev.getX();
                float moveDy = moveStartY - ev.getY();
                if (moving || scale == 1 && Math.abs(moveDy) + AndroidUtilities.dp(12) < Math.abs(moveDx)
                        || scale != 1) {
                    if (!moving) {
                        moveDx = 0;
                        moveDy = 0;
                        moving = true;
                        canDragDown = false;
                    }

                    moveStartX = ev.getX();
                    moveStartY = ev.getY();
                    updateMinMax(scale);
                    if (translationX < minX && (!rightImage.hasImage())
                            || translationX > maxX && !leftImage.hasImage()) {
                        moveDx /= 3.0f;
                    }
                    if (maxY == 0 && minY == 0) {
                        if (translationY - moveDy < minY) {
                            translationY = minY;
                            moveDy = 0;
                        } else if (translationY - moveDy > maxY) {
                            translationY = maxY;
                            moveDy = 0;
                        }
                    } else {
                        if (translationY < minY || translationY > maxY) {
                            moveDy /= 3.0f;
                        }
                    }

                    translationX -= moveDx;
                    if (scale != 1) {
                        translationY -= moveDy;
                    }

                    photoContainerView.invalidate();
                }
            } else {
                invalidCoords = false;
                moveStartX = ev.getX();
                moveStartY = ev.getY();
            }
        }
    } else if (ev.getActionMasked() == MotionEvent.ACTION_CANCEL
            || ev.getActionMasked() == MotionEvent.ACTION_UP
            || ev.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
        if (zooming) {
            invalidCoords = true;
            if (scale < 1.0f) {
                updateMinMax(1.0f);
                animateTo(1.0f, 0, 0, true);
            } else if (scale > 3.0f) {
                float atx = (pinchCenterX - getContainerViewWidth() / 2)
                        - ((pinchCenterX - getContainerViewWidth() / 2) - pinchStartX)
                                * (3.0f / pinchStartScale);
                float aty = (pinchCenterY - getContainerViewHeight() / 2)
                        - ((pinchCenterY - getContainerViewHeight() / 2) - pinchStartY)
                                * (3.0f / pinchStartScale);
                updateMinMax(3.0f);
                if (atx < minX) {
                    atx = minX;
                } else if (atx > maxX) {
                    atx = maxX;
                }
                if (aty < minY) {
                    aty = minY;
                } else if (aty > maxY) {
                    aty = maxY;
                }
                animateTo(3.0f, atx, aty, true);
            } else {
                checkMinMax(true);
            }
            zooming = false;
        } else if (draggingDown) {
            if (Math.abs(dragY - ev.getY()) > getContainerViewHeight() / 6.0f) {
                closePhoto(true);
            } else {
                animateTo(1, 0, 0, false);
            }
            draggingDown = false;
        } else if (moving) {
            float moveToX = translationX;
            float moveToY = translationY;
            updateMinMax(scale);
            moving = false;
            canDragDown = true;
            float velocity = 0;
            if (velocityTracker != null && scale == 1) {
                velocityTracker.computeCurrentVelocity(1000);
                velocity = velocityTracker.getXVelocity();
            }

            if ((translationX < minX - getContainerViewWidth() / 3 || velocity < -AndroidUtilities.dp(650))
                    && rightImage.hasImage()) {
                goToNext();
                return true;
            }
            if ((translationX > maxX + getContainerViewWidth() / 3 || velocity > AndroidUtilities.dp(650))
                    && leftImage.hasImage()) {
                goToPrev();
                return true;
            }

            if (translationX < minX) {
                moveToX = minX;
            } else if (translationX > maxX) {
                moveToX = maxX;
            }
            if (translationY < minY) {
                moveToY = minY;
            } else if (translationY > maxY) {
                moveToY = maxY;
            }
            animateTo(scale, moveToX, moveToY, false);
        }
    }
    return false;
}

From source file:processing.core.PApplet.java

protected void enqueueMotionEvent(MotionEvent event) {
    synchronized (motionLock) {
        // on first run, allocate array for motion events
        if (motionEventQueue == null) {
            motionEventQueue = new PMotionEvent[20];
            for (int i = 0; i < motionEventQueue.length; i++) {
                motionEventQueue[i] = new PMotionEvent();
            }//from w  ww . j a v a 2s. co  m
        }
        // allocate more PMotionEvent objects if we're out
        int historyCount = event.getHistorySize();
        // println("motion: " + motionEventCount + " " + historyCount + " "
        // + motionEventQueue.length);
        if (motionEventCount + historyCount >= motionEventQueue.length) {
            int atLeast = motionEventCount + historyCount + 1;
            PMotionEvent[] temp = new PMotionEvent[max(atLeast, motionEventCount << 1)];
            if (PApplet.DEBUG) {
                println("motion: " + motionEventCount + " " + historyCount + " " + motionEventQueue.length);
                println("allocating " + temp.length + " entries for motion events");
            }
            System.arraycopy(motionEventQueue, 0, temp, 0, motionEventCount);
            motionEventQueue = temp;
            for (int i = motionEventCount; i < motionEventQueue.length; i++) {
                motionEventQueue[i] = new PMotionEvent();
            }
        }

        // this will be the last event in the list
        PMotionEvent pme = motionEventQueue[motionEventCount + historyCount];
        pme.setAction(event.getAction());
        pme.setNumPointers(event.getPointerCount());
        pme.setPointers(event);

        // historical events happen before the 'current' values
        if (pme.action == MotionEvent.ACTION_MOVE && historyCount > 0) {
            for (int i = 0; i < historyCount; i++) {
                PMotionEvent hist = motionEventQueue[motionEventCount++];
                hist.setAction(event.getAction());
                hist.setNumPointers(event.getPointerCount());
                hist.setPointers(event, i);
            }
        }

        // now step over the last one that we used to assign 'pme'
        // if historyCount is 0, this just steps over the last
        motionEventCount++;
    }
}