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.dragon4.owo.ar_trace.ARCore.MixView.java

@Override
public boolean onTouchEvent(MotionEvent me) {
    try {/* w ww  .  j a  v  a2 s  .  c om*/
        killOnError(); // ?  ?

        //  ? x, y  
        float xPress = me.getX();
        float yPress = me.getY();

        // ?? ??  ?? ? ?? 
        if (me.getAction() == MotionEvent.ACTION_UP) {
            dataView.clickEvent(xPress, yPress);
        }

        return true;

    } catch (Exception ex) { //  
        //doError(ex);
        ex.printStackTrace();
        return super.onTouchEvent(me);
    }
}

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

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

    int X = (int) event.getX();
    int Y = (int) event.getY();
    textPosX = X;/*from   w  w  w  .  j  av a 2 s  . c  o m*/
    textPosY = Y;

    if (isPlaced()) {
        return;
    }
    finished = true;

    // redraw the canvas
    invalidate();

    return;

}

From source file:com.appunite.scroll.ScaleImageView.java

@SuppressWarnings("UnusedDeclaration")
public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    final ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
    mMinEdge = viewConfiguration.getScaledEdgeSlop();

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScaleImageView, defStyle, defStyle);
    assert a != null;

    Drawable src = null;/*  w  w w.  ja v a2s .  c  o  m*/
    try {
        mMinWidth = a.getDimensionPixelSize(R.styleable.ScaleImageView_android_minWidth, 0);
        mMinHeight = a.getDimensionPixelSize(R.styleable.ScaleImageView_android_minHeight, 0);
        src = a.getDrawable(R.styleable.ScaleImageView_android_src);
    } finally {
        a.recycle();
    }

    ScaleGestureDetector.OnScaleGestureListener scaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        /**
         * This is the active focal point in terms of the viewport. Could be a local
         * variable but kept here to minimize per-frame allocations.
         */
        private PointF viewportFocus = new PointF();

        @Override
        public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
            float focusX = scaleGestureDetector.getFocusX();
            float focusY = scaleGestureDetector.getFocusY();
            float scaleFactor = scaleGestureDetector.getScaleFactor();
            float previousScale = mScale;
            mScale *= scaleFactor;

            doScale(focusX, focusY, scaleFactor);
            return true;
        }

    };
    GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {

            releaseEdgeEffects();
            mScroller.forceFinished(true);
            ViewCompat.postInvalidateOnAnimation(ScaleImageView.this);
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return performClick();
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            mZoomer.forceFinished(true);
            mZoomStartScale = mScale;
            mZoomFocalPoint.set(e.getX(), e.getY());
            mZoomer.startZoom(ZOOM_AMOUNT);
            ViewCompat.postInvalidateOnAnimation(ScaleImageView.this);
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            getRealTranslation(mTranslation, mRealTranslation);
            mRealTranslation.offset(-distanceX, -distanceY);
            getTranslation(mRealTranslation, mTranslation);

            computeMaxScrollSize(mMaxScrollBuffer);
            getImageRect(mRectF);
            float scrolledX = -mRectF.left;
            float scrolledY = -mRectF.top;
            boolean canScrollX = mRectF.left > mContentRect.left || mRectF.right < mContentRect.right;
            boolean canScrollY = mRectF.top > mContentRect.top || mRectF.bottom < mContentRect.bottom;
            validateTranslation();

            disallowParentInterceptWhenOnEdge(distanceX, distanceY);

            if (mScale > mMinScale) {
                if (canScrollX && scrolledX < 0) {
                    mEdgeEffectLeft.onPull(scrolledX / (float) mContentRect.width());
                    mEdgeEffectLeftActive = true;
                }
                if (canScrollY && scrolledY < 0) {
                    mEdgeEffectTop.onPull(scrolledY / (float) mContentRect.height());
                    mEdgeEffectTopActive = true;
                }
                if (canScrollX && scrolledX > mMaxScrollBuffer.x) {
                    mEdgeEffectRight.onPull((scrolledX - mMaxScrollBuffer.x) / (float) mContentRect.width());
                    mEdgeEffectRightActive = true;
                }
                if (canScrollY && scrolledY > mMaxScrollBuffer.y) {
                    mEdgeEffectBottom.onPull((scrolledY - mMaxScrollBuffer.y) / (float) mContentRect.height());
                    mEdgeEffectBottomActive = true;
                }
            }

            ViewCompat.postInvalidateOnAnimation(ScaleImageView.this);
            return true;
        }

        private void disallowParentInterceptWhenOnEdge(float directionX, float directionY) {
            final ViewParent parent = getParent();
            if (parent != null && (mAllowParentHorizontalScroll || mAllowParentVerticalScroll)) {
                getImageRect(mRectF);
                if (mAllowParentHorizontalScroll) {
                    if (directionX > 0 && Math.abs(mRectF.right - mContentRect.right) > mMinEdge) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                    if (directionX < 0 && Math.abs(mRectF.left - mContentRect.left) > mMinEdge) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
                if (mAllowParentVerticalScroll) {
                    if (directionY > 0 && Math.abs(mRectF.bottom - mContentRect.bottom) > mMinEdge) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                    if (directionY < 0 && Math.abs(mRectF.top - mContentRect.top) > mMinEdge) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
            }
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            disallowParentInterceptWhenOnEdge(velocityX, velocityY);
            fling((int) -velocityX, (int) -velocityY);
            return true;
        }
    };

    mScaleGestureDetector = new ScaleGestureDetector(context, scaleGestureListener);
    mGestureDetector = new GestureDetectorCompat(context, gestureListener);

    mScroller = new OverScroller(context);
    mZoomer = new Zoomer(context);

    // Sets up edge effects
    mEdgeEffectLeft = new EdgeEffectCompat(context);
    mEdgeEffectTop = new EdgeEffectCompat(context);
    mEdgeEffectRight = new EdgeEffectCompat(context);
    mEdgeEffectBottom = new EdgeEffectCompat(context);

    setSrcDrawable(src);
}

From source file:com.android.widget.SlidingPaneLayout.java

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

    if (!mCanSlide || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();//from w ww  .ja  va 2s  .c om
        return super.onInterceptTouchEvent(ev);
    }

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

    float x, y;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mIsUnableToDrag = false;
        x = ev.getX();
        y = ev.getY();
        mInitialMotionX = x;
        mInitialMotionY = y;
        break;
    case MotionEvent.ACTION_MOVE:
        x = ev.getX();
        y = ev.getY();
        final float adx = Math.abs(x - mInitialMotionX);
        final float ady = Math.abs(y - mInitialMotionY);
        final int slop = mDragHelper.getTouchSlop();
        if (ady > slop && adx > ady) {
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
    }

    final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev)
            // Intercept touch events only in the overhang area.
            && (ev.getY() <= mSlideOffset * mSlideRange + mOverhangSize && mState != STATE_OPENED_ENTIRELY);

    return interceptForDrag;
}

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

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

    int X = (int) event.getX();
    int Y = (int) event.getY();
    textResizePosX = X;//  ww w.ja  va  2s .  c  om
    textResizePosY = Y;

    JSONObject touchedObject = getTouchedObject(X, Y);
    if (touchedObject != null) {

        try {

            //for text
            if (touchedObject.optString("type").compareTo("Text") == 0) {
                touchedObject.put("size", textViewSize);
                Bitmap bm = textToBitmap(touchedObject.optString("text"), touchedObject.optInt("color"),
                        touchedObject.optInt("x"), touchedObject.optInt("y"), textViewSize);
                touchedObject.put("textBitmap", mqtt.bitmapToString(bm));
            }
            mqtt.publishObject(touchedObject);
        } catch (JSONException e) {

        }
        invalidate();
    }
}

From source file:android.widget.Gallery.java

@Override
public boolean onDown(MotionEvent e) {

    // Kill any existing fling/scroll
    mFlingRunnable.stop(false);/*from w  ww .java 2s.  c o  m*/

    // Get the item's view that was touched
    mDownTouchPosition = pointToPosition((int) e.getX(), (int) e.getY());

    if (mDownTouchPosition >= 0) {
        mDownTouchView = getChildAt(mDownTouchPosition - mFirstPosition);
        mDownTouchView.setPressed(true);
    }

    // Reset the multiple-scroll tracking state
    mIsFirstScroll = true;

    // Must return true to get matching events for this down event.
    return true;
}

From source file:android.support.design.widget.BottomSheetBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        mIgnoreEvents = true;//from  w w  w  . j  ava2s. com
        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();
        View scroll = mNestedScrollingChildRef.get();
        if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
            mActivePointerId = event.getPointerId(event.getActionIndex());
            mTouchingScrollingChild = true;
        }
        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.
    View scroll = mNestedScrollingChildRef.get();
    return action == MotionEvent.ACTION_MOVE && scroll != null && !mIgnoreEvents && mState != STATE_DRAGGING
            && !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY())
            && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
}

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

@Override
public boolean onTouchEvent(MotionEvent event) {
    //detect user touch
    float touchX = event.getX();
    float touchY = event.getY();
    //System.out.println("X"+touchX);
    // System.out.println("Y"+touchY);

    if (rectMode) {
        onTouchRectangleMode(event);//from  ww  w.  j  a  v a2s.c  om
        return true;
    } else if (circleMode) {
        onTouchCircleMode(event);
        return true;
    } else if (lineMode) {
        onTouchLineMode(event);
        return true;
    } else if (textMode) {
        onTouchTextMode(event);
        return true;
    } else if (dragMode) {
        onTouchDragEvent(event);
        return true;
    } else if (removeObjectMode) {
        onRemoveObjectEvent(event);
        return true;
    } else if (colorDropperMode) {
        onTouchColorDropperMode(event);
        return true;
    } else if (textSizeMode) {
        onTouchTextResizeEvent(event);
        return true;
    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        drawPath.moveTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_MOVE:
        drawPath.lineTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_UP:
        drawCanvas.drawPath(drawPath, drawPaint);
        drawPath.reset();
        break;
    default:
        return false;
    }
    //System.out.println(mqtt);

    invalidate();
    mqtt.publish(touchX, touchY, event.getAction(), paintColor, mode, strokeWidth);

    return true;
}

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

public void onTouchLineMode(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

        points = new Point[2];
        startX = X;/*  w ww .  j  a  v  a2 s. c o  m*/
        startY = Y;
        linePath.from(X, Y);
        linePath.to(X, Y);

        finished = false;
        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;

            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
        linePath.to(X, Y);
        colorballs.get(1).setX(X);
        colorballs.get(1).setY(Y);
        invalidate();
        break;

    case MotionEvent.ACTION_UP:
        // touch drop - just do things here after dropping
        linePath.to(X, Y);
        colorballs.get(1).setX(X);
        colorballs.get(1).setY(Y);
        finished = true;

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

}

From source file:cl.monsoon.s1next.widget.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();// w  w  w.  j a  v a  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;
}