Example usage for android.view MotionEvent getPointerCoords

List of usage examples for android.view MotionEvent getPointerCoords

Introduction

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

Prototype

public final void getPointerCoords(int pointerIndex, PointerCoords outPointerCoords) 

Source Link

Document

Populates a PointerCoords object with pointer coordinate data for the specified pointer index.

Usage

From source file:Main.java

private static PointerCoords[] getPointerCoords(MotionEvent e) {
    int n = e.getPointerCount();
    PointerCoords[] r = new PointerCoords[n];
    for (int i = 0; i < n; i++) {
        r[i] = new PointerCoords();
        e.getPointerCoords(i, r[i]);
    }/*w  w  w  .j ava 2 s  .  c o  m*/
    return r;
}

From source file:Main.java

private static PointerCoords[] getPointerCoords(final MotionEvent e) {
    final int n = e.getPointerCount();
    final PointerCoords[] r = new PointerCoords[n];
    for (int i = 0; i < n; i++) {
        r[i] = new PointerCoords();
        e.getPointerCoords(i, r[i]);
    }/*from www. j a v  a 2s.c  om*/
    return r;
}

From source file:Main.java

private static MotionEvent.PointerCoords[] getPointerCoords(final MotionEvent motionEvent) {
    final int pointerCount = motionEvent.getPointerCount();
    final MotionEvent.PointerCoords[] array = new MotionEvent.PointerCoords[pointerCount];
    for (int i = 0; i < pointerCount; ++i) {
        motionEvent.getPointerCoords(i, array[i] = new MotionEvent.PointerCoords());
    }//from w w  w.  ja v a 2  s.co  m
    return array;
}

From source file:Main.java

public static JSONObject CreateJSonObjectFromMotionEvent(MotionEvent e) throws JSONException {
    JSONObject jObj = new JSONObject();
    jObj.put("downTime", e.getDownTime());
    jObj.put("eventTime", e.getEventTime());
    jObj.put("action", e.getAction());
    jObj.put("pointerCount", e.getPointerCount());
    jObj.put("metaState", e.getMetaState());
    jObj.put("buttonState", e.getButtonState());
    jObj.put("xPrecision", e.getXPrecision());
    jObj.put("yPrecision", e.getYPrecision());
    jObj.put("deviceId", e.getDeviceId());
    jObj.put("edgeFlags", e.getEdgeFlags());
    jObj.put("source", e.getSource());
    jObj.put("flags", e.getFlags());

    for (int i = 0; i < e.getPointerCount(); i++) {
        PointerProperties prop = new PointerProperties();
        e.getPointerProperties(i, prop);

        PointerCoords coords = new PointerCoords();
        e.getPointerCoords(i, coords);

        JSONObject pointer = JObjFromPointer(prop, coords);
        jObj.accumulate("pointers", pointer);
    }/*from   w  w w  .  j  ava2s.  co  m*/

    return jObj;
}

From source file:com.amazon.appstream.sampleclient.SampleClientActivity.java

/**
 * A "generic motion event" includes joystick and mouse motion
 * when the mouse button isn't down. In our simple sample, we're
 * not handling the joystick, but this is where any such code
 * would live.//from   ww  w. j  a  va2  s . c o m
 *
 * This will only ever be called in HONEYCOMB_MR1 (12) or later, so I'm marking the
 * function using \@TargetApi to allow it to call the super.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
    if (event.getSource() == InputDevice.SOURCE_MOUSE) {
        event.getPointerCoords(0, mCoordHolder);
        switch (event.getAction()) {
        case MotionEvent.ACTION_HOVER_MOVE:
            AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y, 0);
            break;

        default:
            return super.dispatchGenericMotionEvent(event);
        }
        return true;
    }
    return super.dispatchGenericMotionEvent(event);
}

From source file:com.amazon.appstream.fireclient.FireClientActivity.java

/**
 * A "generic motion event" includes joystick and mouse motion
 * when the mouse button isn't down. In our simple sample, we're
 * not handling the joystick, but this is where any such code
 * would live./* w w w .  j a v a  2  s. c om*/
 *
 * This will only ever be called in HONEYCOMB_MR1 (12) or later, so I'm marking the
 * function using \@TargetApi to allow it to call the super.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
    if (event.getSource() == InputDevice.SOURCE_MOUSE) {
        event.getPointerCoords(0, mCoordHolder);
        switch (event.getAction()) {
        case MotionEvent.ACTION_HOVER_MOVE:
            AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y, 0);
            break;

        default:
            return super.dispatchGenericMotionEvent(event);
        }
        return true;
    } else if (event.getSource() == InputDevice.SOURCE_JOYSTICK) {
        Log.v(TAG, "Joystick event:" + event.toString());

        if (!mInitializedJoystick) {
            mInitializedJoystick = true;

            InputDevice joystick = InputDevice.getDevice(event.getDeviceId());
            InputDevice.MotionRange lThumbX = joystick.getMotionRange(MotionEvent.AXIS_X,
                    InputDevice.SOURCE_JOYSTICK);
            InputDevice.MotionRange lThumbY = joystick.getMotionRange(MotionEvent.AXIS_Y,
                    InputDevice.SOURCE_JOYSTICK);
            InputDevice.MotionRange rThumbX = joystick.getMotionRange(MotionEvent.AXIS_Z);
            InputDevice.MotionRange rThumbY = joystick.getMotionRange(MotionEvent.AXIS_RZ);
            InputDevice.MotionRange rTrigger = joystick.getMotionRange(MotionEvent.AXIS_GAS);
            if (rTrigger == null) {
                rTrigger = joystick.getMotionRange(MotionEvent.AXIS_RTRIGGER);
                mRTrigger = MotionEvent.AXIS_RTRIGGER;
            }

            InputDevice.MotionRange lTrigger = joystick.getMotionRange(MotionEvent.AXIS_BRAKE);
            if (lTrigger == null) {
                lTrigger = joystick.getMotionRange(MotionEvent.AXIS_LTRIGGER);
                mLTrigger = MotionEvent.AXIS_LTRIGGER;
            }

            List<InputDevice.MotionRange> ranges = joystick.getMotionRanges();

            InputDevice.MotionRange dPad = null;
            String name = joystick.getName();

            /*
            The Amazon Fire Game Controller follows the NVidia standard of sending
            AXIS_HAT_X/AXIS_HAT_Y results when the user hits the D-Pad. Only if we
            return false from dispatchGenericMotionEvent() will it then send
            DPAD keycodes.
                    
            But the most popular Android joystick on the market at this time, the
            Nyko Playpad Pro, returns AXIS_HAT_X/AXIS_HAT_Y results when the left
            analog stick hits its extremes, meaning that the analog stick will
            generate DPAD keys if we return false. The Nyko generates DPAD keys
            directly for the DPAD controller.
                    
            So we have two incompatible standards fighting with each other. Probably
            the safest thing to do would be to ask the user to press on their DPAD
            and see what messages we get, but that is beyond the scope of this
            example code.
            */
            if (name.equals("Amazon Fire Game Controler")) {
                for (int i = 0; i < ranges.size(); ++i) {
                    InputDevice.MotionRange range = ranges.get(i);
                    int axis = range.getAxis();

                    if (axis == MotionEvent.AXIS_HAT_X || axis == MotionEvent.AXIS_HAT_Y) {
                        dPad = ranges.get(i);
                        break;
                    }
                }
            }

            JoystickHelper.joystickDeadZones(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, dPad);
        }

        float lThumbX = event.getAxisValue(MotionEvent.AXIS_X);
        float lThumbY = -event.getAxisValue(MotionEvent.AXIS_Y);
        float rThumbX = event.getAxisValue(MotionEvent.AXIS_Z);
        float rThumbY = -event.getAxisValue(MotionEvent.AXIS_RZ);
        float lTrigger = event.getAxisValue(mLTrigger);
        float rTrigger = event.getAxisValue(mRTrigger);
        float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
        float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);

        JoystickHelper.setJoystickState(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, hatX, hatY);
        return true;
    }

    return super.dispatchGenericMotionEvent(event);
}

From source file:com.amazon.appstream.fireclient.FireClientActivity.java

/**
 * A "touch event" includes mouse motion when the mouse button
 * is down.//from   w  ww .j  a  va  2 s .c o m
 */
@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    if (mKeyboardActive) {
        if (mGestureDetector.onTouchEvent(event)) {
            return true;
        }
    }

    if (super.dispatchTouchEvent(event))
        return true;

    int flags = 0;
    if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {
        flags = AppStreamInterface.CET_TOUCH_FLAG;
    }

    event.getPointerCoords(0, mCoordHolder);
    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE:
        AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y, flags);
        break;
    case MotionEvent.ACTION_DOWN:
        AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y,
                AppStreamInterface.CET_MOUSE_1_DOWN | flags);
        break;
    case MotionEvent.ACTION_UP:
        AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y,
                AppStreamInterface.CET_MOUSE_1_UP | flags);
        break;
    }
    return true;
}

From source file:com.juick.android.ThreadFragment.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public boolean onTouch(View view, MotionEvent event) {
    if (parent.onListTouchEvent(view, event))
        return true;
    if (parent.isFinishing())
        return true;
    if (mScaleDetector != null) {
        try {//from   w  w  w  .j ava2  s.  c om
            mScaleDetector.onTouchEvent(event);
        } catch (Exception e) {
            // shit happens there inside
        }
    }
    try {
        MotionEvent.PointerCoords pc = new MotionEvent.PointerCoords();
        event.getPointerCoords(0, pc);
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (navMenu != null && navMenu.getVisibility() == View.VISIBLE) {
                int[] listViewLocation = new int[2];
                int[] imageLocation = new int[2];
                getListView().getLocationOnScreen(listViewLocation);
                navMenu.getLocationOnScreen(imageLocation);
                imageLocation[0] += navMenu.initialTranslationX;
                float touchX = pc.x + listViewLocation[0] - imageLocation[0];
                float touchY = pc.y + listViewLocation[1] - imageLocation[1];
                System.out.println("TOUCH: ACTION_DOWN: x=" + pc.x + " y=" + pc.y);
                if (touchX > -20 && touchX < navMenu.getWidth() && touchY > 0
                        && touchY < navMenu.getHeight() * 1.5) { // extra Y pixels due to picture not balanced
                    touchOriginX = pc.x;
                    touchOriginY = pc.y;
                    System.out.println("TOUCH: OK TOUCH NAVMENU");
                    return true;
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            if (!ignoreMove && !isNavigationMenuShown())
                resetMainMenuButton(false);
            if (touchOriginX > 0 || ignoreMove) {
                touchOriginX = -1;
                ignoreMove = false;
                return true;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (ignoreMove || navMenu == null)
                return true;
            event.getPointerCoords(0, pc);
            double travelledDistance = Math
                    .sqrt(Math.pow(touchOriginX - pc.x, 2) + Math.pow(touchOriginY - pc.y, 2));
            boolean inZone = false;
            if (!isNavigationMenuShown()) {
                if (touchOriginX >= 0) {
                    // detect angle where finger moves
                    if (travelledDistance < 10) { // grace period
                        inZone = true;
                    } else {
                        float dx = Math.abs(touchOriginX - pc.x);
                        float dy = Math.abs(touchOriginY - pc.y);
                        if (dx > dy) {
                            // movement in 45 degree zone
                            if (touchOriginX > pc.x) {
                                // towards left
                                inZone = true;
                                double neededDistance = 1.5 / 2.54 * getResources().getDisplayMetrics().xdpi;
                                if (travelledDistance > neededDistance) {
                                    // moved 1.5 centimeters
                                    System.out.println("TOUCH: OPEN MENU");
                                    ignoreMove = true;
                                    openNavigationMenu(pc.x - touchOriginX + initNavMenuTranslationX);
                                    touchOriginX = -1;
                                }
                            }
                        } else {
                            System.out.println("TOUCH: LEAVING ZONE: dx=" + dx + " dy=" + dy);
                        }
                    }
                    if (inZone && !ignoreMove) {
                        TranslateAnimation immediate = new TranslateAnimation(Animation.ABSOLUTE,
                                pc.x - touchOriginX + initNavMenuTranslationX, Animation.ABSOLUTE,
                                pc.x - touchOriginX + initNavMenuTranslationX, Animation.ABSOLUTE, 0,
                                Animation.ABSOLUTE, 0);
                        immediate.setDuration(5);
                        immediate.setFillAfter(true);
                        immediate.setFillBefore(true);
                        immediate.setFillEnabled(true);
                        navMenu.startAnimation(immediate);
                    }
                }
                if (!inZone) {
                    resetMainMenuButton(false);
                    if (touchOriginX >= 0) {
                        System.out.println("TOUCH: ACTION_MOVE: x=" + pc.x + " y=" + pc.y);
                        System.out.println("TOUCH: LEFT ZONE");
                        touchOriginX = -1;
                    }
                }
                if (inZone) {
                    return true;
                }
                if (doOnClick != null || ignoreMove) {
                    return true;
                }
            }
            break;
        }
    } catch (NoClassDefFoundError err) {
        // so be it.
    }
    return false;
}

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

@SuppressLint("NewApi")
@Override/*w  w w .j  a  v a  2 s .  co  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;
}