Example usage for android.view MotionEvent getPressure

List of usage examples for android.view MotionEvent getPressure

Introduction

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

Prototype

public final float getPressure(int pointerIndex) 

Source Link

Document

Returns the current pressure of this event for the given pointer index (use #getPointerId(int) to find the pointer identifier for this index).

Usage

From source file:Main.java

public static float getPressure(MotionEvent event, int index) {
    final int historySize = event.getHistorySize();
    return historySize == 0 ? event.getPressure(index) : event.getHistoricalPressure(index, historySize - 1);
}

From source file:it.sephiroth.android.library.imagezoom.ScaleGestureDetector.java

private void setContext(final MotionEvent curr) {
    if (mCurrEvent != null) {
        mCurrEvent.recycle();//from  w  w w .j a va2s .co  m
    }
    mCurrEvent = MotionEvent.obtain(curr);

    mCurrLen = -1;
    mPrevLen = -1;
    mScaleFactor = -1;

    final MotionEvent prev = mPrevEvent;

    final float px0 = prev.getX(0);
    final float py0 = prev.getY(0);
    final float px1 = prev.getX(1);
    final float py1 = prev.getY(1);
    final float cx0 = curr.getX(0);
    final float cy0 = curr.getY(0);
    final float cx1 = curr.getX(1);
    final float cy1 = curr.getY(1);

    final float pvx = px1 - px0;
    final float pvy = py1 - py0;
    final float cvx = cx1 - cx0;
    final float cvy = cy1 - cy0;
    mPrevFingerDiffX = pvx;
    mPrevFingerDiffY = pvy;
    mCurrFingerDiffX = cvx;
    mCurrFingerDiffY = cvy;

    mFocusX = cx0 + cvx * 0.5f;
    mFocusY = cy0 + cvy * 0.5f;
    mTimeDelta = curr.getEventTime() - prev.getEventTime();
    mCurrPressure = curr.getPressure(0) + curr.getPressure(1);
    mPrevPressure = prev.getPressure(0) + prev.getPressure(1);
}

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;/*  w  w w  .  ja 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.mylikes.likes.etchasketch.Slate.java

@SuppressLint("NewApi")
@Override// w ww .  jav a2 s.c  o m
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;
}