Code example for MotionEvent

Methods: findPointerIndexgetActiongetPointerIdgetX, MotionEvent.getY

0
 
            /* 
             * Shortcut the most recurring case: the user is in the dragging state and he is moving his finger. We want to 
             * intercept this motion. 
             */ 
            final int action = ev.getAction();
            if ((action == MotionEvent.ACTION_MOVE) && (touchState != TOUCH_STATE_REST)) {
                return true; 
            } 
 
            if (mVelocityTracker == null) {
                mVelocityTracker = VelocityTracker.obtain();
            } 
            mVelocityTracker.addMovement(ev);
 
    //      switch (action & MotionEvent.ACTION_MASK) { 
            switch (action) {
            case MotionEvent.ACTION_MOVE:
 
    //          Log.d("workspace","Intercepted a move event"); 
                /* 
                 * Locally do absolute value. mLastMotionX is set to the y value of the down event. 
                 */ 
                handleInterceptMove(ev);
                break; 
 
            case MotionEvent.ACTION_DOWN:
                // Remember location of down touch 
                final float x1 = ev.getX();
                final float y1 = ev.getY();
                lastMotionX = x1;
                lastMotionY = y1;
                allowLongPress = true;
                mActivePointerId = ev.getPointerId(0);
 
                /* 
                 * If being flinged and user touches the screen, initiate drag; otherwise don't. mScroller.isFinished should be 
                 * false when being flinged. 
                 */ 
                touchState = scroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
                break; 
 
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                mActivePointerId = INVALID_POINTER;
                allowLongPress = false;
 
                if (mVelocityTracker != null) {
                    mVelocityTracker.recycle();
                    mVelocityTracker = null;
                } 
                touchState = TOUCH_STATE_REST;
                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 touchState != TOUCH_STATE_REST;
        } 
 
        private void handleInterceptMove(MotionEvent ev) {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);
            final int xDiff = (int) Math.abs(x - lastMotionX);
            final int yDiff = (int) Math.abs(y - lastMotionY);
            boolean xMoved = xDiff > touchSlop;
            boolean yMoved = yDiff > touchSlop;
 
            if (xMoved || yMoved) {
                //Log.d("workspace","Detected move.  Checking to scroll."); 
                if (xMoved && !yMoved) {
                    //Log.d("workspace","Detected X move.  Scrolling."); 
                    // Scroll if the user moved far enough along the X axis 
                    touchState = TOUCH_STATE_SCROLLING;
                    lastMotionX = x;
                } 
                // Either way, cancel any pending longpress 
                if (allowLongPress) {
                    allowLongPress = false;
                    // Try canceling the long press. It could also have been scheduled 
                    // by a distant descendant, so use the mAllowLongPress flag to block 
                    // everything 
                    final View currentView = getChildAt(currentScreen);
                    currentView.cancelLongPress();
                } 
            } 
        } 
 
        private void onSecondaryPointerUp(MotionEvent ev) {
            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >>
                    MotionEvent.ACTION_POINTER_ID_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new 
                // active pointer and adjust accordingly. 
                // TODO: Make this decision more intelligent. 
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                lastMotionX = ev.getX(newPointerIndex);
                lastMotionY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
                if (mVelocityTracker != null) {
                    mVelocityTracker.clear();
                } 
            } 
        }