Example usage for android.view MotionEvent getX

List of usage examples for android.view MotionEvent getX

Introduction

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

Prototype

public final float getX(int pointerIndex) 

Source Link

Document

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

Usage

From source file:au.gov.ga.worldwind.androidremote.client.Remote.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (controlling) {
        velocityTracker.addMovement(event);
        velocityTracker.computeCurrentVelocity(1);

        boolean down = event.getActionMasked() == MotionEvent.ACTION_DOWN
                || event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN;
        boolean up = event.getActionMasked() == MotionEvent.ACTION_UP
                || event.getActionMasked() == MotionEvent.ACTION_POINTER_UP;
        Finger[] fingers = new Finger[event.getPointerCount()];
        for (int i = 0; i < event.getPointerCount(); i++) {
            fingers[i] = new Finger(event.getPointerId(i), event.getX(i), event.getY(i),
                    velocityTracker.getXVelocity(i), velocityTracker.getYVelocity(i),
                    !(event.getActionIndex() == i && up));
        }//from  w  w w.  ja v a2  s.com

        FingerMessage<?> message = up ? new UpMessage(fingers)
                : down ? new DownMessage(fingers) : new MoveMessage(fingers);
        communicator.sendMessage(message);
    }
    return super.onTouchEvent(event);
}

From source file:android.support.v7.widget.ForwardingListener.java

/**
 * Observes motion events and determines when to start forwarding.
 *
 * @param srcEvent motion event in source view coordinates
 * @return true to start forwarding motion events, false otherwise
 */// w ww.ja  v  a2 s.  com
private boolean onTouchObserved(MotionEvent srcEvent) {
    final View src = mSrc;
    if (!src.isEnabled()) {
        return false;
    }

    final int actionMasked = MotionEventCompat.getActionMasked(srcEvent);
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = srcEvent.getPointerId(0);

        if (mDisallowIntercept == null) {
            mDisallowIntercept = new DisallowIntercept();
        }
        src.postDelayed(mDisallowIntercept, mTapTimeout);

        if (mTriggerLongPress == null) {
            mTriggerLongPress = new TriggerLongPress();
        }
        src.postDelayed(mTriggerLongPress, mLongPressTimeout);
        break;
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
        if (activePointerIndex >= 0) {
            final float x = srcEvent.getX(activePointerIndex);
            final float y = srcEvent.getY(activePointerIndex);

            // Has the pointer moved outside of the view?
            if (!pointInView(src, x, y, mScaledTouchSlop)) {
                clearCallbacks();

                // Don't let the parent intercept our events.
                src.getParent().requestDisallowInterceptTouchEvent(true);
                return true;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        clearCallbacks();
        break;
    }

    return false;
}

From source file:org.cocos2dx.lib.TextInputWraper.java

private void dumpEvent(MotionEvent event) {
    String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?",
            "9?" };
    StringBuilder sb = new StringBuilder();
    int action = event.getAction();
    int actionCode = action & MotionEvent.ACTION_MASK;
    sb.append("event ACTION_").append(names[actionCode]);
    if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) {
        sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
        sb.append(")");
    }//from   w ww  .j  av a  2s .c  o  m
    sb.append("[");
    for (int i = 0; i < event.getPointerCount(); i++) {
        sb.append("#").append(i);
        sb.append("(pid ").append(event.getPointerId(i));
        sb.append(")=").append((int) event.getX(i));
        sb.append(",").append((int) event.getY(i));
        if (i + 1 < event.getPointerCount())
            sb.append(";");
    }
    sb.append("]");
    Log.d(TAG, sb.toString());
}

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

private void handleDown(MotionEvent event) {
    int actionIndex = event.getActionIndex();
    float eventX = event.getX(actionIndex);
    float eventY = event.getY(actionIndex);
    switchToState(STATE_START, eventX, eventY);
    if (!trySwitchToFirstTouchState(eventX, eventY)) {
        mDragging = false;//from ww  w  .j av  a2  s  .  c  om
    } else {
        mPointerId = event.getPointerId(actionIndex);
        updateGlowPosition(eventX, eventY);
    }
}

From source file:org.medcare.Dicom.DicomActivity.java

/** Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
}

From source file:org.medcare.Dicom.DicomActivity.java

/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event) {
    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    point.set(x / 2, y / 2);// w w w .  j a v  a  2  s .c o m
}

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

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (isInGraphView(event.getX(0), event.getY(0))) {
        this.mDetector.onTouchEvent(event);
        if (isMeasure) {
            measureEvent(event);//from   w w  w.  j a v  a 2s. c o  m
        } else {
            scaleEvent(event);
        }
        invalidateGraphView();
        // Go to scaling mode when user release finger in measure mode.
        if (event.getActionMasked() == MotionEvent.ACTION_UP) {
            if (isMeasure) {
                switchMeasureAndScaleMode();
            }
        }
    } else {
        // When finger is outside the plot, hide the cursor and go to scaling mode.
        if (isMeasure) {
            graphView.hideCursor();
            switchMeasureAndScaleMode();
        }
    }
    return super.onTouchEvent(event);
}

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

private void handleUp(MotionEvent event) {
    if (DEBUG && mDragging)
        Log.v(TAG, "** Handle RELEASE");
    int actionIndex = event.getActionIndex();
    if (event.getPointerId(actionIndex) == mPointerId) {
        switchToState(STATE_FINISH, event.getX(actionIndex), event.getY(actionIndex));
    }/* w ww. jav  a 2s.c o  m*/
}

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

private void scaleEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_MOVE) {
        xShift0 = INIT;//from  www  . j  a  va 2 s .  co  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.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  a2  s. 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;
}