Example usage for android.view MotionEvent ACTION_MOVE

List of usage examples for android.view MotionEvent ACTION_MOVE

Introduction

In this page you can find the example usage for android.view MotionEvent ACTION_MOVE.

Prototype

int ACTION_MOVE

To view the source code for android.view MotionEvent ACTION_MOVE.

Click Source Link

Document

Constant for #getActionMasked : A change has happened during a press gesture (between #ACTION_DOWN and #ACTION_UP ).

Usage

From source file:cn.fantasee.swipwmenulistview.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(View, MotionEvent)
 *//*from  w ww .  j a v  a  2 s.  c om*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (!isSwipeEnabled()) {
        return false;
    }

    if (viewWidth < 2) {
        viewWidth = swipeListView.getWidth();
    }

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (paused && downPosition != ListView.INVALID_POSITION) {
            return false;
        }
        swipeCurrentAction = SwipeListView.SWIPE_ACTION_NONE;

        int childCount = swipeListView.getChildCount();
        int[] listViewCoords = new int[2];
        swipeListView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = swipeListView.getChildAt(i);
            child.getHitRect(rect);

            int childPosition = swipeListView.getPositionForView(child);

            // dont allow swiping if this is on the header or footer or IGNORE_ITEM_VIEW_TYPE or enabled is false on the adapter
            boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition)
                    && swipeListView.getAdapter().getItemViewType(childPosition) >= 0;

            if (allowSwipe && rect.contains(x, y)) {
                setParentView(child);
                setFrontView(child.findViewById(swipeFrontView), childPosition);

                downX = motionEvent.getRawX();
                downPosition = childPosition;

                frontView.setClickable(!opened.get(downPosition));
                frontView.setLongClickable(!opened.get(downPosition));

                velocityTracker = VelocityTracker.obtain();
                velocityTracker.addMovement(motionEvent);
                if (swipeBackView > 0) {
                    setBackView(child.findViewById(swipeBackView));
                }
                break;
            }
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (velocityTracker == null || !swiping || downPosition == ListView.INVALID_POSITION) {
            break;
        }

        float deltaX = motionEvent.getRawX() - downX;
        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        if (!opened.get(downPosition)) {
            if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && velocityTracker.getXVelocity() > 0) {
                velocityX = 0;
            }
            if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && velocityTracker.getXVelocity() < 0) {
                velocityX = 0;
            }
        }
        float velocityY = Math.abs(velocityTracker.getYVelocity());
        boolean swap = false;
        boolean swapRight = false;
        if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity && velocityY * 2 < velocityX) {
            swapRight = velocityTracker.getXVelocity() > 0;
            if (SwipeListView.DEBUG) {
                Log.d(SwipeListView.TAG, "swapRight: " + swapRight + " - swipingRight: " + swipingRight);
            }
            if (swapRight != swipingRight && swipeActionLeft != swipeActionRight) {
                swap = false;
            } else if (opened.get(downPosition) && openedRight.get(downPosition) && swapRight) {
                swap = false;
            } else if (opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight) {
                swap = false;
            } else {
                swap = true;
            }
        } else if (Math.abs(deltaX) > viewWidth / 2) {
            swap = true;
            swapRight = deltaX > 0;
        }

        generateAnimate(frontView, swap, swapRight, downPosition);
        if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
            swapChoiceState(downPosition);
        }

        velocityTracker.recycle();
        velocityTracker = null;
        downX = 0;
        // change clickable front view
        //                if (swap) {
        //                    frontView.setClickable(opened.get(downPosition));
        //                    frontView.setLongClickable(opened.get(downPosition));
        //                }
        swiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (velocityTracker == null || paused || downPosition == ListView.INVALID_POSITION) {
            break;
        }

        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        float velocityY = Math.abs(velocityTracker.getYVelocity());

        float deltaX = motionEvent.getRawX() - downX;
        float deltaMode = Math.abs(deltaX);

        int swipeMode = this.swipeMode;
        int changeSwipeMode = swipeListView.changeSwipeMode(downPosition);
        if (changeSwipeMode >= 0) {
            swipeMode = changeSwipeMode;
        }

        if (swipeMode == SwipeListView.SWIPE_MODE_NONE) {
            deltaMode = 0;
        } else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH) {
            if (opened.get(downPosition)) {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0) {
                    deltaMode = 0;
                }
            } else {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0) {
                    deltaMode = 0;
                }
            }
        }
        if (deltaMode > slop && swipeCurrentAction == SwipeListView.SWIPE_ACTION_NONE
                && velocityY < velocityX) {
            swiping = true;
            swipingRight = (deltaX > 0);
            if (SwipeListView.DEBUG) {
                Log.d(SwipeListView.TAG, "deltaX: " + deltaX + " - swipingRight: " + swipingRight);
            }
            if (opened.get(downPosition)) {
                swipeListView.onStartClose(downPosition, swipingRight);
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
            } else {
                if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
                }
                swipeListView.onStartOpen(downPosition, swipeCurrentAction, swipingRight);
            }
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
            if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
                backView.setVisibility(View.GONE);
            }
        }

        if (swiping && downPosition != ListView.INVALID_POSITION) {
            if (opened.get(downPosition)) {
                deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset : -viewWidth + leftOffset;
            }
            move(deltaX);
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:com.android.hcframe.DraggableGridViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our
        // descendants.
        return false;
    }/*  www . j  a  v a 2s. com*/

    if (mPageCount <= 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }

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

    final int action = ev.getAction();
    boolean needsInvalidate = false;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        mScroller.abortAnimation();
        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        HcLog.D("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                + " mIsUnableToDrag=" + mIsUnableToDrag);

        if (!mIsBeingDragged && mScrollState == SCROLL_STATE_IDLE) {
            mLastPosition = getPositionByXY((int) mLastMotionX, (int) mLastMotionY);
        } else {
            mLastPosition = -1;
        }
        if (mLastPosition >= 0) {
            mLastDownTime = System.currentTimeMillis();
        } else {
            mLastDownTime = Long.MAX_VALUE;
        }
        HcLog.D("Down at mLastPosition=" + mLastPosition);
        mLastDragged = -1;
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);

        if (mLastDragged >= 0) {
            // change draw location of dragged visual
            final View v = getChildAt(mLastDragged);
            final int l = getScrollX() + (int) x - v.getWidth() / 2;
            final int t = getScrollY() + (int) y - v.getHeight() / 2;
            v.layout(l, t, l + v.getWidth(), t + v.getHeight());

            // check for new target hover
            if (mScrollState == SCROLL_STATE_IDLE) {
                final int target = getTargetByXY((int) x, (int) y);
                if (target != -1 && mLastTarget != target) {
                    animateGap(target);
                    mLastTarget = target;
                    HcLog.D("Moved to mLastTarget=" + mLastTarget);
                }
                // edge holding
                final int edge = getEdgeByXY((int) x, (int) y);
                if (mLastEdge == -1) {
                    if (edge != mLastEdge) {
                        mLastEdge = edge;
                        mLastEdgeTime = System.currentTimeMillis();
                    }
                } else {
                    if (edge != mLastEdge) {
                        mLastEdge = -1;
                    } else {
                        if ((System.currentTimeMillis() - mLastEdgeTime) >= EDGE_HOLD_DURATION) {
                            performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                            triggerSwipe(edge);
                            mLastEdge = -1;
                        }
                    }
                }
            }
        } else if (!mIsBeingDragged) {
            final float xDiff = Math.abs(x - mLastMotionX);
            final float yDiff = Math.abs(y - mLastMotionY);
            HcLog.D("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

            if (xDiff > mTouchSlop && xDiff > yDiff) {
                HcLog.D("Starting drag!");
                mIsBeingDragged = true;
                requestParentDisallowInterceptTouchEvent(true);
                mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop
                        : mInitialMotionX - mTouchSlop;
                mLastMotionY = y;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        // Not else! Note that mIsBeingDragged can be set above.
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            needsInvalidate |= performDrag(x);
        } else if (mLastPosition >= 0) {
            final int currentPosition = getPositionByXY((int) x, (int) y);
            HcLog.D("Moved to currentPosition=" + currentPosition);
            if (currentPosition == mLastPosition) {
                if ((System.currentTimeMillis() - mLastDownTime) >= LONG_CLICK_DURATION) {
                    if (onItemLongClick(currentPosition)) {
                        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                        mLastDragged = mLastPosition;
                        requestParentDisallowInterceptTouchEvent(true);
                        mLastTarget = -1;
                        animateDragged();
                        mLastPosition = -1;
                    }
                    mLastDownTime = Long.MAX_VALUE;
                }
            } else {
                mLastPosition = -1;
            }
        }
        break;
    }
    case MotionEvent.ACTION_UP: {
        HcLog.D("Touch up!!!");
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);

        if (mLastDragged >= 0) {
            rearrange();
        } else if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            final int width = getWidth();
            final int scrollX = getScrollX();
            final int currentPage = scrollX / width;
            final int offsetPixels = scrollX - currentPage * width;
            final float pageOffset = (float) offsetPixels / (float) width;
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
        } else if (mLastPosition >= 0) {
            final int currentPosition = getPositionByXY((int) x, (int) y);
            HcLog.D("Touch up!!! currentPosition=" + currentPosition);
            if (currentPosition == mLastPosition) {
                onItemClick(currentPosition);
            }
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL:
        HcLog.D("Touch cancel!!!");
        if (mLastDragged >= 0) {
            rearrange();
        } else if (mIsBeingDragged) {
            scrollToItem(mCurItem, true, 0, false);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
    return true;
}

From source file:com.inmobi.ultrapush.AnalyzeActivity.java

private void scaleEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_MOVE) {
        xShift0 = INIT;//from  w  w w  . jav a 2  s  .  c o  m
        yShift0 = INIT;
        isPinching = false;
        //      Log.i(TAG, "scaleEvent(): Skip event " + event.getAction());
        return;
    }
    //    Log.i(TAG, "scaleEvent(): switch " + event.getAction());
    switch (event.getPointerCount()) {
    case 2:
        if (isPinching) {
            graphView.setShiftScale(event.getX(0), event.getY(0), event.getX(1), event.getY(1));
        } else {
            graphView.setShiftScaleBegin(event.getX(0), event.getY(0), event.getX(1), event.getY(1));
        }
        isPinching = true;
        break;
    case 1:
        float x = event.getX(0);
        float y = event.getY(0);
        graphView.getLocationInWindow(windowLocation);
        //        Log.i(TAG, "scaleEvent(): xy=" + x + " " + y + "  wc = " + wc[0] + " " + wc[1]);
        if (isPinching || xShift0 == INIT) {
            xShift0 = graphView.getXShift();
            x0 = x;
            yShift0 = graphView.getYShift();
            y0 = y;
        } else {
            // when close to the axis, scroll that axis only
            if (x0 < windowLocation[0] + 50) {
                graphView.setYShift(yShift0 + (y0 - y) / graphView.getCanvasHeight() / graphView.getYZoom());
            } else if (y0 < windowLocation[1] + 50) {
                graphView.setXShift(xShift0 + (x0 - x) / graphView.getCanvasWidth() / graphView.getXZoom());
            } else {
                graphView.setXShift(xShift0 + (x0 - x) / graphView.getCanvasWidth() / graphView.getXZoom());
                graphView.setYShift(yShift0 + (y0 - y) / graphView.getCanvasHeight() / graphView.getYZoom());
            }
        }
        isPinching = false;
        break;
    default:
        Log.v(TAG, "Invalid touch count");
        break;
    }
}

From source file:co.codecrunch.musicplayerlite.slidinguppanelhelper.SlidingUpPanelLayout.java

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

    if (!isEnabled() || !isTouchEnabled() || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();/*from  w  w  w.  j a  v a  2s.  c o m*/
        return super.onInterceptTouchEvent(ev);
    }

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

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mIsUnableToDrag = false;
        mInitialMotionX = x;
        mInitialMotionY = y;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final float adx = Math.abs(x - mInitialMotionX);
        final float ady = Math.abs(y - mInitialMotionY);
        final int dragSlop = mDragHelper.getTouchSlop();

        // Handle any horizontal scrolling on the drag view.
        if (mIsUsingDragViewTouchEvents && adx > dragSlop && ady < dragSlop) {
            return super.onInterceptTouchEvent(ev);
        }

        if ((ady > dragSlop && adx > ady) || !isDragViewUnder((int) mInitialMotionX, (int) mInitialMotionY)) {
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
        break;
    }
    }

    return mDragHelper.shouldInterceptTouchEvent(ev);
}

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

boolean onTouchEvent(MotionEvent me) {
    if (mState == STATE_NONE) {
        return false;
    }/*from  w  ww  .  j  ava  2 s . com*/

    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.android.launcher3.Folder.java

public void onDragOver(DragObject d) {
    final DragView dragView = d.dragView;
    final int scrollOffset = mScrollView.getScrollY();
    final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
    r[0] -= getPaddingLeft();//from   w  ww  . jav a  2 s .c o m
    r[1] -= getPaddingTop();

    final long downTime = SystemClock.uptimeMillis();
    final MotionEvent translatedEv = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y,
            0);

    if (!mAutoScrollHelper.isEnabled()) {
        mAutoScrollHelper.setEnabled(true);
    }

    final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
    translatedEv.recycle();

    if (handled) {
        mReorderAlarm.cancelAlarm();
    } else {
        mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
        if (isLayoutRtl()) {
            mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
        }
        if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
            mReorderAlarm.cancelAlarm();
            mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
            mReorderAlarm.setAlarm(REORDER_DELAY);
            mPreviousTargetCell[0] = mTargetCell[0];
            mPreviousTargetCell[1] = mTargetCell[1];
        }
    }
}

From source file:cn.org.eshow.framwork.view.slidingmenu.CustomViewAbove.java

@Override
public boolean onTouchEvent(MotionEvent ev) {

    if (!mEnabled)
        return false;

    if (!mIsBeingDragged && !thisTouchAllowed(ev))
        return false;

    //      if (!mIsBeingDragged && !mQuickReturn)
    //         return false;

    final int action = ev.getAction();

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }//from  w ww  .  java  2 s . c  om
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        mLastMotionX = mInitialMotionX = ev.getX();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            determineDrag(ev);
            if (mIsUnableToDrag)
                return false;
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
            if (mActivePointerId == INVALID_POINTER)
                break;
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            final float leftBound = getLeftBound();
            final float rightBound = getRightBound();
            if (scrollX < leftBound) {
                scrollX = leftBound;
            } else if (scrollX > rightBound) {
                scrollX = rightBound;
            }
            // Don't lose the rounded component
            mLastMotionX += scrollX - (int) scrollX;
            scrollTo((int) scrollX, getScrollY());
            pageScrolled((int) scrollX);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            final int scrollX = getScrollX();
            //            final int widthWithMargin = getWidth();
            //            final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
            // TODO test this. should get better flinging behavior
            final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
            final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
            if (mActivePointerId != INVALID_POINTER) {
                final float x = MotionEventCompat.getX(ev, activePointerIndex);
                final int totalDelta = (int) (x - mInitialMotionX);
                int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta);
                setCurrentItemInternal(nextPage, true, true, initialVelocity);
            } else {
                setCurrentItemInternal(mCurItem, true, true, initialVelocity);
            }
            mActivePointerId = INVALID_POINTER;
            endDrag();
        } else if (mQuickReturn
                && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
            // close the menu
            setCurrentItem(1);
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int indexx = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, indexx);
        mActivePointerId = MotionEventCompat.getPointerId(ev, indexx);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        int pointerIndex = getPointerIndex(ev, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER)
            break;
        mLastMotionX = MotionEventCompat.getX(ev, pointerIndex);
        break;
    }
    return true;
}

From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    final int action = ev.getAction();

    if (!isEnabled()) {
        return false; // We don't want the events. Let them fall through to the all
                      // apps view.
    }/*from ww  w. j  a  v a2  s. co m*/

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

    acquireVelocityTrackerAndAddMovement(ev);

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {

        /*
         * Locally do absolute value. mLastMotionX is set to the y value of the
         * down event.
         */
        final int pointerIndex = ev.findPointerIndex(mActivePointerId);

        if (pointerIndex < 0) {
            // invalid pointer
            return true;
        }

        final float x = ev.getX(pointerIndex);
        final float y = ev.getY(pointerIndex);
        final int xDiff = (int) Math.abs(x - mLastMotionX);
        final int yDiff = (int) Math.abs(y - mLastMotionY);

        final int touchSlop = mTouchSlop;
        boolean xMoved = xDiff > touchSlop;
        boolean yMoved = yDiff > touchSlop;
        mLastMotionX2 = x;

        if (xMoved || yMoved) {

            if (xMoved) {
                // Scroll if the user moved far enough along the X axis
                mTouchState = TOUCH_STATE_SCROLLING;
                mLastMotionX = x;
                mTouchX = getScrollX();
                mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
                enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
            }

        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        // Remember location of down touch
        mLastMotionX = x;
        mLastMotionX2 = x;
        mLastMotionY = y;
        mActivePointerId = ev.getPointerId(0);
        mAllowLongPress = true;

        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        // Release the drag
        clearChildrenCache();
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        mAllowLongPress = false;
        releaseVelocityTracker();
        break;

    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    /*
     * The only time we want to intercept motion events is if we are in the drag mode.
     */
    return mTouchState != TOUCH_STATE_REST;
}

From source file:com.bofsoft.sdk.widget.menu.slidingmenu.CustomViewAbove.java

@Override
public boolean onTouchEvent(MotionEvent ev) {

    if (!mEnabled)
        return false;

    if (!mIsBeingDragged && !thisTouchAllowed(ev))
        return false;

    // if (!mIsBeingDragged && !mQuickReturn)
    // return false;

    final int action = ev.getAction();

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }//from   w w w . j  a v  a  2s .  c  o  m
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished will be false if being
         * flinged.
         */
        completeScroll();

        // Remember where the motion event started
        int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        mLastMotionX = mInitialMotionX = ev.getX();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            determineDrag(ev);
            if (mIsUnableToDrag)
                return false;
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
            if (mActivePointerId == INVALID_POINTER)
                break;
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            final float leftBound = getLeftBound();
            final float rightBound = getRightBound();
            if (scrollX < leftBound) {
                scrollX = leftBound;
            } else if (scrollX > rightBound) {
                scrollX = rightBound;
            }
            // Don't lose the rounded component
            mLastMotionX += scrollX - (int) scrollX;
            scrollTo((int) scrollX, getScrollY());
            pageScrolled((int) scrollX);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            final int scrollX = getScrollX();
            // final int widthWithMargin = getWidth();
            // final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
            // TODO test this. should get better flinging behavior
            final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
            final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
            if (mActivePointerId != INVALID_POINTER) {
                final float x = MotionEventCompat.getX(ev, activePointerIndex);
                final int totalDelta = (int) (x - mInitialMotionX);
                int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta);
                setCurrentItemInternal(nextPage, true, true, initialVelocity);
            } else {
                setCurrentItemInternal(mCurItem, true, true, initialVelocity);
            }
            mActivePointerId = INVALID_POINTER;
            endDrag();
        } else if (mQuickReturn
                && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
            // close the menu
            setCurrentItem(1);
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int indexx = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, indexx);
        mActivePointerId = MotionEventCompat.getPointerId(ev, indexx);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        int pointerIndex = getPointerIndex(ev, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER)
            break;
        mLastMotionX = MotionEventCompat.getX(ev, pointerIndex);
        break;
    }
    return true;
}

From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getActionMasked();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_DOWN:
        if (DEBUG)
            Log.v(TAG, "*** DOWN ***");
        handleDown(event);// w ww  . j  av a  2  s  .c o  m
        handleMove(event);
        handled = true;
        break;

    case MotionEvent.ACTION_MOVE:
        if (DEBUG)
            Log.v(TAG, "*** MOVE ***");
        handleMove(event);
        handled = true;
        break;

    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
        if (DEBUG)
            Log.v(TAG, "*** UP ***");
        handleMove(event);
        handleUp(event);
        handled = true;
        break;

    case MotionEvent.ACTION_CANCEL:
        if (DEBUG)
            Log.v(TAG, "*** CANCEL ***");
        handleMove(event);
        handleCancel(event);
        handled = true;
        break;
    }
    invalidate();
    return handled ? true : super.onTouchEvent(event);
}