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:atownsend.swipeopenhelper.SwipeOpenItemTouchHelper.java

private View findChildView(MotionEvent event) {
    // first check elevated views, if none, then call RV
    final float x = event.getX();
    final float y = event.getY();
    if (selected != null) {
        final View selectedView = selected.getViewHolder().itemView;
        if (hitTest(selectedView, x, y, selectedStartX + dX, selectedStartY + dY)) {
            return selectedView;
        }/* w w  w  .  j av  a  2 s.  c o m*/
    }
    for (int i = recoverAnimations.size() - 1; i >= 0; i--) {
        final RecoverAnimation anim = recoverAnimations.get(i);
        final View view = anim.mViewHolder.getViewHolder().itemView;
        if (hitTest(view, x, y, anim.mX, anim.mY)) {
            return view;
        }
    }
    return recyclerView.findChildViewUnder(x, y);
}

From source file:com.benefit.buy.library.viewpagerindicator.TitlePageIndicator.java

@Override
public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from w  w  w.  ja v  a2s  . co  m*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;
    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;
        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }
        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();
            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < (count - 1)) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            } else {
                //Middle third
                if ((mCenterItemClickListener != null) && (action != MotionEvent.ACTION_CANCEL)) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }
        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) {
            mViewPager.endFakeDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    return true;
}

From source file:am.widget.scalerecyclerview.ScaleRecyclerView.java

/**
 * ??//w  w  w .  j a v  a  2  s. c o  m
 *
 * @param e 
 */
protected boolean dispatchDoubleTapEvent(MotionEvent e) {
    // ?ACTION_UP??
    if (e.getAction() != MotionEvent.ACTION_UP)
        return false;
    // ????View?
    if (!mShouldReactDoubleTab)
        return false;
    // ??
    onDoubleTapEvent(e);
    if (mListener != null && mListener.onDoubleTap(this))
        return true;
    // ?
    final float targetScale = getDoubleTapScale(mScale);
    if (targetScale == mScale)
        return false;
    mScaleHelper.scale(mScale, targetScale, e.getX(), e.getY());
    return true;
}

From source file:net.sourceforge.kalimbaradio.androidapp.activity.DownloadActivity.java

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

    DownloadService downloadService = getDownloadService();
    if (downloadService == null) {
        return false;
    } else if (!downloadService.isAd()) {

        // Right to Left swipe
        if (e1.getX() - e2.getX() > swipeDistance && Math.abs(velocityX) > swipeVelocity) {
            warnIfNetworkOrStorageUnavailable();
            if (downloadService.getCurrentPlayingIndex() < downloadService.size() - 1) {
                downloadService.next();/*from w  w w . j av  a 2s.c o  m*/
                onCurrentChanged();
                onProgressChanged();
            }
            return true;
        }

        // Left to Right swipe
        if (e2.getX() - e1.getX() > swipeDistance && Math.abs(velocityX) > swipeVelocity) {
            warnIfNetworkOrStorageUnavailable();
            downloadService.previous();
            onCurrentChanged();
            onProgressChanged();
            return true;
        }

        // Top to Bottom swipe
        if (e2.getY() - e1.getY() > swipeDistance && Math.abs(velocityY) > swipeVelocity) {
            warnIfNetworkOrStorageUnavailable();
            downloadService.seekTo(downloadService.getPlayerPosition() + 30000);
            onProgressChanged();
            return true;
        }

        // Bottom to Top swipe
        if (e1.getY() - e2.getY() > swipeDistance && Math.abs(velocityY) > swipeVelocity) {
            warnIfNetworkOrStorageUnavailable();
            downloadService.seekTo(downloadService.getPlayerPosition() - 8000);
            onProgressChanged();
            return true;
        }
    }
    return false;

}

From source file:co.bytera.twodimensionalviewpager.fragment.TwoDimensionalViewPagerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_layout, container, false);

    viewPager = (ViewPager) v.findViewById(R.id.viewPager);

    /*/*  w  w w  . ja  v  a 2  s .  com*/
     * To detect the gesture (UP/DOWN/LEFT/RIGHT)
     */
    final GestureDetector gesture = new GestureDetector(getActivity(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onDown(MotionEvent e) {
                    return true;
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    final int SWIPE_MIN_DISTANCE = 120;
                    final int SWIPE_MAX_OFF_PATH = 250;
                    final int SWIPE_THRESHOLD_VELOCITY = 200;

                    try {
                        if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
                                && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                            /*
                             * finger motion is UP side.  page is moving to next row
                             */
                            adapter.nextRow();
                            adapter.notifyDataSetChanged();
                        } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
                                && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                            /*
                             * finger motion is DOWN side.  page is moving to previous row
                             */
                            adapter.previousRow();
                            adapter.notifyDataSetChanged();
                        } else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            /*
                             * more action can be done here
                             */

                        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            /*
                             * more action can be done here
                             */

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return super.onFling(e1, e2, velocityX, velocityY);
                }
            });

    /*
     *  set on touch listener to the viewPager
     */
    viewPager.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return gesture.onTouchEvent(event);
        }
    });

    return v;
}

From source file:cfb.com.dailydevelopment4.example12.library.TitlePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from   w ww.j  a  v  a 2s. c  o m*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:cn.chuangblog.simplebanner1.indicator.TitlePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//from ww w.  j a v a  2  s .  co  m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = InfiniteViewPager.FakePositionHelper.getAdapterSize(mViewPager);
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.android.core.view.indicator.TitlePageIndicator.java

@SuppressLint("ClickableViewAccessibility")
public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from  www.  ja  v a  2 s .co m*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

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

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);/*w  ww.j a va  2 s  .  c o m*/
    final int action = MotionEventCompat.getActionMasked(ev);
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        if (isEnabled() && hitThumb(x, y)) {
            mTouchMode = TOUCH_MODE_DOWN;
            mTouchX = x;
            mTouchY = y;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        switch (mTouchMode) {
        case TOUCH_MODE_IDLE:
            // Didn't target the thumb, treat normally.
            break;

        case TOUCH_MODE_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();
            if (Math.abs(x - mTouchX) > mTouchSlop || Math.abs(y - mTouchY) > mTouchSlop) {
                mTouchMode = TOUCH_MODE_DRAGGING;
                getParent().requestDisallowInterceptTouchEvent(true);
                mTouchX = x;
                mTouchY = y;
                return true;
            }
            break;
        }

        case TOUCH_MODE_DRAGGING: {
            final float x = ev.getX();
            final int thumbScrollRange = getThumbScrollRange();
            final float thumbScrollOffset = x - mTouchX;
            float dPos;
            if (thumbScrollRange != 0) {
                dPos = thumbScrollOffset / thumbScrollRange;
            } else {
                // If the thumb scroll range is empty, just use the
                // movement direction to snap on or off.
                dPos = thumbScrollOffset > 0 ? 1 : -1;
            }
            if (ViewUtils.isLayoutRtl(this)) {
                dPos = -dPos;
            }
            final float newPos = constrain(mThumbPosition + dPos, 0, 1);
            if (newPos != mThumbPosition) {
                mTouchX = x;
                setThumbPosition(newPos);
            }
            return true;
        }
        }
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        if (mTouchMode == TOUCH_MODE_DRAGGING) {
            stopDrag(ev);
            // Allow super class to handle pressed state, etc.
            super.onTouchEvent(ev);
            return true;
        }
        mTouchMode = TOUCH_MODE_IDLE;
        mVelocityTracker.clear();
        break;
    }
    }

    return super.onTouchEvent(ev);
}