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() 

Source Link

Document

#getX(int) for the first pointer index (may be an arbitrary pointer identifier).

Usage

From source file:com.abewy.android.apps.klyph.widget.KlyphDrawerLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mLeftDragger.processTouchEvent(ev);/*from  w w w  .j a v  a  2 s.  c o  m*/
    mRightDragger.processTouchEvent(ev);

    final int action = ev.getAction();
    boolean wantTouchEvents = true;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialMotionX = x;
        mInitialMotionY = y;
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        final float x = ev.getX();
        final float y = ev.getY();
        boolean peekingOnly = true;
        final View touchedView = mLeftDragger.findTopChildUnder((int) x, (int) y);
        if (touchedView != null && isContentView(touchedView)) {
            final float dx = x - mInitialMotionX;
            final float dy = y - mInitialMotionY;
            final int slop = mLeftDragger.getTouchSlop();
            if (dx * dx + dy * dy < slop * slop) {
                // Taps close a dimmed open drawer but only if it isn't locked open.
                final View openDrawer = findOpenDrawer();
                if (openDrawer != null) {
                    peekingOnly = false;//getDrawerLockMode(openDrawer) == LOCK_MODE_LOCKED_OPEN;
                }
            }
        }
        closeDrawers(peekingOnly);
        mDisallowInterceptRequested = false;
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        closeDrawers(true);
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
        break;
    }
    }

    return wantTouchEvents;
}

From source file:androidVNC.VncCanvasActivity.java

boolean touchPan(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        panTouchX = event.getX();
        panTouchY = event.getY();//ww w.  j av  a  2 s  .  c  o m
        break;
    case MotionEvent.ACTION_MOVE:
        pan(event);
        panTouchX = event.getX();
        panTouchY = event.getY();
        break;
    case MotionEvent.ACTION_UP:
        pan(event);
        break;
    }
    return true;
}

From source file:co.codecrunch.musicplayerlite.slidinguppanelhelper.SlidingUpPanelLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (!isEnabled() || !isTouchEnabled() || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();/*from  w  ww  . j a v  a2s  .c o  m*/
        return super.onInterceptTouchEvent(ev);
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }

    final float x = ev.getX();
    final float y = ev.getY();

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mIsUnableToDrag = false;
        mInitialMotionX = x;
        mInitialMotionY = y;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final float adx = Math.abs(x - mInitialMotionX);
        final float ady = Math.abs(y - mInitialMotionY);
        final int dragSlop = mDragHelper.getTouchSlop();

        // Handle any horizontal scrolling on the drag view.
        if (mIsUsingDragViewTouchEvents && adx > dragSlop && ady < dragSlop) {
            return super.onInterceptTouchEvent(ev);
        }

        if ((ady > dragSlop && adx > ady) || !isDragViewUnder((int) mInitialMotionX, (int) mInitialMotionY)) {
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
        break;
    }
    }

    return mDragHelper.shouldInterceptTouchEvent(ev);
}

From source file:cn.emagsoftware.ui.BugFixedSlidingPaneLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    try {//from  w ww .  j  av a 2  s  .c  o  m
        if (!mCanSlide) {
            return super.onTouchEvent(ev);
        }

        mDragHelper.processTouchEvent(ev);

        final int action = ev.getAction();
        boolean wantTouchEvents = true;

        switch (action & MotionEventCompat.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();
            mInitialMotionX = x;
            mInitialMotionY = y;
            break;
        }

        case MotionEvent.ACTION_UP: {
            if (isDimmed(mSlideableView)) {
                final float x = ev.getX();
                final float y = ev.getY();
                final float dx = x - mInitialMotionX;
                final float dy = y - mInitialMotionY;
                final int slop = mDragHelper.getTouchSlop();
                if (dx * dx + dy * dy < slop * slop
                        && mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y)) {
                    // Taps close a dimmed open pane.
                    closePane(mSlideableView, 0);
                    break;
                }
            }
            break;
        }
        }

        return wantTouchEvents;
    } catch (ArrayIndexOutOfBoundsException e) {
        LogManager.logE(BugFixedSlidingPaneLayout.class,
                "internal bug in SlidingPaneLayout,this bug can be ignored in most cases.", e);
        return false;
    } catch (IllegalArgumentException e) {
        LogManager.logE(BugFixedSlidingPaneLayout.class,
                "internal bug in SlidingPaneLayout,this bug can be ignored in most cases.", e);
        return false;
    }
}

From source file:com.android.app.MediaPlaybackActivity.java

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    TextView tv = textViewForContainer(v);
    if (tv == null) {
        return false;
    }/*from ww w. ja  v a  2  s .  co  m*/
    if (action == MotionEvent.ACTION_DOWN) {
        v.setBackgroundColor(0xff606060);
        mInitialX = mLastX = (int) event.getX();
        mDraggingLabel = false;
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        v.setBackgroundColor(0);
        if (mDraggingLabel) {
            Message msg = mLabelScroller.obtainMessage(0, tv);
            mLabelScroller.sendMessageDelayed(msg, 1000);
        }
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (mDraggingLabel) {
            int scrollx = tv.getScrollX();
            int x = (int) event.getX();
            int delta = mLastX - x;
            if (delta != 0) {
                mLastX = x;
                scrollx += delta;
                if (scrollx > mTextWidth) {
                    // scrolled the text completely off the view to the left
                    scrollx -= mTextWidth;
                    scrollx -= mViewWidth;
                }
                if (scrollx < -mViewWidth) {
                    // scrolled the text completely off the view to the right
                    scrollx += mViewWidth;
                    scrollx += mTextWidth;
                }
                tv.scrollTo(scrollx, 0);
            }
            return true;
        }
        int delta = mInitialX - (int) event.getX();
        if (Math.abs(delta) > mTouchSlop) {
            // start moving
            mLabelScroller.removeMessages(0, tv);

            // Only turn ellipsizing off when it's not already off, because it
            // causes the scroll position to be reset to 0.
            if (tv.getEllipsize() != null) {
                tv.setEllipsize(null);
            }
            Layout ll = tv.getLayout();
            // layout might be null if the text just changed, or ellipsizing
            // was just turned off
            if (ll == null) {
                return false;
            }
            // get the non-ellipsized line width, to determine whether scrolling
            // should even be allowed
            mTextWidth = (int) tv.getLayout().getLineWidth(0);
            mViewWidth = tv.getWidth();
            if (mViewWidth > mTextWidth) {
                tv.setEllipsize(TruncateAt.END);
                v.cancelLongPress();
                return false;
            }
            mDraggingLabel = true;
            tv.setHorizontalFadingEdgeEnabled(true);
            v.cancelLongPress();
            return true;
        }
    }
    return false;
}

From source file:com.aidy.bottomdrawerlayout.BottomDrawerLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    Log.i(TAG, "onTouchEvent()");
    mBottomDragger.processTouchEvent(ev);
    final int action = ev.getAction();
    boolean wantTouchEvents = true;
    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        Log.i(TAG, "onTouchEvent() -- ACTION_DOWN");
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialMotionX = x;//from w  ww.  j  ava 2 s.c  o m
        mInitialMotionY = y;
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        Log.i(TAG, "onTouchEvent() -- ACTION_UP");
        final float x = ev.getX();
        final float y = ev.getY();
        boolean peekingOnly = true;
        final View touchedView = mBottomDragger.findTopChildUnder((int) x, (int) y);
        if (touchedView != null && isContentView(touchedView)) {
            final float dx = x - mInitialMotionX;
            final float dy = y - mInitialMotionY;
            final int slop = mBottomDragger.getTouchSlop();
            if (dx * dx + dy * dy < slop * slop) {
                // Taps close a dimmed open drawer but only if it isn't
                // locked open.
                final View openDrawer = findOpenDrawer();
                if (openDrawer != null) {
                    peekingOnly = getDrawerLockMode(openDrawer) == LOCK_MODE_LOCKED_OPEN;
                }
            }
        }
        closeDrawers(peekingOnly);
        mDisallowInterceptRequested = false;
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        Log.i(TAG, "onTouchEvent() -- ACTION_CANCEL");
        closeDrawers(true);
        mDisallowInterceptRequested = false;
        mChildrenCanceledTouch = false;
        break;
    }
    }

    boolean result = wantTouchEvents;
    Log.i(TAG, "onTouchEvent() -- result = " + result);
    return result;
}

From source file:com.appsummary.luoxf.mylibrary.swipbackhelper.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's
 * onInterceptTouchEvent should cause the parent to intercept the touch
 * event stream.//  www  .j  a  v  a  2s.  c  o  m
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from
 * onInterceptTouchEvent
 */
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        if (mDragState == STATE_SETTLING) {
            setDragState(STATE_DRAGGING);
        } else if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgeTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
            setDragState(STATE_JUDGING);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);
        saveInitialMotion(x, y, pointerId);
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_JUDGING) {
            if (mVelocityTracker == null) {
                mVelocityTracker = VelocityTracker.obtain();
            }
            if (mDragState == STATE_DRAGGING)
                mVelocityTracker.addMovement(ev);
            final int i = MotionEventCompat.findPointerIndex(ev, mActivePointerId);

            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[mActivePointerId];
            final float dy = y - mInitialMotionY[mActivePointerId];

            reportNewEdgeDrags(dx, dy, mActivePointerId);

            final View toCapture = findTopChildUnder((int) x, (int) y);
            int slop = checkTouchSlop(toCapture, dx, dy);

            if (slop == -1)
                cancel();
            else if (slop > 0 && tryCaptureViewForDrag(toCapture, mActivePointerId)) {
                break;
            }
            saveLastMotion(ev);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        releaseViewForPointerUp();
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        dispatchViewReleased(0, 0);
        cancel();
        break;
    }

    }
    return mDragState == STATE_DRAGGING;
}

From source file:com.appsummary.luoxf.mylibrary.swipbackhelper.ViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will
 * dispatch callback events as needed before returning. The parent view's
 * onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 */// w ww.  j  av a2  s  .c o m
public void processTouchEvent(MotionEvent ev) {

    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        if (mDragState == STATE_SETTLING) {
            setDragState(STATE_DRAGGING);
        } else if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgeTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
            setDragState(STATE_JUDGING);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_JUDGING) {

            final int i = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[mActivePointerId];
            final float dy = y - mInitialMotionY[mActivePointerId];

            reportNewEdgeDrags(dx, dy, mActivePointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag.
                break;
            }

            final View toCapture = findTopChildUnder((int) x, (int) y);
            int slop = checkTouchSlop(toCapture, dx, dy);
            if (slop == -1)
                cancel();
            else if (slop > 0 && tryCaptureViewForDrag(toCapture, mActivePointerId)) {
                break;
            }
            saveLastMotion(ev);
        }

        if (mDragState == STATE_DRAGGING) {
            if (mVelocityTracker == null) {
                mVelocityTracker = VelocityTracker.obtain();
            }
            if (mDragState == STATE_DRAGGING)
                mVelocityTracker.addMovement(ev);

            final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            //????????
            if (index == -1)
                break;

            final float x = MotionEventCompat.getX(ev, index);
            final float y = MotionEventCompat.getY(ev, index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = (int) (y - mLastMotionY[mActivePointerId]);
            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        releaseViewForPointerUp();
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        dispatchViewReleased(0, 0);
        cancel();
        break;
    }
    }
}

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();//  ww  w. ja  v  a 2s  . 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:co.adrianblan.fastbrush.MyGLSurfaceView.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    switch (e.getAction()) {

    case MotionEvent.ACTION_DOWN:
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } else {/* ww  w  . j  a va  2 s. c o m*/
            mVelocityTracker.clear();
        }

        queueEvent(new Runnable() {
            @Override
            public void run() {
                mRenderer.touchHasStarted();
            }
        });

        // No break is intentional

    case MotionEvent.ACTION_MOVE:

        mVelocityTracker.addMovement(e);

        // Compute velocity in pixels per second
        mVelocityTracker.computeCurrentVelocity(1);

        final ArrayList<TouchData> touchDataList = new ArrayList<>(e.getHistorySize() + 1);
        Vector2 viewportPosition;

        Vector2 viewportVelocity = new Vector2(
                VelocityTrackerCompat.getXVelocity(mVelocityTracker, e.getActionIndex()),
                VelocityTrackerCompat.getYVelocity(mVelocityTracker, e.getActionIndex()));

        // Add previous touch coordinates
        for (int i = 0; i < e.getHistorySize(); i++) {
            viewportPosition = new Vector2(e.getHistoricalX(i), e.getHistoricalY(i));

            touchDataList.add(new TouchData(mRenderer.viewportToWorld(viewportPosition), viewportVelocity,
                    e.getHistoricalSize(i), e.getHistoricalPressure(i)));
        }

        // Add current touch coordinates
        viewportPosition = new Vector2(e.getX(), e.getY());
        touchDataList.add(new TouchData(mRenderer.viewportToWorld(viewportPosition), viewportVelocity,
                e.getSize(), e.getPressure()));

        // Ensure we call switchMode() on the OpenGL thread.
        // queueEvent() is a method of GLSurfaceView that will do this for us.
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mRenderer.addTouchData(touchDataList);
            }
        });

        requestRender();
        break;

    case MotionEvent.ACTION_UP:

        queueEvent(new Runnable() {
            @Override
            public void run() {
                mRenderer.touchHasEnded();
            }
        });
        requestRender();
        break;
    }
    return true;
}