Example usage for android.view MotionEvent getHistorySize

List of usage examples for android.view MotionEvent getHistorySize

Introduction

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

Prototype

public final int getHistorySize() 

Source Link

Document

Returns the number of historical points in this event.

Usage

From source file:com.klinker.deskclock.widget.multiwaveview.GlowPadView.java

private void handleMove(MotionEvent event) {
    int activeTarget = -1;
    final int historySize = event.getHistorySize();
    ArrayList<TargetDrawable> targets = mTargetDrawables;
    int ntargets = targets.size();
    float x = 0.0f;
    float y = 0.0f;
    int actionIndex = event.findPointerIndex(mPointerId);

    if (actionIndex == -1) {
        return; // no data for this pointer
    }//from w  w w  .jav a  2s  . c o m

    for (int k = 0; k < historySize + 1; k++) {
        float eventX = k < historySize ? event.getHistoricalX(actionIndex, k) : event.getX(actionIndex);
        float eventY = k < historySize ? event.getHistoricalY(actionIndex, k) : event.getY(actionIndex);
        // tx and ty are relative to wave center
        float tx = eventX - mWaveCenterX;
        float ty = eventY - mWaveCenterY;
        float touchRadius = (float) Math.sqrt(dist2(tx, ty));
        final float scale = touchRadius > mOuterRadius ? mOuterRadius / touchRadius : 1.0f;
        float limitX = tx * scale;
        float limitY = ty * scale;
        double angleRad = Math.atan2(-ty, tx);

        if (!mDragging) {
            trySwitchToFirstTouchState(eventX, eventY);
        }

        if (mDragging) {
            // For multiple targets, snap to the one that matches
            final float snapRadius = mOuterRadius - mSnapMargin;
            final float snapDistance2 = snapRadius * snapRadius;
            // Find first target in range
            for (int i = 0; i < ntargets; i++) {
                TargetDrawable target = targets.get(i);

                double targetMinRad = (i - 0.5) * 2 * Math.PI / ntargets;
                double targetMaxRad = (i + 0.5) * 2 * Math.PI / ntargets;
                if (target.isEnabled()) {
                    boolean angleMatches = (angleRad > targetMinRad && angleRad <= targetMaxRad)
                            || (angleRad + 2 * Math.PI > targetMinRad
                                    && angleRad + 2 * Math.PI <= targetMaxRad);
                    if (angleMatches && (dist2(tx, ty) > snapDistance2)) {
                        activeTarget = i;
                    }
                }
            }
        }
        x = limitX;
        y = limitY;
    }

    if (!mDragging) {
        return;
    }

    if (activeTarget != -1) {
        switchToState(STATE_SNAP, x, y);
        updateGlowPosition(x, y);
    } else {
        switchToState(STATE_TRACKING, x, y);
        updateGlowPosition(x, y);
    }

    if (mActiveTarget != activeTarget) {
        // Defocus the old target
        if (mActiveTarget != -1) {
            TargetDrawable target = targets.get(mActiveTarget);
            target.setState(TargetDrawable.STATE_INACTIVE);
        }
        // Focus the new target
        if (activeTarget != -1) {
            TargetDrawable target = targets.get(activeTarget);
            target.setState(TargetDrawable.STATE_FOCUSED);
            final AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
                    .getSystemService(Context.ACCESSIBILITY_SERVICE);
            if (accessibilityManager.isEnabled()) {
                String targetContentDescription = getTargetDescription(activeTarget);
                if (Build.VERSION.SDK_INT >= 16) {
                    announceForAccessibility(targetContentDescription);
                } else {
                    AccessibilityEvent acc_event = AccessibilityEvent
                            .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
                    AccessibilityRecordCompat arc = new AccessibilityRecordCompat(acc_event);
                    arc.setSource(this);
                    acc_event.setClassName(this.getClass().getName());
                    acc_event.setPackageName(this.getContext().getPackageName());
                    acc_event.setEnabled(this.isEnabled());
                    acc_event.getText().add(targetContentDescription);
                    accessibilityManager.sendAccessibilityEvent(acc_event);
                }
            }
        }
    }
    mActiveTarget = activeTarget;
}

From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java

private void handleMove(MotionEvent event) {
    int activeTarget = -1;
    final int historySize = event.getHistorySize();
    ArrayList<TargetDrawable> targets = mTargetDrawables;
    int ntargets = targets.size();
    float x = 0.0f;
    float y = 0.0f;
    int actionIndex = event.findPointerIndex(mPointerId);

    if (actionIndex == -1) {
        return; // no data for this pointer
    }/*from   w w w  .  j a  v a  2s .  c om*/

    for (int k = 0; k < historySize + 1; k++) {
        float eventX = k < historySize ? event.getHistoricalX(actionIndex, k) : event.getX(actionIndex);
        float eventY = k < historySize ? event.getHistoricalY(actionIndex, k) : event.getY(actionIndex);
        // tx and ty are relative to wave center
        float tx = eventX - mWaveCenterX;
        float ty = eventY - mWaveCenterY;
        float touchRadius = (float) Math.hypot(tx, ty);
        final float scale = touchRadius > mOuterRadius ? mOuterRadius / touchRadius : 1.0f;
        float limitX = tx * scale;
        float limitY = ty * scale;
        double angleRad = Math.atan2(-ty, tx);

        if (!mDragging) {
            trySwitchToFirstTouchState(eventX, eventY);
        }

        if (mDragging) {
            // For multiple targets, snap to the one that matches
            final float snapRadius = mRingScaleFactor * mOuterRadius - mSnapMargin;
            final float snapDistance2 = snapRadius * snapRadius;
            // Find first target in range
            for (int i = 0; i < ntargets; i++) {
                TargetDrawable target = targets.get(i);

                double targetMinRad = (i - 0.5) * 2 * Math.PI / ntargets;
                double targetMaxRad = (i + 0.5) * 2 * Math.PI / ntargets;
                if (target.isEnabled()) {
                    boolean angleMatches = (angleRad > targetMinRad && angleRad <= targetMaxRad)
                            || (angleRad + 2 * Math.PI > targetMinRad
                                    && angleRad + 2 * Math.PI <= targetMaxRad);
                    if (angleMatches && (dist2(tx, ty) > snapDistance2)) {
                        activeTarget = i;
                    }
                }
            }
        }
        x = limitX;
        y = limitY;
    }

    if (!mDragging) {
        return;
    }

    if (activeTarget != -1) {
        switchToState(STATE_SNAP, x, y);
        updateGlowPosition(x, y);
    } else {
        switchToState(STATE_TRACKING, x, y);
        updateGlowPosition(x, y);
    }

    if (mActiveTarget != activeTarget) {
        // Defocus the old target
        if (mActiveTarget != -1) {
            TargetDrawable target = targets.get(mActiveTarget);
            target.setState(TargetDrawable.STATE_INACTIVE);
        }
        // Focus the new target
        if (activeTarget != -1) {
            TargetDrawable target = targets.get(activeTarget);
            target.setState(TargetDrawable.STATE_FOCUSED);
            final AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
                    .getSystemService(Context.ACCESSIBILITY_SERVICE);
            if (accessibilityManager.isEnabled()) {
                String targetContentDescription = getTargetDescription(activeTarget);
                announceForAccessibility(targetContentDescription);
            }
        }
    }
    mActiveTarget = activeTarget;
}

From source file:com.example.androidannotationtesttwo.widget.swiptlistview.SwipeListView.java

/**
 * adjust header padding according to motion event
 * /* w w  w. j  av a  2s.  c o  m*/
 * @param ev
 */
private void adjustHeaderPadding(MotionEvent ev) {
    // adjust header padding according to motion event history
    int pointerCount = ev.getHistorySize();
    if (isVerticalFadingEdgeEnabled()) {
        setVerticalScrollBarEnabled(false);
    }
    for (int i = 0; i < pointerCount; i++) {
        if (currentHeaderStatus == HEADER_STATUS_DROP_DOWN_TO_LOAD
                || currentHeaderStatus == HEADER_STATUS_RELEASE_TO_LOAD) {
            headerLayout.setPadding(headerLayout.getPaddingLeft(),
                    (int) (((ev.getHistoricalY(i) - actionDownPointY) - headerOriginalHeight)
                            / headerPaddingTopRate),
                    headerLayout.getPaddingRight(), headerLayout.getPaddingBottom());
        }
    }
}

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

@SuppressLint("NewApi")
@Override/*from www .  ja  v a  2 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;
}

From source file:com.rexmtorres.android.patternlock.PatternLockView.java

private void handleActionMove(MotionEvent event) {
    // Handle all recent motion events so we don't skip any cells even when the device
    // is busy...
    final float radius = mPathWidth;
    final int historySize = event.getHistorySize();
    mTmpInvalidateRect.setEmpty();/*from w  w w  . j ava 2 s.  c o m*/
    boolean invalidateNow = false;
    for (int i = 0; i < historySize + 1; i++) {
        final float x = i < historySize ? event.getHistoricalX(i) : event.getX();
        final float y = i < historySize ? event.getHistoricalY(i) : event.getY();
        Cell hitCell = detectAndAddHit(x, y);
        final int patternSize = mPattern.size();
        if (hitCell != null && patternSize == 1) {
            setPatternInProgress(true);
            notifyPatternStarted();
        }
        // note current x and y for rubber banding of in progress patterns
        final float dx = Math.abs(x - mInProgressX);
        final float dy = Math.abs(y - mInProgressY);
        if (dx > DRAG_THRESHHOLD || dy > DRAG_THRESHHOLD) {
            invalidateNow = true;
        }
        if (mPatternInProgress && patternSize > 0) {
            final ArrayList<Cell> pattern = mPattern;
            final Cell lastCell = pattern.get(patternSize - 1);
            float lastCellCenterX = getCenterXForColumn(lastCell.column);
            float lastCellCenterY = getCenterYForRow(lastCell.row);
            // Adjust for drawn segment from last cell to (x,y). Radius accounts for line width.
            float left = Math.min(lastCellCenterX, x) - radius;
            float right = Math.max(lastCellCenterX, x) + radius;
            float top = Math.min(lastCellCenterY, y) - radius;
            float bottom = Math.max(lastCellCenterY, y) + radius;
            // Invalidate between the pattern's new cell and the pattern's previous cell
            if (hitCell != null) {
                final float width = mSquareWidth * 0.5f;
                final float height = mSquareHeight * 0.5f;
                final float hitCellCenterX = getCenterXForColumn(hitCell.column);
                final float hitCellCenterY = getCenterYForRow(hitCell.row);
                left = Math.min(hitCellCenterX - width, left);
                right = Math.max(hitCellCenterX + width, right);
                top = Math.min(hitCellCenterY - height, top);
                bottom = Math.max(hitCellCenterY + height, bottom);
            }
            // Invalidate between the pattern's last cell and the previous location
            mTmpInvalidateRect.union(Math.round(left), Math.round(top), Math.round(right), Math.round(bottom));
        }
    }
    mInProgressX = event.getX();
    mInProgressY = event.getY();
    // To save updates, we only invalidate if the user moved beyond a certain amount.
    if (invalidateNow) {
        mInvalidate.union(mTmpInvalidateRect);
        invalidate(mInvalidate);
        mInvalidate.set(mTmpInvalidateRect);
    }
}

From source file:io.authme.sdk.widget.LockPatternView.java

private void handleActionMove(MotionEvent event) {
    // Handle all recent motion events so we don't skip any cells even when
    // the device
    // is busy...
    final float radius = mPathWidth;
    final int historySize = event.getHistorySize();
    mTmpInvalidateRect.setEmpty();/*w w  w. ja v  a 2 s. c om*/
    boolean invalidateNow = false;
    for (int i = 0; i < historySize + 1; i++) {
        final float x = i < historySize ? event.getHistoricalX(i) : event.getX();
        final float y = i < historySize ? event.getHistoricalY(i) : event.getY();
        Cell hitCell = detectAndAddHit(x, y);
        final int patternSize = mPattern.size();
        if (hitCell != null && patternSize == 1) {
            mPatternInProgress = true;
            notifyPatternStarted();
        }
        // note current x and y for rubber banding of in progress patterns
        final float dx = Math.abs(x - mInProgressX);
        final float dy = Math.abs(y - mInProgressY);
        if (dx > DRAG_THRESHHOLD || dy > DRAG_THRESHHOLD) {
            invalidateNow = true;
        }

        if (mPatternInProgress && patternSize > 0) {
            final ArrayList<Cell> pattern = mPattern;
            final Cell lastCell = pattern.get(patternSize - 1);
            float lastCellCenterX = getCenterXForColumn(lastCell.column);
            float lastCellCenterY = getCenterYForRow(lastCell.row);

            // Adjust for drawn segment from last cell to (x,y). Radius
            // accounts for line width.
            float left = Math.min(lastCellCenterX, x) - radius;
            float right = Math.max(lastCellCenterX, x) + radius;
            float top = Math.min(lastCellCenterY, y) - radius;
            float bottom = Math.max(lastCellCenterY, y) + radius;

            // Invalidate between the pattern's new cell and the pattern's
            // previous cell
            if (hitCell != null) {
                final float width = mSquareWidth * 0.5f;
                final float height = mSquareHeight * 0.5f;
                final float hitCellCenterX = getCenterXForColumn(hitCell.column);
                final float hitCellCenterY = getCenterYForRow(hitCell.row);

                left = Math.min(hitCellCenterX - width, left);
                right = Math.max(hitCellCenterX + width, right);
                top = Math.min(hitCellCenterY - height, top);
                bottom = Math.max(hitCellCenterY + height, bottom);
            }

            // Invalidate between the pattern's last cell and the previous
            // location
            mTmpInvalidateRect.union(Math.round(left), Math.round(top), Math.round(right), Math.round(bottom));
        }
    }
    mInProgressX = event.getX();
    mInProgressY = event.getY();

    // To save updates, we only invalidate if the user moved beyond a
    // certain amount.
    if (invalidateNow) {
        mInvalidate.union(mTmpInvalidateRect);
        invalidate(mInvalidate);
        mInvalidate.set(mTmpInvalidateRect);
    }
}

From source file:com.android.nobug.view.pattern.PatternView.java

private void handleActionMove(MotionEvent event) {

    // Handle all recent motion events so we don't skip any cells even when the device
    // is busy...
    final float radius = mPathWidth;
    final int historySize = event.getHistorySize();
    mTmpInvalidateRect.setEmpty();//from   www  .  ja v  a  2 s. c  o m
    boolean invalidateNow = false;
    for (int i = 0; i < historySize + 1; i++) {
        final float x = i < historySize ? event.getHistoricalX(i) : event.getX();
        final float y = i < historySize ? event.getHistoricalY(i) : event.getY();
        Cell hitCell = detectAndAddHit(x, y);
        final int patternSize = mPattern.size();
        if (hitCell != null && patternSize == 1) {
            setPatternInProgress(true);
            notifyPatternStarted();
        }
        // note current x and y for rubber banding of in progress patterns
        final float dx = Math.abs(x - mInProgressX);
        final float dy = Math.abs(y - mInProgressY);
        if (dx > DRAG_THRESHOLD || dy > DRAG_THRESHOLD) {
            invalidateNow = true;
        }

        if (mPatternInProgress && patternSize > 0) {
            final ArrayList<Cell> pattern = mPattern;
            final Cell lastCell = pattern.get(patternSize - 1);
            float lastCellCenterX = getCenterXForColumn(lastCell.column);
            float lastCellCenterY = getCenterYForRow(lastCell.row);

            // Adjust for drawn segment from last cell to (x,y). Radius accounts for line width.
            float left = Math.min(lastCellCenterX, x) - radius;
            float right = Math.max(lastCellCenterX, x) + radius;
            float top = Math.min(lastCellCenterY, y) - radius;
            float bottom = Math.max(lastCellCenterY, y) + radius;

            // Invalidate between the pattern's new cell and the pattern's previous cell
            if (hitCell != null) {
                final float width = mSquareWidth * 0.5f;
                final float height = mSquareHeight * 0.5f;
                final float hitCellCenterX = getCenterXForColumn(hitCell.column);
                final float hitCellCenterY = getCenterYForRow(hitCell.row);

                left = Math.min(hitCellCenterX - width, left);
                right = Math.max(hitCellCenterX + width, right);
                top = Math.min(hitCellCenterY - height, top);
                bottom = Math.max(hitCellCenterY + height, bottom);
            }

            // Invalidate between the pattern's last cell and the previous location
            mTmpInvalidateRect.union(Math.round(left), Math.round(top), Math.round(right), Math.round(bottom));
        }
    }
    mInProgressX = event.getX();
    mInProgressY = event.getY();

    // To save updates, we only invalidate if the user moved beyond a certain amount.
    if (invalidateNow) {
        mInvalidate.union(mTmpInvalidateRect);
        invalidate(mInvalidate);
        mInvalidate.set(mTmpInvalidateRect);
    }
}

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

private void applyHeaderPadding(MotionEvent ev) {
    // getHistorySize has been available since API 1
    int pointerCount = ev.getHistorySize();

    for (int p = 0; p < pointerCount; p++) {
        if (mRefreshState == RELEASE_TO_REFRESH) {
            if (getListView().isVerticalFadingEdgeEnabled()) {
                getListView().setVerticalScrollBarEnabled(false);
            }/*  ww  w  .jav  a  2 s. c om*/

            int historicalY = (int) ev.getHistoricalY(p);

            // Calculate the padding to apply, we divide by 1.7 to
            // simulate a more resistant effect during pull.
            int topPadding = (int) (((historicalY - mLastMotionY) - mRefreshViewHeight) / 1.7);

            mRefreshView.setPadding(mRefreshView.getPaddingLeft(), topPadding, mRefreshView.getPaddingRight(),
                    mRefreshView.getPaddingBottom());
        }
    }
}

From source file:com.android.messaging.ui.conversationlist.ConversationListSwipeHelper.java

@Override
public boolean onInterceptTouchEvent(final RecyclerView recyclerView, final MotionEvent event) {
    if (event.getPointerCount() > 1) {
        // Ignore subsequent pointers.
        return false;
    }/*from  ww w .  j  a  v  a  2  s.c  o m*/

    // We are not yet tracking a swipe gesture. Begin detection by spying on
    // touch events bubbling down to our children.
    final int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (!hasGestureSwipeTarget()) {
            onGestureStart();

            mVelocityTracker.addMovement(event);
            mInitialX = event.getX();
            mInitialY = event.getY();

            final View viewAtPoint = mRecyclerView.findChildViewUnder(mInitialX, mInitialY);
            final ConversationListItemView child = (ConversationListItemView) viewAtPoint;
            if (viewAtPoint instanceof ConversationListItemView && child != null && child.isSwipeAnimatable()) {
                // Begin detecting swipe on the target for the rest of the gesture.
                mListItemView = child;
                if (mListItemView.isAnimating()) {
                    mListItemView = null;
                }
            } else {
                mListItemView = null;
            }
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (hasValidGestureSwipeTarget()) {
            mVelocityTracker.addMovement(event);

            final int historicalCount = event.getHistorySize();
            // First consume the historical events, then consume the current ones.
            for (int i = 0; i < historicalCount + 1; i++) {
                float currX;
                float currY;
                if (i < historicalCount) {
                    currX = event.getHistoricalX(i);
                    currY = event.getHistoricalY(i);
                } else {
                    currX = event.getX();
                    currY = event.getY();
                }
                final float deltaX = currX - mInitialX;
                final float deltaY = currY - mInitialY;
                final float absDeltaX = Math.abs(deltaX);
                final float absDeltaY = Math.abs(deltaY);

                if (!mIsSwiping && absDeltaY > mTouchSlop
                        && absDeltaY > (ERROR_FACTOR_MULTIPLIER * absDeltaX)) {
                    // Stop detecting swipe for the remainder of this gesture.
                    onGestureEnd();
                    return false;
                }

                if (absDeltaX > mTouchSlop) {
                    // Swipe detected. Return true so we can handle the gesture in
                    // onTouchEvent.
                    mIsSwiping = true;

                    // We don't want to suddenly jump the slop distance.
                    mInitialX = event.getX();
                    mInitialY = event.getY();

                    onSwipeGestureStart(mListItemView);
                    return true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (hasGestureSwipeTarget()) {
            onGestureEnd();
        }
        break;
    }

    // Start intercepting touch events from children if we detect a swipe.
    return mIsSwiping;
}

From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override/*from   w ww  . ja  v a2  s  . co m*/
public boolean onGenericMotionEvent(MotionEvent event) {
    if (event.getEventTime() + EVENT_THRESHOLD_DECAY < SystemClock.uptimeMillis()) {
        //Log.i("DosBoxTurbo","eventtime: "+event.getEventTime() + " systemtime: "+SystemClock.uptimeMillis());
        return true; // get rid of old events
    }
    final int pointerIndex = MotionEventCompat.getActionIndex(event);
    final int pointerId = MotionEventCompat.getPointerId(event, pointerIndex);

    if ((MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_MOVE) && ((mWrap.getSource(event)
            & TouchEventWrapper.SOURCE_CLASS_MASK) == TouchEventWrapper.SOURCE_CLASS_JOYSTICK)) {
        if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) && (mAnalogStickPref < 3)) {
            // use new 3.1 API to handle joystick movements
            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
                processJoystickInput(event, i);
            }

            processJoystickInput(event, -1);
            return true;
        } else {
            // use older 2.2+ API to handle joystick movements
            if (mInputMode == INPUT_MODE_REAL_JOYSTICK) {
                x[pointerId] = mWrap.getX(event, pointerId);
                y[pointerId] = mWrap.getY(event, pointerId);
                DosBoxControl.nativeJoystick((int) ((x[pointerId] * 256f) + mJoyCenterX),
                        (int) ((y[pointerId] * 256f) + mJoyCenterY), ACTION_MOVE, -1);
                return true;
            }
        }
    } else if ((MotionEventCompat.getActionMasked(event) == MotionEventCompat.ACTION_HOVER_MOVE)
            && ((mWrap.getSource(event)
                    & TouchEventWrapper.SOURCE_CLASS_MASK) == TouchEventWrapper.SOURCE_CLASS_POINTER)) {
        if (mInputMode == INPUT_MODE_REAL_MOUSE) {
            x_last[pointerId] = x[pointerId];
            y_last[pointerId] = y[pointerId];
            x[pointerId] = mWrap.getX(event, pointerId);
            y[pointerId] = mWrap.getY(event, pointerId);
            if (mAbsolute) {
                DosBoxControl.nativeMouseWarp(x[pointerId], y[pointerId], mRenderer.x, mRenderer.y,
                        mRenderer.width, mRenderer.height);
            } else {
                DosBoxControl.nativeMouse((int) (x[pointerId] * mMouseSensitivityX),
                        (int) (y[pointerId] * mMouseSensitivityY),
                        (int) (x_last[pointerId] * mMouseSensitivityX),
                        (int) (y_last[pointerId] * mMouseSensitivityY), 2, -1);
            }

            int buttonState = mWrap.getButtonState(event);
            if (((buttonState & TouchEventWrapper.BUTTON_SECONDARY) != 0) && !mSPenButton) {
                // Handle Samsung SPen Button (RMB) - DOWN
                DosBoxControl.nativeMouse(0, 0, 0, 0, ACTION_DOWN, BTN_B);
                mSPenButton = true;
            } else if (((buttonState & TouchEventWrapper.BUTTON_SECONDARY) == 0) && mSPenButton) {
                // Handle Samsung SPen Button (RMB) - UP
                DosBoxControl.nativeMouse(0, 0, 0, 0, ACTION_UP, BTN_B);
                mSPenButton = false;
            }

            if (mDebug)
                Log.d("DosBoxTurbo", "onGenericMotionEvent() INPUT_MODE_REAL_MOUSE x: " + x[pointerId] + "  y: "
                        + y[pointerId] + "  |  xL: " + x_last[pointerId] + "  yL: " + y_last[pointerId]);
            try {
                if (!mInputLowLatency)
                    Thread.sleep(95);
                else
                    Thread.sleep(65);
            } catch (InterruptedException e) {
            }
            return true;
        }
    } else if (MotionEventCompat.getActionMasked(event) == MotionEventCompat.ACTION_HOVER_EXIT) {
        if (mInputMode == INPUT_MODE_REAL_MOUSE) {
            // hover exit
            int buttonState = mWrap.getButtonState(event);
            if (((buttonState & TouchEventWrapper.BUTTON_SECONDARY) == 0) && mSPenButton) {
                // Handle Samsung SPen Button (RMB) - UP
                DosBoxControl.nativeMouse(0, 0, 0, 0, ACTION_UP, BTN_B);
                mSPenButton = false;
                return true;
            }
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        return super.onGenericMotionEvent(event);
    } else {
        return false;
    }
}