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:am.widget.tabstrip.TabStripHelper.java

boolean onTouchEvent(MotionEvent event) {
    if (mClickable) {
        final float x = event.getX();
        final float y = event.getY();
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mClickPosition = PagerAdapter.POSITION_NONE;
            mDownX = x;//from   www .j av  a2 s  .com
            mDownY = y;
            break;
        case MotionEvent.ACTION_UP:
            mClickPosition = PagerAdapter.POSITION_NONE;
            if (mPager != null && getPageCount() > 0) {
                if (mView instanceof TabStripView)
                    mClickPosition = ((TabStripView) mView).getClickedPosition(mDownX, mDownY, x, y);
                else if (mView instanceof TabStripViewGroup)
                    mClickPosition = ((TabStripViewGroup) mView).getClickedPosition(mDownX, mDownY, x, y);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            mClickPosition = PagerAdapter.POSITION_NONE;
            break;
        }
    }
    if (mDoubleClickable) {
        final float x = event.getX();
        final float y = event.getY();
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mFirstDownX = mSecondDownX;
            mFirstDownY = mSecondDownY;
            mSecondDownX = x;
            mSecondDownY = y;
            break;
        case MotionEvent.ACTION_UP:
            final long downTime = event.getDownTime();
            if (mLastUpTime != 0 && downTime - mLastUpTime < DOUBLE_TAP_TIMEOUT) {
                mDoubleClicked = true;
                if (mView instanceof TabStripView)
                    mFirstPosition = ((TabStripView) mView).getClickedPosition(mFirstDownX, mFirstDownY,
                            mFirstUpX, mFirstUpY);
                else if (mView instanceof TabStripViewGroup)
                    mFirstPosition = ((TabStripViewGroup) mView).getClickedPosition(mFirstDownX, mFirstDownY,
                            mFirstUpX, mFirstUpY);
                if (mView instanceof TabStripView)
                    mSecondPosition = ((TabStripView) mView).getClickedPosition(mSecondDownX, mSecondDownY, x,
                            y);
                else if (mView instanceof TabStripViewGroup)
                    mSecondPosition = ((TabStripViewGroup) mView).getClickedPosition(mSecondDownX, mSecondDownY,
                            x, y);
                mLastUpTime = 0;
            } else {
                mDoubleClicked = false;
                mFirstPosition = PagerAdapter.POSITION_NONE;
                mSecondPosition = PagerAdapter.POSITION_NONE;
                mFirstUpX = x;
                mFirstUpY = y;
                mLastUpTime = event.getEventTime();
            }
            break;
        case MotionEvent.ACTION_CANCEL:

            break;
        }
    }

    return false;
}

From source file:org.dvbviewer.controller.ui.fragments.Remote.java

/**
 * Gets the gesture listener./*  w  w  w. jav a  2s .co  m*/
 *
 * @return the gesture listener
 * @author RayBa
 * @date 07.04.2013
 */
private SimpleOnGestureListener getGestureListener() {
    if (gestureListener == null) {
        gestureListener = new SimpleOnGestureListener() {

            private static final int SWIPE_MIN_DISTANCE = 120;
            private static final int SWIPE_MAX_OFF_PATH = 250;
            private static final int SWIPE_THRESHOLD_VELOCITY = 200;

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                try {
                    if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        // SWIPE LEFT
                        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                            return false;
                        }
                        flipper.setInAnimation(inFromRight);
                        flipper.setOutAnimation(outToLeft);
                        flipper.showPrevious();
                    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        // SWIPE RIGHT
                        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                            return false;
                        }
                        flipper.setInAnimation(inFromLeft);
                        flipper.setOutAnimation(outToRight);
                        flipper.showNext();
                    } else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                        // SWIPE UP
                        if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH) {
                        }
                        Toast.makeText(getActivity(), "Swipe up", Toast.LENGTH_SHORT).show();
                    } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                        // SWIPE DOWN
                        if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH) {
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
        };
    }
    return gestureListener;
}

From source file:com.baoyz.dribble.widget.SwipeHoverLayout.java

private void init() {

    ViewConfiguration viewConfiguration = ViewConfiguration.get(getContext());
    mScrollDistance = DimenUtil.dp2px(getContext(), 60);
    mCanScrollDistance = viewConfiguration.getScaledOverscrollDistance();
    mFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
    mTouchSlop = viewConfiguration.getScaledTouchSlop();

    mGestureDetector = new GestureDetectorCompat(getContext(), new GestureDetector.SimpleOnGestureListener() {

        @Override//from  ww w  .j a va  2s.c o m
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            mFlingShow = Math.abs(velocityX) >= mFlingVelocity;
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            int distance = Math.abs((int) (e2.getX() - mDownX));
            mDistanceShow = distance >= 10;
            dispatchSwipe(distance);
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            mDownX = (int) e.getX();
            mFlingShow = false;
            mDistanceShow = false;
            return true;
        }
    });
}

From source file:cn.ieclipse.af.view.ScrollLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }//w  ww . jav a  2s  .  c o  m
    mVelocityTracker.addMovement(event);

    final int action = event.getAction();
    final float x = event.getX();
    final float y = event.getY();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (debug) {
            Log.v(TAG, "event down!");
        }
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        mLastMotionX = x;
        break;

    case MotionEvent.ACTION_MOVE:
        int deltaX = (int) (mLastMotionX - x);
        mLastMotionX = x;

        scrollBy(deltaX, 0);
        break;

    case MotionEvent.ACTION_UP:
        if (debug) {
            Log.v(TAG, "event : up");
        }
        // if (mTouchState == TOUCH_STATE_SCROLLING) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000);
        int velocityX = (int) velocityTracker.getXVelocity();
        if (debug) {
            Log.v(TAG, "velocityX:" + velocityX);
        }

        if (velocityX > mSnapVelocity) {
            if (mCurScreen > 0) {
                // Fling enough to move left
                if (debug) {
                    Log.v(TAG, "snap left");
                }
                snapToScreen(mCurScreen - 1);
            } else {
                // TODO
                disableWipe(false);
                snapToDestination();
            }
        } else if (velocityX < -mSnapVelocity) {
            if (mCurScreen < getChildCount() - 1) {
                // Fling enough to move right
                if (debug) {
                    Log.v(TAG, "snap right");
                }
                snapToScreen(mCurScreen + 1);
            } else {
                // TODO
                disableWipe(false);
                snapToDestination();
            }
        } else {
            snapToDestination();
        }

        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        // }
        mTouchState = TOUCH_STATE_REST;
        break;
    case MotionEvent.ACTION_CANCEL:
        mTouchState = TOUCH_STATE_REST;
        break;
    }

    return true;
}

From source file:android.support.designox.widget.HeaderBehavior.java

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }/* w  w w .j a  v  a  2s .  c  o m*/

    switch (MotionEventCompat.getActionMasked(ev)) {
    case MotionEvent.ACTION_DOWN: {
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();

        if (parent.isPointInChildBounds(child, x, y) && canDragView(child)) {
            mLastMotionY = y;
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            ensureVelocityTracker();
        } else {
            return false;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            return false;
        }

        final int y = (int) MotionEventCompat.getY(ev, activePointerIndex);
        int dy = mLastMotionY - y;

        if (!mIsBeingDragged && Math.abs(dy) > mTouchSlop) {
            mIsBeingDragged = true;
            if (dy > 0) {
                dy -= mTouchSlop;
            } else {
                dy += mTouchSlop;
            }
        }

        if (mIsBeingDragged) {
            mLastMotionY = y;
            // We're being dragged so scroll the ABL
            scroll(parent, child, dy, getMaxDragOffset(child), 0);
        }
        break;
    }

    case MotionEvent.ACTION_UP:
        if (mVelocityTracker != null) {
            mVelocityTracker.addMovement(ev);
            mVelocityTracker.computeCurrentVelocity(1000);
            float yvel = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            fling(parent, child, -getScrollRangeForDragFling(child), 0, yvel);
        }
        // $FALLTHROUGH
    case MotionEvent.ACTION_CANCEL: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(ev);
    }

    return true;
}

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

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }/*from w w  w . j a  v a 2s. c  o m*/

    switch (MotionEventCompat.getActionMasked(ev)) {
    case MotionEvent.ACTION_DOWN: {
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();

        if (parent.isPointInChildBounds(child, x, y) && canDragView(child)) {
            mLastMotionY = y;
            mActivePointerId = ev.getPointerId(0);
            ensureVelocityTracker();
        } else {
            return false;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        if (activePointerIndex == -1) {
            return false;
        }

        final int y = (int) ev.getY(activePointerIndex);
        int dy = mLastMotionY - y;

        if (!mIsBeingDragged && Math.abs(dy) > mTouchSlop) {
            mIsBeingDragged = true;
            if (dy > 0) {
                dy -= mTouchSlop;
            } else {
                dy += mTouchSlop;
            }
        }

        if (mIsBeingDragged) {
            mLastMotionY = y;
            // We're being dragged so scroll the ABL
            scroll(parent, child, dy, getMaxDragOffset(child), 0);
        }
        break;
    }

    case MotionEvent.ACTION_UP:
        if (mVelocityTracker != null) {
            mVelocityTracker.addMovement(ev);
            mVelocityTracker.computeCurrentVelocity(1000);
            float yvel = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            fling(parent, child, -getScrollRangeForDragFling(child), 0, yvel);
        }
        // $FALLTHROUGH
    case MotionEvent.ACTION_CANCEL: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(ev);
    }

    return true;
}

From source file:cn.ieclipse.af.view.ScrollLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (debug) {/*w  w  w  .  j a v  a2  s  .c  o m*/
        Log.e(TAG, "onInterceptTouchEvent-slop:" + mTouchSlop);
    }

    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
        return true;
    }

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

    switch (action) {
    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(mLastMotionX - x);
        if (xDiff > mTouchSlop) {
            mTouchState = TOUCH_STATE_SCROLLING;

        }
        break;

    case MotionEvent.ACTION_DOWN:
        mLastMotionX = x;
        mLastMotionY = y;
        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        mTouchState = TOUCH_STATE_REST;
        break;
    }

    return mTouchState != TOUCH_STATE_REST;
}

From source file:cn.colink.commumication.swipelistview.SwipeListView.java

/**
 * @see android.widget.ListView#onInterceptTouchEvent(android.view.MotionEvent)
 *//*from w  w  w  .  jav  a  2  s  .  c o m*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = MotionEventCompat.getActionMasked(ev);
    final float x = ev.getX();
    final float y = ev.getY();

    if (touchState == TOUCH_STATE_SCROLLING_X) {
        return touchListener.onTouch(this, ev);
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE:
        checkInMoving(x, y);
        return touchState == TOUCH_STATE_SCROLLING_Y;
    case MotionEvent.ACTION_DOWN:
        touchListener.onTouch(this, ev);
        touchState = TOUCH_STATE_REST;
        lastMotionX = x;
        lastMotionY = y;
        return false;
    case MotionEvent.ACTION_CANCEL:
        touchState = TOUCH_STATE_REST;
        break;
    case MotionEvent.ACTION_UP:
        touchListener.onTouch(this, ev);
        return touchState == TOUCH_STATE_SCROLLING_Y;
    default:
        break;
    }

    return super.onInterceptTouchEvent(ev);
}

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

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    boolean dispatchEventToHelper = mInterceptingEvents;

    switch (MotionEventCompat.getActionMasked(event)) {
    case MotionEvent.ACTION_DOWN:
        mInterceptingEvents = parent.isPointInChildBounds(child, (int) event.getX(), (int) event.getY());
        dispatchEventToHelper = mInterceptingEvents;
        break;//from ww  w  .  j  av a 2 s. c o  m
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        // Reset the ignore flag for next time
        mInterceptingEvents = false;
        break;
    }

    if (dispatchEventToHelper) {
        ensureViewDragHelper(parent);
        return mViewDragHelper.shouldInterceptTouchEvent(event);
    }
    return false;
}