Code example for MotionEvent

Methods: findPointerIndexgetActiongetPointerIdgetXgetY

0
            return true; 
        } 
 
        @Override 
        public boolean onTouchEvent(MotionEvent ev) {
            switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                mLastTouchX = getActiveX(ev);
                mLastTouchY = getActiveY(ev);
                break; 
            } 
            case MotionEvent.ACTION_MOVE: {
                final float x = getActiveX(ev);
                final float y = getActiveY(ev);
 
                if (shouldDrag()) { 
                    mListener.onDrag(x - mLastTouchX, y - mLastTouchY);
                } 
 
                mLastTouchX = x;
                mLastTouchY = y;
                break; 
            } 
            } 
            return true; 
        } 
    } 
 
    private static class EclairDetector extends CupcakeDetector { 
        private static final int INVALID_POINTER_ID = -1;
        private int mActivePointerId = INVALID_POINTER_ID;
        private int mActivePointerIndex = 0;
 
        @Override 
        float getActiveX(MotionEvent ev) {
            return ev.getX(mActivePointerIndex);
        } 
 
        @Override 
        float getActiveY(MotionEvent ev) {
            return ev.getY(mActivePointerIndex);
        } 
 
        @Override 
        public boolean onTouchEvent(MotionEvent ev) {
            final int action = ev.getAction();
            switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mActivePointerId = ev.getPointerId(0);
                break; 
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                mActivePointerId = INVALID_POINTER_ID;
                break; 
            case MotionEvent.ACTION_POINTER_UP:
                final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                        >> MotionEvent.ACTION_POINTER_INDEX_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. 
                    final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                    mActivePointerId = ev.getPointerId(newPointerIndex);
                    mLastTouchX = ev.getX(newPointerIndex);
                    mLastTouchY = ev.getY(newPointerIndex);
                } 
                break; 
            } 
 
            mActivePointerIndex = ev.findPointerIndex(mActivePointerId);
            return super.onTouchEvent(ev);
        } 
    } 
 
    private static class FroyoDetector extends EclairDetector {