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:paulscode.android.mupen64plusae.game.GameDrawerLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // Only intercept this touch event if it is not directly over a touchscreen input
    // (So the game sidebar is never accidentally triggered)

    int action = event.getAction();
    int actionCode = action & MotionEvent.ACTION_MASK;
    long currentEventTime = System.currentTimeMillis();

    boolean upAction = (actionCode == MotionEvent.ACTION_UP || actionCode == MotionEvent.ACTION_CANCEL
            || actionCode == MotionEvent.ACTION_POINTER_UP);

    // If the touch ended along the left edge, ignore edge swipes for a little while
    if (upAction) {
        int actionIndex = MotionEventCompat.getActionIndex(event);
        int xLocation = (int) event.getX(actionIndex);
        if (xLocation < 10)
            mLastEdgeTime = currentEventTime;
    }/*from  w  ww  . ja v  a 2s.c om*/

    if (ignore.contains(event)) {
        if (upAction)
            ignore.remove(event);
        return false;
    } else if (actionCode == MotionEvent.ACTION_POINTER_DOWN
            || (actionCode == MotionEvent.ACTION_DOWN && currentEventTime - mLastEdgeTime < 250)) {
        // Ignore secondary inputs and inputs too close to the most recent one (0.25 seconds)
        ignore.add(event);
        return false;
    } else if (actionCode == MotionEvent.ACTION_DOWN && !isDrawerOpen(GravityCompat.START)
            && mTouchMap != null) {
        for (int i = 0; i < event.getPointerCount(); i++) {
            int xLocation = (int) event.getX(i);
            int yLocation = (int) event.getY(i);

            // See if it touches the d-pad or the C buttons,
            // as they are small enough to interfere with left edge swipes
            // (fortunately placing the C buttons on the left is unusual)
            int buttonIndex = mTouchMap.getButtonPress(xLocation, yLocation);
            if (buttonIndex != TouchMap.UNMAPPED) {
                if ("dpad".equals(TouchMap.ASSET_NAMES.get(buttonIndex))
                        || "groupC".equals(TouchMap.ASSET_NAMES.get(buttonIndex))) {
                    ignore.add(event);
                    return false;
                }
            }

            // See if it touches the analog stick
            Point point = mTouchMap.getAnalogDisplacement(xLocation, yLocation);

            int dX = point.x;
            int dY = point.y;
            float displacement = (float) Math.sqrt((dX * dX) + (dY * dY));

            // Add a slightly larger hit area around the analog stick,
            // by artificially shrinking the size of the displacement
            displacement = displacement * 0.9f;

            if (mTouchMap.isInCaptureRange(displacement)) {
                ignore.add(event);
                return false;
            }
        }
    }

    // Let the parent DrawerLayout deal with it
    try {
        return super.onInterceptTouchEvent(event);
    } catch (Exception ex) {
        // For some reason this is very prone to crashing here when using multitouch:
        // android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent
        // But fortunately this is very unimportant, so we can safely ignore it
        // The source code is here if you want to attempt a fix:
        // https://github.com/android/platform_frameworks_support/blob/master/v4/java/android/support/v4/widget/ViewDragHelper.java
        return false;
    }
}

From source file:com.mylikes.likes.etchasketch.Slate.java

@SuppressLint("NewApi")
@Override/*w  w  w .j  a  v  a 2s  .com*/
public boolean onTouchEvent(MotionEvent event) {
    final int action = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) ? event.getActionMasked()
            : event.getAction();
    int N = event.getHistorySize();
    int P = event.getPointerCount();
    long time = event.getEventTime();

    mEmpty = false;

    // starting a new touch? commit the previous state of the canvas
    if (action == MotionEvent.ACTION_DOWN) {
        commitStroke();
    }

    if (mZoomMode) {
        return false;
    }

    int pointerIndex = MotionEventCompat.getActionIndex(event);
    int pointerId = event.getPointerId(pointerIndex);
    if (moveMode) {
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) {
            if (firstFinger != null) {
                MotionEvent.PointerCoords coords1 = new MotionEvent.PointerCoords();
                event.getPointerCoords(0, coords1);
                Log.d(TAG, "coords1: " + coords1.x + " " + coords1.y);
                MotionEvent.PointerCoords coords2 = new MotionEvent.PointerCoords();
                event.getPointerCoords(1, coords2);
                firstFinger.set(coords1.x, coords1.y);
                secondFinger = new PointF(coords2.x, coords2.y);
            } else {
                touchStartTime = System.currentTimeMillis();
                moveDrawingStartX = event.getX();
                moveDrawingStartY = event.getY();
                int i = 0;
                moveDrawingIndex = -1;
                resizeDrawingIndex = -1;
                resizeDrawingCorner = null;
                if (selectedDrawing != null) {
                    moveDrawingIndex = 0;
                }
                if (i >= overlays.size()) {
                    return true;
                }
                Log.d(TAG, "Start dragging overlay");
                firstFinger = new PointF(event.getX(), event.getY());
            }
            return true;
        } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
            if (secondFinger != null) {
                secondFinger = null;
                MotionEvent.PointerCoords coords1 = new MotionEvent.PointerCoords();
                event.getPointerCoords(0, coords1);
                moveDrawingStartX = coords1.x;
                moveDrawingStartY = coords1.y;
                return true;
            }
            if (firstFinger != null) {
                firstFinger = null;
            }
            if (moveDrawingIndex == -1 && resizeDrawingIndex == -1
                    && System.currentTimeMillis() - touchStartTime < 400
                    && Math.abs(event.getX() - moveDrawingStartX)
                            + Math.abs(event.getY() - moveDrawingStartY) < 8) {
                if (resizeDrawingCorner != null && selectedDrawing != null) {
                    if (resizeDrawingCorner == "tl" && selectedDrawing instanceof TextDrawing) {
                        //promptForText((TextDrawing)selectedDrawing);
                    } else if (resizeDrawingCorner == "tr") {
                        //maybeRemoveDrawing(selectedDrawing);
                    }
                } else {
                    //promptForText((int) event.getX(), (int) event.getY());
                }
            }
            moveDrawingIndex = -1;
            Log.d(TAG, "Stop dragging overlay");
            // TODO: add to undo stack
            return true;
        } else if (firstFinger != null && secondFinger != null && action == MotionEvent.ACTION_MOVE) {
            MotionEvent.PointerCoords coords1 = new MotionEvent.PointerCoords();
            event.getPointerCoords(0, coords1);
            Log.d(TAG, "coords1: " + coords1.x + " " + coords1.y);
            MotionEvent.PointerCoords coords2 = new MotionEvent.PointerCoords();
            event.getPointerCoords(1, coords2);
            Log.d(TAG, "coords2: " + coords2.x + " " + coords2.y);
            float xDist = firstFinger.x - secondFinger.x, yDist = firstFinger.y - secondFinger.y;
            float origAngle = (float) Math.atan2(yDist, xDist);
            if (origAngle < 0)
                origAngle += Math.PI * 2;
            float lastDistance = (float) Math.sqrt(xDist * xDist + yDist * yDist);

            xDist = coords2.x - coords1.x;
            yDist = coords2.y - coords1.y;
            float newDistance = (float) Math.sqrt(xDist * xDist + yDist * yDist);
            float newAngle = (float) Math.atan2(yDist, xDist);
            if (newAngle < 0)
                newAngle += Math.PI * 2;
            if (newAngle - origAngle > Math.PI / 2) {
                origAngle += Math.PI;
            } else if (origAngle - newAngle > Math.PI / 2) {
                newAngle += Math.PI;
            }

            firstFinger.set(coords1.x, coords1.y);
            secondFinger = new PointF(coords2.x, coords2.y);
            if (selectedDrawing != null) {
                selectedDrawing.resizeBy(newDistance / lastDistance);
                selectedDrawing.rotateBy(newAngle - origAngle);
                invalidate();
            }
        } else if (moveDrawingIndex >= 0 && moveDrawingIndex < overlays.size()
                && action == MotionEvent.ACTION_MOVE) {
            float x = event.getX();
            float y = event.getY();
            overlays.get(moveDrawingIndex).moveBy((int) ((x - moveDrawingStartX) * getDrawingDensity()),
                    (int) ((y - moveDrawingStartY) * getDrawingDensity()));
            // TODO: only invalidate relevant Rect
            invalidate();
            moveDrawingStartX = x;
            moveDrawingStartY = y;
            return true;
        } else if (resizeDrawingIndex >= 0 && resizeDrawingIndex < overlays.size()
                && action == MotionEvent.ACTION_MOVE) {
            float x = event.getX();
            float y = event.getY();
            overlays.get(resizeDrawingIndex).resizeCorner(resizeDrawingCorner, (int) (x - moveDrawingStartX),
                    (int) (y - moveDrawingStartY));
            // TODO: only invalidate relevant Rect
            invalidate();
            moveDrawingStartX = x;
            moveDrawingStartY = y;
            return true;
        }
        return false;
    }

    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN
            || action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
        int j = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) ? event.getActionIndex() : 0;

        mTmpSpot.update(event.getX(j), event.getY(j), event.getSize(j), event.getPressure(j) + event.getSize(j),
                time, getToolTypeCompat(event, j));
        mStrokes[event.getPointerId(j)].add(mTmpSpot);
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
            mStrokes[event.getPointerId(j)].finish(time);
        }
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (dbgX >= 0) {
            dbgRect.set(dbgX - 1, dbgY - 1, dbgX + 1, dbgY + 1);
        }

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < P; j++) {
                mTmpSpot.update(event.getHistoricalX(j, i), event.getHistoricalY(j, i),
                        event.getHistoricalSize(j, i),
                        event.getHistoricalPressure(j, i) + event.getHistoricalSize(j, i),
                        event.getHistoricalEventTime(i), getToolTypeCompat(event, j));
                if ((mDebugFlags & FLAG_DEBUG_STROKES) != 0) {
                    if (dbgX >= 0) {
                        //mTiledCanvas.drawLine(dbgX, dbgY, mTmpSpot.x, mTmpSpot.y, mDebugPaints[3]);
                    }
                    dbgX = mTmpSpot.x;
                    dbgY = mTmpSpot.y;
                    dbgRect.union(dbgX - 1, dbgY - 1, dbgX + 1, dbgY + 1);
                }
                mStrokes[event.getPointerId(j)].add(mTmpSpot);
            }
        }
        for (int j = 0; j < P; j++) {
            mTmpSpot.update(event.getX(j), event.getY(j), event.getSize(j),
                    event.getPressure(j) + event.getSize(j), time, getToolTypeCompat(event, j));
            if ((mDebugFlags & FLAG_DEBUG_STROKES) != 0) {
                if (dbgX >= 0) {
                    //mTiledCanvas.drawLine(dbgX, dbgY, mTmpSpot.x, mTmpSpot.y, mDebugPaints[3]);
                }
                dbgX = mTmpSpot.x;
                dbgY = mTmpSpot.y;
                dbgRect.union(dbgX - 1, dbgY - 1, dbgX + 1, dbgY + 1);
            }
            mStrokes[event.getPointerId(j)].add(mTmpSpot);
        }

        if ((mDebugFlags & FLAG_DEBUG_STROKES) != 0) {
            Rect dirty = new Rect();
            dbgRect.roundOut(dirty);
            invalidate(dirty);
        }
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        for (int j = 0; j < P; j++) {
            mStrokes[event.getPointerId(j)].finish(time);
        }
        dbgX = dbgY = -1;
    }
    return true;
}

From source file:org.florescu.android.rangeseekbar.RangeSeekBar.java

/**
 * Handles thumb selection and movement. Notifies listener callback on certain events.
 *///from w w  w  . j a  v a 2  s. c o m
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {

    if (!isEnabled()) {
        return false;
    }

    int pointerIndex;

    final int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN:
        // Remember where the motion event started
        mActivePointerId = event.getPointerId(event.getPointerCount() - 1);
        pointerIndex = event.findPointerIndex(mActivePointerId);
        mDownMotionX = event.getX(pointerIndex);

        pressedThumb = evalPressedThumb(mDownMotionX);

        // Only handle thumb presses.
        if (pressedThumb == null) {
            pressedThumb = getClosestThumb(mDownMotionX);
            if (pressedThumb == null) {
                return false;
            }
        }

        if (listener != null) {
            listener.onRangeSeekBarPressed(this,
                    Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(),
                    pressedThumb);
        }

        setPressed(true);
        invalidate();
        onStartTrackingTouch();
        trackTouchEvent(event);
        attemptClaimDrag();

        break;
    case MotionEvent.ACTION_MOVE:
        if (pressedThumb != null) {

            if (mIsDragging) {
                trackTouchEvent(event);
            } else {
                // Scroll to follow the motion event
                pointerIndex = event.findPointerIndex(mActivePointerId);
                final float x = event.getX(pointerIndex);

                if (Math.abs(x - mDownMotionX) > mScaledTouchSlop) {
                    setPressed(true);
                    invalidate();
                    onStartTrackingTouch();
                    trackTouchEvent(event);
                    attemptClaimDrag();
                }
            }

            if (notifyWhileDragging && listener != null) {
                listener.onRangeSeekBarValueChanged(this,
                        Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(),
                        pressedThumb);
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsDragging) {
            trackTouchEvent(event);
            onStopTrackingTouch();
            setPressed(false);
        } else {
            // Touch up when we never crossed the touch slop threshold
            // should be interpreted as a tap-seek to that location.
            onStartTrackingTouch();
            trackTouchEvent(event);
            onStopTrackingTouch();
        }

        if (listener != null) {
            listener.onRangeSeekBarValueChanged(this,
                    Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(),
                    pressedThumb);
        }

        pressedThumb = null;
        invalidate();

        break;
    case MotionEvent.ACTION_POINTER_DOWN: {
        final int index = event.getPointerCount() - 1;
        // final int index = ev.getActionIndex();
        mDownMotionX = event.getX(index);
        mActivePointerId = event.getPointerId(index);
        invalidate();
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(event);
        invalidate();
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsDragging) {
            onStopTrackingTouch();
            setPressed(false);
        }
        invalidate(); // see above explanation
        break;
    }
    return true;
}

From source file:com.pdftron.pdf.tools.Tool.java

public boolean onMove(MotionEvent e1, MotionEvent e2, float x_dist, float y_dist) {
    if (isCreatingAnnotation()) {
        if (e1.getPointerCount() == 2 || e2.getPointerCount() == 2) {
            mAllowTwoFingerScroll = true;
        }// w  w w.j a v  a2s .c  o  m
    } else {
        mAllowTwoFingerScroll = false;
    }

    // Enable page turning (in non-continuous page presentation mode);
    // it is always enabled for pan mode and text-highlighter
    // it is enabled only if scrolled with two fingers in other modes
    if (getMode() == ToolManager.e_pan || getMode() == ToolManager.e_text_select
            || getMode() == ToolManager.e_text_highlighter) {
        mPDFView.setBuiltInPageSlidingEnabled(true);
    } else {
        if (mAllowTwoFingerScroll || mAllowOneFingerScrollWithStylus) {
            mPDFView.setBuiltInPageSlidingEnabled(true);
        } else {
            mPDFView.setBuiltInPageSlidingEnabled(false);
        }
    }

    // showTransientPageNumber();
    return false;
}

From source file:com.inmobi.ultrapush.AnalyzeActivity.java

private void scaleEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_MOVE) {
        xShift0 = INIT;//from w ww .j  a v  a2 s. c  o m
        yShift0 = INIT;
        isPinching = false;
        //      Log.i(TAG, "scaleEvent(): Skip event " + event.getAction());
        return;
    }
    //    Log.i(TAG, "scaleEvent(): switch " + event.getAction());
    switch (event.getPointerCount()) {
    case 2:
        if (isPinching) {
            graphView.setShiftScale(event.getX(0), event.getY(0), event.getX(1), event.getY(1));
        } else {
            graphView.setShiftScaleBegin(event.getX(0), event.getY(0), event.getX(1), event.getY(1));
        }
        isPinching = true;
        break;
    case 1:
        float x = event.getX(0);
        float y = event.getY(0);
        graphView.getLocationInWindow(windowLocation);
        //        Log.i(TAG, "scaleEvent(): xy=" + x + " " + y + "  wc = " + wc[0] + " " + wc[1]);
        if (isPinching || xShift0 == INIT) {
            xShift0 = graphView.getXShift();
            x0 = x;
            yShift0 = graphView.getYShift();
            y0 = y;
        } else {
            // when close to the axis, scroll that axis only
            if (x0 < windowLocation[0] + 50) {
                graphView.setYShift(yShift0 + (y0 - y) / graphView.getCanvasHeight() / graphView.getYZoom());
            } else if (y0 < windowLocation[1] + 50) {
                graphView.setXShift(xShift0 + (x0 - x) / graphView.getCanvasWidth() / graphView.getXZoom());
            } else {
                graphView.setXShift(xShift0 + (x0 - x) / graphView.getCanvasWidth() / graphView.getXZoom());
                graphView.setYShift(yShift0 + (y0 - y) / graphView.getCanvasHeight() / graphView.getYZoom());
            }
        }
        isPinching = false;
        break;
    default:
        Log.v(TAG, "Invalid touch count");
        break;
    }
}

From source file:com.example.piechart3d.PieChart3DView.java

@SuppressLint("NewApi")
@Override//from w w  w .  j a  v a2  s .co  m
public boolean onTouchEvent(MotionEvent event) {
    getParent().requestDisallowInterceptTouchEvent(true);
    float xxx = event.getX() - centerx;
    float yyy = event.getY() - centery;

    {
        mDetector.onTouchEvent(event);
        String direction;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case (MotionEvent.ACTION_DOWN):
            if (mZAnimator != null)
                mZAnimator.cancel();
            iniMAngle = (float) java.lang.Math.atan2(yyy, xxx);
            inix = mRenderer.angle_x;
            iniz = mRenderer.angle_z;
            x1 = event.getX();
            y1 = event.getY();
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            iniMAngle = (float) java.lang.Math.atan2(yyy, xxx);
            Log.d("appAS", "Pointer down");
            inix = mRenderer.angle_x;
            iniz = mRenderer.angle_z;
            d = rotation(event);
            break;
        case MotionEvent.ACTION_POINTER_UP:
            inix = mRenderer.angle_x;
            iniz = mRenderer.angle_z;
            iniMAngle = (float) java.lang.Math.atan2(yyy, xxx);
            break;
        case (MotionEvent.ACTION_MOVE): {
            if (event.getPointerCount() == 1) {
                x2 = event.getX();
                y2 = event.getY();
                dx = x2 - x1;
                dy = y2 - y1;
                if (Math.abs(dx) < Math.abs(dy) && Math.abs(dy) > 25) {
                    iniz = mRenderer.angle_z;
                    if (dy > 0)
                        direction = "down";
                    else
                        direction = "up";
                    this.requestDoRender(inix - (dy / 3), iniz);
                } else {
                    mAngle = (float) java.lang.Math.atan2(yyy, xxx);
                    inix = mRenderer.angle_x;
                    if (dx > 0)
                        direction = "right";
                    else
                        direction = "left";
                    this.requestDoRender(inix, /* iniz + (dx / 3) */
                            iniz + (float) Math.toDegrees((mAngle - iniMAngle)));
                    Log.d("ang", "" + iniz + " " + (iniz + (float) Math.toDegrees((mAngle - iniMAngle))));
                }
            } else if (event.getPointerCount() == 2) {
                Log.d("app", "Rotating " + event.getPointerCount());
                float rot = rotation(event) - d;
                this.requestDoRender(inix, iniz + rot);
            }
        }
        }
    }
    return true;
}

From source file:de.lmu.ifi.medien.probui.system.ProbUIManager.java

public void manageHelper(MotionEvent ev) throws WrongObservationDelegationException {

    this.currentTouchObservations.clear();

    int action = MotionEventCompat.getActionMasked(ev);
    int index = MotionEventCompat.getActionIndex(ev);
    int pointerID = ev.getPointerId(index);

    int type = -1;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        type = ProbObservationTouch.TYPE_TOUCH_DOWN;
        break;//from   ww  w  . j  a v  a 2  s .co m
    case MotionEvent.ACTION_MOVE:
        type = ProbObservationTouch.TYPE_TOUCH_MOVE;
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        type = ProbObservationTouch.TYPE_TOUCH_UP;
        break;
    default:
        type = -1;
        break;
    }

    long timestamp = ev.getEventTime();

    ProbObservationTouch observation = ProbObservationFactory.createTouchObservation(ev.getX(index),
            ev.getY(index), ev.getX(index) * 1.0 / container.getWidth(),
            ev.getY(index) * 1.0 / container.getHeight(), ev.getOrientation(index),
            ev.getTouchMinor(index) * 1.0 / container.getWidth(),
            ev.getTouchMajor(index) * 1.0 / container.getHeight(), ev.getPressure(index), type, pointerID,
            timestamp);
    this.currentTouchObservations.add(observation);

    // Since move is always associated with the first pointer,
    // we need to manually duplicate it for the second one
    // (TODO: and for further pointers, if we change it to more than 2):
    if (ev.getPointerCount() == 2 && type == ProbObservationTouch.TYPE_TOUCH_MOVE) {
        ProbObservationTouch observation2 = ProbObservationFactory.createTouchObservation(ev.getX(index),
                ev.getY(index), ev.getX(1) * 1.0 / container.getWidth(),
                ev.getY(1) * 1.0 / container.getHeight(), ev.getOrientation(1),
                ev.getToolMinor(1) * 1.0 / container.getWidth(),
                ev.getToolMajor(1) * 1.0 / container.getHeight(), ev.getPressure(1), type, ev.getPointerId(1),
                timestamp);
        this.currentTouchObservations.add(observation2);
    }

    //Log.d("MULTITOUCH", "type: " + type + ", index: " + pointerID + ", size: " + ev.getTouchMajor(index) * 1.0 / container.getHeight());

    // Distribute touch observation to the cores of all probInteractors
    // (for reasoning by these interactor cores!, not for visual feedback etc. - that comes below: interactor.onTouchDown etc.)
    boolean passedOn = false;
    for (ProbInteractor interactor : this.probInteractors) {

        for (int i = 0; i < this.currentTouchObservations.size(); i++) {
            ProbObservationTouch obs = this.currentTouchObservations.get(i);
            if (obs == null)
                continue;
            if (obs.getNominalFeatures()[0] != ProbObservationTouch.TYPE_TOUCH_MOVE
                    || this.currentTouchObservations.size() != this.previousTouchObservations.size()) {
                interactor.getCore().onTouchObservation(obs);
                passedOn = true;
            } else { // This code filters out move events that moved very little (potentially improves performance):
                double[] obsXY = this.currentTouchObservations.get(i).getRealFeatures();
                double[] obsPrevXY = this.previousTouchObservations.get(i).getRealFeatures();
                double dx = obsXY[0] - obsPrevXY[0];
                double dy = obsXY[1] - obsPrevXY[1];
                double dist = Math.sqrt(dx * dx + dy * dy);
                if (dist > 0.0125) { // TODO: movement threshold currently hardcoded: 0.0125
                    interactor.getCore().onTouchObservation(obs);
                    passedOn = true;
                } else {
                }
            }
        }

    }

    if (passedOn) {
        this.previousTouchObservations.clear();
        this.previousTouchObservations.addAll(this.currentTouchObservations);
    }

    // Forward the touch observation for probInteractors
    // to react (e.g. visual feedback, triggering actions, nothing to do with the mediation):
    for (ProbInteractor interactor : this.probInteractors) {
        for (ProbObservationTouch obs : this.currentTouchObservations) {
            if (obs != null) {
                switch (obs.getNominalFeatures()[0]) {

                case ProbObservationTouch.TYPE_TOUCH_DOWN:
                    interactor.onTouchDown(obs);
                    break;

                case ProbObservationTouch.TYPE_TOUCH_MOVE:
                    interactor.onTouchMove(obs);
                    break;

                case ProbObservationTouch.TYPE_TOUCH_UP:
                    interactor.onTouchUp(obs, ev.getPointerCount() - 1);
                    break;
                default:
                    break;
                }
            }
        }
    }

    // If no element is determined yet (i.e. no decision yet), update the reasoning process.
    if (!isOneDetermined() && passedOn) {
        this.mediator.mediate(false);
    }

    // Post mediation: Forward the touch observation again
    // to the post-mediation versions of the onTouch... methods
    for (ProbInteractor interactor : this.probInteractors) {
        for (ProbObservationTouch obs : this.currentTouchObservations) {
            if (obs != null) {
                switch (obs.getNominalFeatures()[0]) {

                case ProbObservationTouch.TYPE_TOUCH_DOWN:
                    interactor.onTouchDownPost(obs);
                    break;

                case ProbObservationTouch.TYPE_TOUCH_MOVE:
                    interactor.onTouchMovePost(obs);
                    break;

                case ProbObservationTouch.TYPE_TOUCH_UP:
                    interactor.onTouchUpPost(obs, ev.getPointerCount() - 1);
                    break;
                default:
                    break;
                }
            }
        }
    }

    // Pass on to other GUI elements:
    if (!isOneDetermined()) {
        for (View view : this.nonProbInteractors) {
            if (view.isFocusable() && view.isEnabled())
                view.onTouchEvent(ev);
        }
    }
}

From source file:com.mishiranu.dashchan.ui.navigator.DrawerForm.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        multipleFingersCountingTime = false;
        multipleFingersStartTime = 0L;//  w  w  w  . j a v a 2 s  . co  m
        multipleFingersTime = 0L;
        break;
    }
    case MotionEvent.ACTION_POINTER_DOWN: {
        if (!multipleFingersCountingTime) {
            multipleFingersCountingTime = true;
            multipleFingersStartTime = System.currentTimeMillis();
        }
        break;
    }
    case MotionEvent.ACTION_POINTER_UP: {
        if (event.getPointerCount() <= 2) {
            if (multipleFingersCountingTime) {
                multipleFingersCountingTime = false;
                multipleFingersTime += System.currentTimeMillis() - multipleFingersStartTime;
            }
        }
        break;
    }
    }
    return false;
}

From source file:keyboard.ecloga.com.eclogakeyboard.EclogaKeyboard.java

@Override
public boolean onTouch(View v, final MotionEvent event) {
    mGestureDetector.onTouchEvent(event);

    if (event.getPointerCount() > 1) {
        return true;
    }//from   ww w  .j  a  v  a  2  s .c om

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mDownX = event.getX();
        mDownY = event.getY();
        isOnClick = true;
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (isOnClick) {
            swipe = false;
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (isOnClick && (Math.abs(mDownX - event.getX()) > 120 || Math.abs(mDownY - event.getY()) > 120)) {
            swipe = true;
            isOnClick = false;
        }
        break;
    default:
        break;
    }

    return false;
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    int action = event.getAction();
    action = action & MotionEvent.ACTION_MASK;

    mGestureDetector.onTouchEvent(event);
    boolean scaleInProgress = scaleInProgress();
    mScaleGestureDetector.onTouchEvent(event);
    if (mInteractionMode == InteractionMode.SCALE) {
        return true;
    }/*from w w w  .  j a  va 2  s  .  co  m*/
    if (!scaleInProgress() && scaleInProgress) {
        // If we were scaling, the scale will stop but we will
        // still issue an ACTION_UP. Let the subclasses know.
        mFinishedScalingOperation = true;
    }

    int ex = (int) event.getX();
    int ey = (int) event.getY();
    if (action == MotionEvent.ACTION_DOWN) {
        mInteractionMode = InteractionMode.MOVE;
        mTouchDown.x = ex;
        mTouchDown.y = ey;
        mTouchShowOriginalDate = System.currentTimeMillis();
        mShowOriginalDirection = 0;
        MasterImage.getImage().setOriginalTranslation(MasterImage.getImage().getTranslation());
    }

    if (action == MotionEvent.ACTION_MOVE && mInteractionMode == InteractionMode.MOVE) {
        mTouch.x = ex;
        mTouch.y = ey;

        float scaleFactor = MasterImage.getImage().getScaleFactor();
        if (scaleFactor > 1 && (!ENABLE_ZOOMED_COMPARISON || event.getPointerCount() == 2)) {
            float translateX = (mTouch.x - mTouchDown.x) / scaleFactor;
            float translateY = (mTouch.y - mTouchDown.y) / scaleFactor;
            Point originalTranslation = MasterImage.getImage().getOriginalTranslation();
            Point translation = MasterImage.getImage().getTranslation();
            translation.x = (int) (originalTranslation.x + translateX);
            translation.y = (int) (originalTranslation.y + translateY);
            MasterImage.getImage().setTranslation(translation);
            mTouchShowOriginal = false;
        } else if (enableComparison() && !mOriginalDisabled
                && (System.currentTimeMillis() - mTouchShowOriginalDate > mTouchShowOriginalDelayMin)
                && event.getPointerCount() == 1) {
            mTouchShowOriginal = true;
        }
    }

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL
            || action == MotionEvent.ACTION_OUTSIDE) {
        mInteractionMode = InteractionMode.NONE;
        mTouchShowOriginal = false;
        mTouchDown.x = 0;
        mTouchDown.y = 0;
        mTouch.x = 0;
        mTouch.y = 0;
        if (MasterImage.getImage().getScaleFactor() <= 1) {
            MasterImage.getImage().setScaleFactor(1);
            MasterImage.getImage().resetTranslation();
        }
    }

    float scaleFactor = MasterImage.getImage().getScaleFactor();
    Point translation = MasterImage.getImage().getTranslation();
    constrainTranslation(translation, scaleFactor);
    MasterImage.getImage().setTranslation(translation);

    invalidate();
    return true;
}