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.aosijia.dragonbutler.ui.widget.UnderlinePageIndicator.java

@SuppressLint("ClickableViewAccessibility")
public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*  ww  w.  j ava  2 s  . 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;

            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:co.dift.ui.SwipeToAction.java

/** Private methods **/
private void init() {
    gestureDetector = new GestureDetectorCompat(recyclerView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override//from   w  w  w. j  a  v  a2s  .com
                public void onLongPress(MotionEvent e) {
                    logger.d("onLongPress called!");
                    if (touchedViewHolder != null) {
                        swipeListener.onLongClick(touchedViewHolder.getItemData());
                    }
                }

                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    logger.d("onSingleTapUp called!");
                    if (touchedViewHolder != null) {
                        swipeListener.onClick(touchedViewHolder.getItemData());
                        return true;
                    }
                    return super.onSingleTapUp(e);
                }
            });

    recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
            gestureDetector.onTouchEvent(ev);

            switch (ev.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                // starting point
                downX = ev.getX();
                downY = ev.getY();

                downTime = new Date().getTime();

                // which item are we touching
                resolveItem(downX, downY);

                break;
            }

            case MotionEvent.ACTION_UP: {
                upX = ev.getX();
                upY = ev.getY();
                upTime = new Date().getTime();

                resolveState();
                break;
            }

            case MotionEvent.ACTION_MOVE: {
                final float x = ev.getX();
                final float dx = x - downX;

                if (!shouldMove(dx))
                    break;

                // current position. moving only over x-axis
                frontViewLastX = frontViewX + dx + (dx > 0 ? -getRevealThreshold() : getRevealThreshold());
                //                        logger.d("dx: %f, frontViewLastX: %f", dx, frontViewLastX);
                if (maxSwipeXPosition != null) {
                    if (frontViewLastX > 0 && frontViewLastX > maxSwipeXPosition) {
                        frontViewLastX = maxSwipeXPosition;
                    } else if (frontViewLastX <= -maxSwipeXPosition) {
                        frontViewLastX = -maxSwipeXPosition;
                    }
                }
                frontView.setX(frontViewLastX);

                if (frontViewLastX > 0) {
                    revealRight();
                } else {
                    revealLeft();
                }

                break;
            }

            case MotionEvent.ACTION_CANCEL: {
                resolveState();

                break;
            }
            }

            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            logger.d("onTouchEvent called");
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            logger.d("onRequestDisallowInterceptTouchEvent called");
        }
    });
}

From source file:com.acious.android.paginationseekbar.PaginationSeekBar.java

private void updateDragging(MotionEvent ev) {
    setHotspot(ev.getX(), ev.getY());
    int x = (int) ev.getX();
    Rect oldBounds = mThumb.getBounds();
    int halfThumb = oldBounds.width() / 2;
    int addedThumb = mAddedTouchBounds;
    int newX = x - mDragOffset + halfThumb;
    int left = getPaddingLeft() + halfThumb + addedThumb;
    int right = getWidth() - (getPaddingRight() + halfThumb + addedThumb);
    if (newX < left) {
        newX = left;/*from   w ww.  j a v a 2  s .com*/
    } else if (newX > right) {
        newX = right;
    }

    int available = right - left;
    float scale = (float) (newX - left) / (float) available;
    if (isRtl()) {
        scale = 1f - scale;
    }
    int progress = Math.round((scale * (mMax - mMin)) + mMin);
    setProgress(progress, true);
}

From source file:net.homelinux.penecoptero.android.citybikes.app.MainActivity.java

protected void applyMapViewLongPressListener(MapView mapView) {
    final MapView finalMapView = mapView;

    final GestureDetector gd = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        @Override/*w w  w  .  j  av a2s  . co  m*/
        public void onLongPress(MotionEvent e) {
            //Log.i("CityBikes","LONG PRESS!");
            Projection astral = finalMapView.getProjection();
            GeoPoint center = astral.fromPixels((int) e.getX(), (int) e.getY());
            locator.lockCenter(center);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // TODO Auto-generated method stub
            //Log.i("CityBikes","Double tap!");
            Projection astral = finalMapView.getProjection();
            GeoPoint center = astral.fromPixels((int) e.getX(), (int) e.getY());
            locator.lockCenter(center);
            return super.onDoubleTap(e);
        }

    });
    mapView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent ev) {
            return gd.onTouchEvent(ev);
        }
    });
}

From source file:com.test.hwautotest.emmagee.service.EmmageeService.java

/**
 * create a floating window to show real-time data.
 *//*from   www .j  av a  2s  .  c o m*/
private void createFloatingWindow() {
    SharedPreferences shared = getSharedPreferences("float_flag", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = shared.edit();
    editor.putInt("float", 1);
    editor.commit();
    windowManager = (WindowManager) getApplicationContext().getSystemService("window");
    wmParams = ((MyApplication) getApplication()).getMywmParams();
    wmParams.type = 2002;
    wmParams.flags |= 8;
    wmParams.gravity = Gravity.LEFT | Gravity.TOP;
    wmParams.x = 0;
    wmParams.y = 0;
    wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.format = 1;
    windowManager.addView(viFloatingWindow, wmParams);
    viFloatingWindow.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            x = event.getRawX();
            y = event.getRawY();
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // state = MotionEvent.ACTION_DOWN;
                startX = x;
                startY = y;
                mTouchStartX = event.getX();
                mTouchStartY = event.getY();
                Log.d("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY);
                break;
            case MotionEvent.ACTION_MOVE:
                // state = MotionEvent.ACTION_MOVE;
                updateViewPosition();
                break;

            case MotionEvent.ACTION_UP:
                // state = MotionEvent.ACTION_UP;
                updateViewPosition();
                //               showImg();//?
                mTouchStartX = mTouchStartY = 0;
                break;
            }
            return true;
        }
    });

    //      btnWifi.setOnClickListener(new OnClickListener() {
    //         @Override
    //         public void onClick(View v) {
    //            try {
    //               btnWifi = (Button) viFloatingWindow.findViewById(R.id.wifi);
    //               String buttonText = (String) btnWifi.getText();
    //               String wifiText = getResources().getString(
    //                     R.string.openwifi);
    //               if (buttonText.equals(wifiText)) {
    //                  wifiManager.setWifiEnabled(true);
    //                  btnWifi.setText(R.string.closewifi);
    //               } else {
    //                  wifiManager.setWifiEnabled(false);
    //                  btnWifi.setText(R.string.openwifi);
    //               }
    //            } catch (Exception e) {
    //               Toast.makeText(viFloatingWindow.getContext(), "?wifi",
    //                     Toast.LENGTH_LONG).show();
    //               Log.e(LOG_TAG, e.toString());
    //            }
    //         }
    //      });

    //?
    imgClose.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            showMemi();
        }
    });
}

From source file:com.andview.refreshview.swipe.SwipeMenuLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mVelocityTracker == null)
        mVelocityTracker = VelocityTracker.obtain();
    mVelocityTracker.addMovement(ev);/*  w  w w. ja va  2  s.  c  om*/
    int dx;
    int dy;
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastX = (int) ev.getX();
        mLastY = (int) ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!isSwipeEnable())
            break;
        int disX = (int) (mLastX - ev.getX());
        int disY = (int) (mLastY - ev.getY());
        if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) {
            mDragging = true;
        }
        if (mDragging) {
            if (mSwipeCurrentHorizontal == null || shouldResetSwipe) {
                if (disX < 0) {
                    if (mSwipeLeftHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                } else {
                    if (mSwipeRightHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                }
            }
            scrollBy(disX, 0);
            mLastX = (int) ev.getX();
            mLastY = (int) ev.getY();
            shouldResetSwipe = false;
        }
        break;
    case MotionEvent.ACTION_UP:
        dx = (int) (mDownX - ev.getX());
        dy = (int) (mDownY - ev.getY());
        mDragging = false;
        mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity);
        int velocityX = (int) mVelocityTracker.getXVelocity();
        int velocity = Math.abs(velocityX);
        if (velocity > mScaledMinimumFlingVelocity) {
            if (mSwipeCurrentHorizontal != null) {
                int duration = getSwipeDuration(ev, velocity);
                if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) {
                    if (velocityX < 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                } else {
                    if (velocityX > 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                }
                ViewCompat.postInvalidateOnAnimation(this);
            }
        } else {
            judgeOpenClose(dx, dy);
        }
        mVelocityTracker.clear();
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop
                || isMenuOpen()) {
            ev.setAction(MotionEvent.ACTION_CANCEL);
            super.onTouchEvent(ev);
            return true;
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        mDragging = false;
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        } else {
            dx = (int) (mDownX - ev.getX());
            dy = (int) (mDownY - ev.getY());
            judgeOpenClose(dx, dy);
        }
        break;
    }
    return super.onTouchEvent(ev);
}

From source file:com.android.hiparker.lierda_light.view.viewpagerindicator.LinePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from  ww w.  jav a2s.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;

            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.android.messaging.ui.conversationlist.ConversationListSwipeHelper.java

@Override
public void onTouchEvent(final RecyclerView recyclerView, final MotionEvent event) {
    // We should only be here if we intercepted the touch due to swipe.
    Assert.isTrue(mIsSwiping);//from  w w  w  .  j  av a 2s. c  om

    // We are now tracking a swipe gesture.
    mVelocityTracker.addMovement(event);

    final int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_OUTSIDE:
    case MotionEvent.ACTION_MOVE:
        if (hasValidGestureSwipeTarget()) {
            mListItemView.setSwipeTranslationX(event.getX() - mInitialX);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (hasValidGestureSwipeTarget()) {
            final float maxVelocity = mMaximumFlingVelocity;
            mVelocityTracker.computeCurrentVelocity(UNIT_SECONDS, maxVelocity);
            final float velocityX = getLastComputedXVelocity();

            final float translationX = mListItemView.getSwipeTranslationX();

            int swipeDirection = SWIPE_DIRECTION_NONE;
            if (translationX != 0) {
                swipeDirection = translationX > 0 ? SWIPE_DIRECTION_RIGHT : SWIPE_DIRECTION_LEFT;
            } else if (velocityX != 0) {
                swipeDirection = velocityX > 0 ? SWIPE_DIRECTION_RIGHT : SWIPE_DIRECTION_LEFT;
            }

            final boolean fastEnough = isTargetSwipedFastEnough();
            final boolean farEnough = isTargetSwipedFarEnough();

            final boolean shouldDismiss = (fastEnough || farEnough);

            if (shouldDismiss) {
                if (fastEnough) {
                    animateDismiss(mListItemView, velocityX);
                } else {
                    animateDismiss(mListItemView, swipeDirection);
                }
            } else {
                animateRestore(mListItemView, velocityX);
            }

            onSwipeGestureEnd(mListItemView, shouldDismiss ? swipeDirection : SWIPE_DIRECTION_NONE);
        } else {
            onGestureEnd();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (hasValidGestureSwipeTarget()) {
            animateRestore(mListItemView, 0f);
            onSwipeGestureEnd(mListItemView, SWIPE_DIRECTION_NONE);
        } else {
            onGestureEnd();
        }
        break;
    }
}

From source file:com.appsimobile.appsii.SidebarHotspot.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        mVelocityTracker = VelocityTracker.obtain();
        mVelocityTracker.addMovement(e);
        // remove the background to make sure it does not overlap
        // the sidebar

        mIsDragOpening = true;/*from   ww w .j av a 2 s . c o  m*/
        setBackgroundResource(0);

        float x = e.getX();
        float y = e.getY();

        if (mCallback != null) {
            mSwipeListener = mCallback.open(this, Gesture.TO_CENTER, (int) x, (int) y);
            mSwipeInProgress = mSwipeListener != null;
            mState = STATE_AWAITING_RELEASE;
            if (mVibrate) {
                vibrate();
            }

            return true;
        }
        return false;
    }
    case MotionEvent.ACTION_MOVE:
        mVelocityTracker.addMovement(e);
        float x = e.getX();
        float y = e.getY();
        return detectSwipe(x, y, e);
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        cancelMotionHandling(e, false);
        return false;
    }

    return super.onTouchEvent(e);
}

From source file:com.cloverstudio.spika.CameraCropActivity.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    ImageView view = (ImageView) v;
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        savedMatrix.set(matrix);/*from  www.  j  a va  2s  .  c  o m*/
        start.set(event.getX(), event.getY());
        mode = DRAG;
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        oldDist = spacing(event);
        if (oldDist > 10f) {
            savedMatrix.set(matrix);
            midPoint(mid, event);
            mode = ZOOM;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        mode = NONE;
        break;
    case MotionEvent.ACTION_MOVE:
        if (mode == DRAG) {
            // ...
            matrix.set(savedMatrix);
            matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
            start_x = event.getX() - start.x;
            start_y = event.getY() - start.y;
        } else if (mode == ZOOM) {
            float newDist = spacing(event);
            if (newDist > 10f) {
                matrix.set(savedMatrix);
                float scale = newDist / oldDist;
                mAspect = scale;
                matrix.postScale(scale, scale, mid.x, mid.y);
            }
        }
        break;
    }

    view.setImageMatrix(matrix);
    view.invalidate();
    return true;
}