Code example for MotionEvent

Methods: getActiongetDownTimegetEventTimegetPointerCountgetXgetY

0
 
    private long lastClickTime;
 
    @Override 
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
 
            // NORMAL CASE : FIRST POINTER DOWN 
            case MotionEvent.ACTION_DOWN:
                // Start dragging 
                startDrag(event);
                state = State.DRAG;
                lastDownX = event.getX();
                lastDownY = event.getY();
                break; 
 
            // NORMAL CASE : SECOND POINTER DOWN 
            case MotionEvent.ACTION_POINTER_2_DOWN:
                startDrag(event);
                startZoom(event);
                state = State.ZOOM;
                break; 
 
            // NORMAL CASE : SECOND POINTER UP 
            case MotionEvent.ACTION_POINTER_2_UP:
                // End zooming, goes back to dragging 
                state = State.DRAG;
                break; 
 
            // NORMAL CASE : FIRST POINTER UP 
            case MotionEvent.ACTION_UP:
                // End everything 
                state = State.NONE;
                endDrag(); 
 
                // Treat clicks 
                if (isClick(event, lastDownX, lastDownY, event.getX(), event.getY())) {
                    long time = System.currentTimeMillis();
                    if (time - lastClickTime < MAX_DOUBLE_CLICK_TIME) {
                        if (onDoubleTapListener != null) {
                            onDoubleTapListener.onDoubleTap(event.getX(), event.getY());
                        } 
                        lastClickTime = 0;
                    } else { 
                        lastClickTime = System.currentTimeMillis();
                    } 
                } 
                break; 
 
            // TRICKY CASE : FIRST POINTER UP WHEN SECOND STILL DOWN 
            case MotionEvent.ACTION_POINTER_1_UP:
 
                // FIXME Probably not the good value 
                dragLastX = pointer2LastX;
                dragLastY = pointer2LastY;
                startDrag(event);
                state = State.DRAG;
                break; 
 
            // TRICKY CASE : FIRST POINTER UP THEN DOWN WHILE SECOND POINTER STILL UP 
            case MotionEvent.ACTION_POINTER_1_DOWN:
                pointer2LastX = event.getX(POINTER1);
                pointer2LastY = event.getY(POINTER1);
 
                startDrag(event);
                startZoom(event);
                state = State.ZOOM;
                break; 
 
            // NORMAL CASE : MOVE 
            case MotionEvent.ACTION_MOVE:
 
                switch (state) {
                    case ZOOM:
                        pointer2LastX = event.getX(POINTER2);
                        pointer2LastY = event.getY(POINTER2);
                        zoom(event);
 
                    case DRAG:
                        drag(event);
                        break; 
                    default: 
                        break; 
                } 
                break; 
        } 
 
        return true; 
    } 
 
    private void endDrag() { 
        onDragListener.endDrag(dragLastX, dragLastY);
    } 
 
    private void startZoom(MotionEvent event) {
        zoomLastDistance = distance(event);
    } 
 
    private void zoom(MotionEvent event) {
        float zoomCurrentDistance = distance(event);
 
        if (onPinchListener != null) {
            onPinchListener.onPinch(zoomCurrentDistance / zoomLastDistance, //
                    new PointF(event.getX(POINTER1), event.getY(POINTER1)));
        } 
 
        zoomLastDistance = zoomCurrentDistance;
    } 
 
    private void startDrag(MotionEvent event) {
        dragLastX = event.getX(POINTER1);
        dragLastY = event.getY(POINTER1);
        onDragListener.startDrag(dragLastX, dragLastY);
    } 
 
    private void drag(MotionEvent event) {
        float dragCurrentX = event.getX(POINTER1);
        float dragCurrentY = event.getY(POINTER1);
 
        if (onDragListener != null) {
            onDragListener.onDrag(dragCurrentX - dragLastX,
                    dragCurrentY - dragLastY);
        } 
 
        dragLastX = dragCurrentX;
        dragLastY = dragCurrentY;
    } 
 
    /** Calculates the distance between the 2 current pointers */ 
    private float distance(MotionEvent event) {
        if (event.getPointerCount() < 2) {
            return 0; 
        } 
        return PointF.length(event.getX(POINTER1) - event.getX(POINTER2), //
                event.getY(POINTER1) - event.getY(POINTER2));
    } 
 
    /** 
     * Test if a MotionEvent with the given start and end offsets 
     * can be considered as a "click". 
     * @param upEvent The final finger-up event. 
     * @param xDown   The x-offset of the down event. 
     * @param yDown   The y-offset of the down event. 
     * @param xUp     The x-offset of the up event. 
     * @param yUp     The y-offset of the up event. 
     * @return true if it's a click, false otherwise 
     */ 
    private boolean isClick(MotionEvent upEvent, float xDown, float yDown, float xUp, float yUp) {
        if (upEvent == null) return false;
        long time = upEvent.getEventTime() - upEvent.getDownTime();
        float distance = PointF.length( //
                xDown - xUp, //
                yDown - yUp);
        return time < MAX_CLICK_TIME && distance < MAX_CLICK_DISTANCE;
    }