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.example.SmartBoard.DrawingView.java

public void onTouchCircleMode(MotionEvent event) {
    int eventaction = event.getAction();

    int X = (int) event.getX();
    int Y = (int) event.getY();

    switch (eventaction) {

    case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on
        // a ball
        finished = false;/*from   w w  w.j a va2  s.  c  om*/
        if (points[0] == null) {
            //initialize rectangle.
            points[0] = new Point();
            points[0].x = X;
            points[0].y = Y;

            points[1] = new Point();
            points[1].x = X;
            points[1].y = Y + 30;

            points[2] = new Point();
            points[2].x = X + 30;
            points[2].y = Y + 30;

            points[3] = new Point();
            points[3].x = X + 30;
            points[3].y = Y;

            balID = 2;
            groupId = 1;
            // declare each ball with the ColorBall class
            for (Point pt : points) {
                colorballs.add(new ColorBall(getContext(), R.drawable.dot_drag_handle, pt));
            }
        }
        invalidate();
        break;

    case MotionEvent.ACTION_MOVE: // touch drag with the ball

        if (balID > -1) {
            // move the balls the same as the finger
            colorballs.get(balID).setX(X);
            colorballs.get(balID).setY(Y);

            if (groupId == 1) {
                colorballs.get(1).setX(colorballs.get(0).getX());
                colorballs.get(1).setY(colorballs.get(2).getY());
                colorballs.get(3).setX(colorballs.get(2).getX());
                colorballs.get(3).setY(colorballs.get(0).getY());
            } else {
                colorballs.get(0).setX(colorballs.get(1).getX());
                colorballs.get(0).setY(colorballs.get(3).getY());
                colorballs.get(2).setX(colorballs.get(3).getX());
                colorballs.get(2).setY(colorballs.get(1).getY());
            }

            invalidate();
        }

        break;

    case MotionEvent.ACTION_UP:
        // touch drop - just do things here after dropping
        finished = true;

        break;
    }
    // redraw the canvas
    invalidate();
    return;

}

From source file:cn.com.hgh.view.SlideSwitch.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (slideable == false)
        return super.onTouchEvent(event);
    int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        eventStartX = (int) event.getRawX();
        break;/*w ww. j  a v  a2 s .  c  om*/
    case MotionEvent.ACTION_MOVE:
        //         eventLastX = (int) event.getRawX();
        //         diffX = eventLastX - eventStartX;
        //         int tempX = diffX + frontRect_left_begin;
        //         tempX = (tempX > max_left ? max_left : tempX);
        //         tempX = (tempX < min_left ? min_left : tempX);
        //         if (tempX >= min_left && tempX <= max_left) {
        //            frontRect_left = tempX;
        //            alpha = (int) (255 * (float) tempX / (float) max_left);
        //            invalidateView();
        //         }
        break;
    case MotionEvent.ACTION_UP:
        int wholeX = (int) (event.getRawX() - eventStartX);
        frontRect_left_begin = frontRect_left;
        toRight = (frontRect_left_begin > max_left / 2 ? true : false);
        if (Math.abs(wholeX) < 3) {
            toRight = !toRight;
        }
        //x??
        float eventx = event.getX();
        if (isOpen) {
            //?
            if (eventx < width / 2) {
                //            switchBusinessStatus();
                switchStatus();

            }
        } else {
            //?
            if (eventx > width / 2) {
                //               switchBusinessStatus();
                switchStatus();

            }
        }
        break;
    default:
        break;
    }
    return true;
}

From source file:com.android.volley.ui.PhotoView.java

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    final int action = e.getAction();
    boolean handled = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (mQuickScaleEnabled) {
            mDownFocusX = e.getX();
            mDownFocusY = e.getY();//from   w ww.java 2  s  .c o  m
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mQuickScaleEnabled) {
            handled = scale(e);
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (mQuickScaleEnabled && mDoubleTapOccurred) {
            final int deltaX = (int) (e.getX() - mDownFocusX);
            final int deltaY = (int) (e.getY() - mDownFocusY);
            int distance = (deltaX * deltaX) + (deltaY * deltaY);
            if (distance > sTouchSlopSquare) {
                mDoubleTapOccurred = false;
            }
        }
        break;

    }
    return handled;
}

From source file:com.example.SmartBoard.DrawingView.java

public void onTouchRectangleMode(MotionEvent event) {
    int eventaction = event.getAction();

    int X = (int) event.getX();
    int Y = (int) event.getY();

    switch (eventaction) {

    case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on
        // a ball
        finished = false;/* www .  java  2  s .com*/
        if (points[0] == null) {
            //initialize rectangle.
            points[0] = new Point();
            points[0].x = X;
            points[0].y = Y;

            points[1] = new Point();
            points[1].x = X;
            points[1].y = Y + 30;

            points[2] = new Point();
            points[2].x = X + 30;
            points[2].y = Y + 30;

            points[3] = new Point();
            points[3].x = X + 30;
            points[3].y = Y;

            balID = 2;
            groupId = 1;
            // declare each ball with the ColorBall class
            for (Point pt : points) {
                colorballs.add(new ColorBall(getContext(), R.drawable.dot_drag_handle, pt));
            }
        } else {
            //resize rectangle
            balID = -1;
            groupId = -1;
            for (int i = colorballs.size() - 1; i >= 0; i--) {
                ColorBall ball = colorballs.get(i);
                // check if inside the bounds of the ball (circle)
                // get the center for the ball
                int centerX = ball.getX() + ball.getWidthOfBall();
                int centerY = ball.getY() + ball.getHeightOfBall();

                // calculate the radius from the touch to the center of the
                // ball
                double radCircle = Math
                        .sqrt((double) (((centerX - X) * (centerX - X)) + (centerY - Y) * (centerY - Y)));

                if (radCircle < ball.getWidthOfBall()) {

                    balID = ball.getID();
                    if (balID == 1 || balID == 3) {
                        groupId = 2;
                    } else {
                        groupId = 1;
                    }
                    invalidate();
                    break;
                }
                invalidate();
            }
        }
        break;

    case MotionEvent.ACTION_MOVE: // touch drag with the ball

        if (balID > -1) {
            // move the balls the same as the finger
            colorballs.get(balID).setX(X);
            colorballs.get(balID).setY(Y);

            if (groupId == 1) {
                colorballs.get(1).setX(colorballs.get(0).getX());
                colorballs.get(1).setY(colorballs.get(2).getY());
                colorballs.get(3).setX(colorballs.get(2).getX());
                colorballs.get(3).setY(colorballs.get(0).getY());
            } else {
                colorballs.get(0).setX(colorballs.get(1).getX());
                colorballs.get(0).setY(colorballs.get(3).getY());
                colorballs.get(2).setX(colorballs.get(3).getX());
                colorballs.get(2).setY(colorballs.get(1).getY());
            }

            invalidate();
        }

        break;

    case MotionEvent.ACTION_UP:
        // touch drop - just do things here after dropping
        finished = true;

        break;
    }
    // redraw the canvas
    invalidate();
    return;

}

From source file:android.car.ui.provider.CarDrawerLayout.java

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

    // "|" used deliberately here; both methods should be invoked.
    final boolean interceptForDrag = mDragger.shouldInterceptTouchEvent(ev);

    boolean interceptForTap = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        if (onScreen() > 0 && isContentView(mDragger.findTopChildUnder((int) x, (int) y))) {
            interceptForTap = true;/*from ww  w .  j  av a2 s.  co  m*/
        }
        mChildrenCanceledTouch = false;
        break;
    }
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mChildrenCanceledTouch = false;
    }
    }

    return interceptForDrag || interceptForTap || mChildrenCanceledTouch;
}

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

private boolean canStartScroll(MotionEvent event) {
    View view = JuickMessagesAdapter.findViewForCoordinates(
            (ViewGroup) getActivity().getWindow().getDecorView(), event.getX(), event.getY());
    while (view != null) {
        Object tag = view.getTag();
        if (tag instanceof String) {
            String stag = (String) tag;
            if (stag.contains("horizontal-slider")) {
                return false;
            }//  w  w  w .j a  v a  2s. co m
        }
        if (view instanceof ImageGallery) {
            return false;
        }
        if (view.getParent() instanceof View) {
            view = (View) view.getParent();
        } else {
            break;
        }
    }
    return true;
}

From source file:androidVNC.VncCanvasActivity.java

/**
 * Pan based on touch motions//from  ww w. java  2  s .c om
 *
 * @param event
 */
private boolean pan(MotionEvent event) {
    float curX = event.getX();
    float curY = event.getY();
    int dX = (int) (panTouchX - curX);
    int dY = (int) (panTouchY - curY);

    return vncCanvas.pan(dX, dY);
}

From source file:com.android.launcher3.folder.FolderIcon.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Call the superclass onTouchEvent first, because sometimes it changes the state to
    // isPressed() on an ACTION_UP
    boolean result = super.onTouchEvent(event);

    // Check for a stylus button press, if it occurs cancel any long press checks.
    if (mStylusEventHelper.onMotionEvent(event)) {
        mLongPressHelper.cancelLongPress();
        return true;
    }//  ww w .j av a2 s  . com

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mLongPressHelper.postCheckForLongPress();
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        mLongPressHelper.cancelLongPress();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
            mLongPressHelper.cancelLongPress();
        }
        break;
    }
    return result;
}

From source file:biz.laenger.android.vpbs.ViewPagerBottomSheetBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        mIgnoreEvents = true;/*from w  w  w  .  j  a  va 2s  .  c o m*/
        return false;
    }
    int action = MotionEventCompat.getActionMasked(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchingScrollingChild = false;
        mActivePointerId = MotionEvent.INVALID_POINTER_ID;
        // Reset the ignore flag
        if (mIgnoreEvents) {
            mIgnoreEvents = false;
            return false;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        int initialX = (int) event.getX();
        mInitialY = (int) event.getY();
        for (WeakReference<View> scrollableView : scrollableViews) {
            View scroll = scrollableView.get();
            if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
                mActivePointerId = event.getPointerId(event.getActionIndex());
                mTouchingScrollingChild = true;
                break;
            }
        }

        mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID
                && !parent.isPointInChildBounds(child, initialX, mInitialY);
        break;
    }
    if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
        return true;
    }
    // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
    // it is not the top most view of its parent. This is not necessary when the touch event is
    // happening over the scrolling content as nested scrolling logic handles that case.
    boolean isPointInChildBounds = false;

    for (WeakReference<View> scrollableView : scrollableViews) {
        isPointInChildBounds = scrollableView.get() != null
                && parent.isPointInChildBounds(scrollableView.get(), (int) event.getX(), (int) event.getY());
        if (isPointInChildBounds) {
            break;
        }
    }

    return action == MotionEvent.ACTION_MOVE && !mIgnoreEvents && mState != STATE_DRAGGING
            && !isPointInChildBounds && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}

From source file:androidVNC.VncCanvasActivity.java

boolean trackballMouse(MotionEvent evt) {
    int dx = convertTrackballDelta(evt.getX());
    int dy = convertTrackballDelta(evt.getY());

    evt.offsetLocation(vncCanvas.mouseX + dx - evt.getX(), vncCanvas.mouseY + dy - evt.getY());

    if (vncCanvas.processPointerEvent(evt, trackballButtonDown)) {
        return true;
    }/*from w  ww. j  ava 2 s  .co  m*/
    return VncCanvasActivity.super.onTouchEvent(evt);
}