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:android.hqs.view.pager.indicator.CirclePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*  w ww  .  j ava  2  s  .  c om*/
    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;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        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.achep.acdisplay.ui.widgets.CircleView.java

public boolean sendTouchEvent(@NonNull MotionEvent event) {
    final int action = event.getActionMasked();

    // If current circle is canceled then
    // ignore all actions except of touch down (to reset state.)
    if (mCanceled && action != MotionEvent.ACTION_DOWN)
        return false;

    // Cancel the current circle on two-or-more-fingers touch.
    if (event.getPointerCount() > 1) {
        cancelCircle();/*from   w ww  . ja  va2 s .c om*/
        return false;
    }

    final float x = event.getX();
    final float y = event.getY();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        clearAnimation();
        Config config = Config.getInstance();

        // Corner actions
        int width = getWidth();
        int height = getHeight();
        int radius = Math.min(width, height) / 3;
        if (MathUtils.isInCircle(x, y, 0, 0, radius)) { // Top left
            mCornerActionId = config.getCornerActionLeftTop();
        } else if (MathUtils.isInCircle(x, y, -width, 0, radius)) { // Top right
            mCornerActionId = config.getCornerActionRightTop();
        } else if (MathUtils.isInCircle(x, y, 0, -height, radius)) { // Bottom left
            mCornerActionId = config.getCornerActionLeftBottom();
        } else if (MathUtils.isInCircle(x, y, -width, -height, radius)) { // Bottom right
            mCornerActionId = config.getCornerActionRightBottom();
        } else {
            // The default action is unlocking.
            mCornerActionId = Config.CORNER_UNLOCK;
        }

        // Update colors and icon drawable.
        boolean needsColorReset = updateIcon();
        setInnerColor(getColor(config.getCircleInnerColor()), needsColorReset);
        setOuterColor(getColor(config.getCircleOuterColor()));

        // Initialize circle
        mRadiusTargetAimed = false;
        mRadiusMaxPeak = 0;
        mPoint[0] = x;
        mPoint[1] = y;
        mCanceled = false;

        if (mHandler.hasMessages(ACTION_UNLOCK)) {
            // Cancel unlocking process.
            mHandler.sendEmptyMessage(ACTION_UNLOCK_CANCEL);
        }

        mHandler.removeCallbacksAndMessages(null);
        mHandler.sendEmptyMessageDelayed(MSG_CANCEL, 1000);
        mHandler.sendEmptyMessage(ACTION_START);
        break;
    case MotionEvent.ACTION_MOVE:
        setRadius(x, y);
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mRadiusTargetAimed || action == MotionEvent.ACTION_CANCEL) {
            cancelCircle();
            break;
        }

        startUnlock();
        break;
    }
    return true;
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

@Override
public boolean onDoubleTap(MotionEvent arg0) {
    mZoomIn = !mZoomIn;/*from  w  w w  .  j a  v a 2 s.  c om*/
    float scale = 1.0f;
    final float x = arg0.getX();
    final float y = arg0.getY();
    if (mZoomIn) {
        scale = MasterImage.getImage().getMaxScaleFactor();
    }
    if (scale != MasterImage.getImage().getScaleFactor()) {
        if (mAnimatorScale != null) {
            mAnimatorScale.cancel();
        }
        mAnimatorScale = ValueAnimator.ofFloat(MasterImage.getImage().getScaleFactor(), scale);
        float translateX = (getWidth() / 2 - x);
        float translateY = (getHeight() / 2 - y);
        Point translation = MasterImage.getImage().getTranslation();
        int startTranslateX = translation.x;
        int startTranslateY = translation.y;
        if (scale != 1.0f) {
            translation.x = (int) (mOriginalTranslation.x + translateX);
            translation.y = (int) (mOriginalTranslation.y + translateY);
        } else {
            translation.x = 0;
            translation.y = 0;
        }
        constrainTranslation(translation, scale);

        startAnimTranslation(startTranslateX, translation.x, startTranslateY, translation.y,
                mAnimationZoomDelay);
        mAnimatorScale.setDuration(mAnimationZoomDelay);
        mAnimatorScale.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                MasterImage.getImage().setScaleFactor((Float) animation.getAnimatedValue());
                invalidate();
            }
        });
        mAnimatorScale.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                applyTranslationConstraints();
                MasterImage.getImage().needsUpdatePartialPreview();
                invalidate();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        mAnimatorScale.start();
    }
    return true;
}

From source file:cn.edu.zafu.easemob.imagecoverflow.CoverFlowView.java

private void touchEnded(MotionEvent event) {
    float pos = (event.getX() / mWidth) * MOVE_POS_MULTIPLE - 5;
    pos /= 2;// ww w . j a v  a2s  . co m

    if (mTouchMoved || (mOffset - Math.floor(mOffset)) != 0) {
        mStartOffset += mTouchStartPos - pos;
        mOffset = mStartOffset;

        mVelocity.addMovement(event);

        mVelocity.computeCurrentVelocity(1000);
        double speed = mVelocity.getXVelocity();

        speed = (speed / mWidth) * MOVE_SPEED_MULTIPLE;
        if (speed > MAX_SPEED)
            speed = MAX_SPEED;
        else if (speed < -MAX_SPEED)
            speed = -MAX_SPEED;

        startAnimation(-speed);
    } else {
        Log.e(VIEW_LOG_TAG, " touch ==>" + event.getX() + " , " + event.getY());
        if (mTouchRect != null) {
            if (mTouchRect.contains(event.getX(), event.getY()) && mCoverFlowListener != null
                    && topImageClickEnable && !mLongClickTriggled) {
                final int actuallyPosition = mTopImageIndex;

                mCoverFlowListener.topImageClicked(this, actuallyPosition);
            }
        }
    }

    mVelocity.clear();
    mVelocity.recycle();
}

From source file:com.aretha.slidemenu.SlideMenu.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final float x = ev.getX();
    final float y = ev.getY();
    final int currentState = mCurrentState;
    if (STATE_DRAG == currentState || STATE_SCROLL == currentState) {
        return true;
    }/*  w  w w .j a  va2  s.  c  o m*/
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mPressedX = mLastMotionX = x;
        mIsTapContent = isTapContent(x, y);
        return isOpen() && mIsTapContent;
    case MotionEvent.ACTION_MOVE:
        float distance = x - mPressedX;
        if (Math.abs(distance) >= mTouchSlop && mIsTapContent) {
            if (!canScroll(this, (int) distance, (int) x, (int) y)) {
                setCurrentState(STATE_DRAG);
                return true;
            }
        }
    }

    return super.onInterceptTouchEvent(ev);
}

From source file:cn.edu.zafu.easemob.imagecoverflow.CoverFlowView.java

private void touchBegan(MotionEvent event) {
    endAnimation();//w w  w .  j av  a  2 s .  c o m

    float x = event.getX();
    mTouchStartX = x;
    mTouchStartY = event.getY();
    mStartTime = AnimationUtils.currentAnimationTimeMillis();
    mStartOffset = mOffset;

    mTouchMoved = false;

    mTouchStartPos = (x / mWidth) * MOVE_POS_MULTIPLE - 5;
    mTouchStartPos /= 2;

    mVelocity = VelocityTracker.obtain();
    mVelocity.addMovement(event);
}

From source file:com.aibinong.tantan.ui.widget.CircleNoPageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/* w  w w  .ja 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 = mRealCountGetter.getRealCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    //                            mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    //                            mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        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.acious.android.paginationseekbar.PaginationSeekBar.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }//from   w  w  w .j  a v a 2s.  co  m
    int actionMasked = MotionEventCompat.getActionMasked(event);
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN:
        mDownX = event.getX();
        startDragging(event, isInScrollingContainer());
        break;
    case MotionEvent.ACTION_MOVE:
        if (isDragging()) {
            updateDragging(event);
        } else {
            final float x = event.getX();
            if (Math.abs(x - mDownX) > mTouchSlop) {
                startDragging(event, false);
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        notifyPageChange(mValue, true);
    case MotionEvent.ACTION_CANCEL:
        stopDragging();
        break;
    }
    return true;
}

From source file:com.appunite.list.FastScroller.java

boolean onTouchEvent(MotionEvent me) {
    if (mState == STATE_NONE) {
        return false;
    }/*w w  w. j av a2  s.c  o  m*/

    final int action = me.getAction();

    if (action == MotionEvent.ACTION_DOWN) {
        if (isPointInside(me.getX(), me.getY())) {
            if (!mList.isInScrollingContainerUnhide()) {
                beginDrag();
                return true;
            }
            mInitialTouchY = me.getY();
            startPendingDrag();
        }
    } else if (action == MotionEvent.ACTION_UP) { // don't add ACTION_CANCEL here
        if (mPendingDrag) {
            // Allow a tap to scroll.
            beginDrag();

            final int viewHeight = mList.getHeight();
            // Jitter
            int newThumbY = (int) me.getY() - mThumbH + 10;
            if (newThumbY < 0) {
                newThumbY = 0;
            } else if (newThumbY + mThumbH > viewHeight) {
                newThumbY = viewHeight - mThumbH;
            }
            mThumbY = newThumbY;
            scrollTo((float) mThumbY / (viewHeight - mThumbH));

            cancelPendingDrag();
            // Will hit the STATE_DRAGGING check below
        }
        if (mState == STATE_DRAGGING) {
            if (mList != null) {
                // ViewGroup does the right thing already, but there might
                // be other classes that don't properly reset on touch-up,
                // so do this explicitly just in case.
                mList.requestDisallowInterceptTouchEvent(false);
                mList.reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }
            setState(STATE_VISIBLE);
            final Handler handler = mHandler;
            handler.removeCallbacks(mScrollFade);
            if (!mAlwaysShow) {
                handler.postDelayed(mScrollFade, 1000);
            }

            mList.invalidate();
            return true;
        }
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (mPendingDrag) {
            final float y = me.getY();
            if (Math.abs(y - mInitialTouchY) > mScaledTouchSlop) {
                setState(STATE_DRAGGING);
                if (mListAdapter == null && mList != null) {
                    getSectionsFromIndexer();
                }
                if (mList != null) {
                    mList.requestDisallowInterceptTouchEvent(true);
                    mList.reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
                }

                cancelFling();
                cancelPendingDrag();
                // Will hit the STATE_DRAGGING check below
            }
        }
        if (mState == STATE_DRAGGING) {
            final int viewHeight = mList.getHeight();
            // Jitter
            int newThumbY = (int) me.getY() - mThumbH + 10;
            if (newThumbY < 0) {
                newThumbY = 0;
            } else if (newThumbY + mThumbH > viewHeight) {
                newThumbY = viewHeight - mThumbH;
            }
            if (Math.abs(mThumbY - newThumbY) < 2) {
                return true;
            }
            mThumbY = newThumbY;
            // If the previous scrollTo is still pending
            if (mScrollCompleted) {
                scrollTo((float) mThumbY / (viewHeight - mThumbH));
            }
            return true;
        }
    } else if (action == MotionEvent.ACTION_CANCEL) {
        cancelPendingDrag();
    }
    return false;
}

From source file:com.aretha.slidemenu.SlideMenu.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    final float x = event.getX();
    final float y = event.getY();
    final int currentState = mCurrentState;
    final boolean isTapContent = mIsTapContent;

    mVelocityTracker.addMovement(event);

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mPressedX = mLastMotionX = x;//from   w  w w  .  java2 s  .  co m
        mIsTapContent = isTapContent(x, y);
        if (mIsTapContent) {
            mScroller.abortAnimation();
        }
        mVelocityTracker = VelocityTracker.obtain();
        break;
    case MotionEvent.ACTION_MOVE:
        if (Math.abs(x - mPressedX) >= mTouchSlop && isTapContent && currentState != STATE_DRAG) {
            setCurrentState(STATE_DRAG);
        }
        if (STATE_DRAG != currentState) {
            mLastMotionX = x;
            return false;
        }

        drag(mLastMotionX, x);
        mLastMotionX = x;
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_OUTSIDE:
        if (STATE_DRAG == currentState) {
            mVelocityTracker.computeCurrentVelocity(1000);
            endDrag(x, mVelocityTracker.getXVelocity());
        } else if (isTapContent) {
            performContentClick();
        }
        mVelocityTracker.recycle();
        mIsTapContent = false;
        break;
    }

    return true;
}