Example usage for android.view MotionEvent getHistoricalEventTime

List of usage examples for android.view MotionEvent getHistoricalEventTime

Introduction

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

Prototype

public final long getHistoricalEventTime(int pos) 

Source Link

Document

Returns the time that a historical movement occurred between this event and the previous event, in the android.os.SystemClock#uptimeMillis time base.

Usage

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

@SuppressLint("NewApi")
@Override/*from  ww  w .  j a  v a2s.c om*/
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;
}