Android Gesture

2 minute read

A “touch gesture” occurs when a user places one or more fingers on the touch screen, and your application interprets that pattern of touches as a particular gesture. There are correspondingly two phases to gesture detection:

  1. Gather data about touch events.
  2. Interpret the data to see if it meets the criteria for any of the gestures your app supports.

MotionEvent

public class MainActivity extends Activity {
// ...
    @Override
    public boolean onTouchEvent(MotionEvent event){

        int action = MotionEventCompat.getActionMasked(event);

        switch(action) {
            case (MotionEvent.ACTION_DOWN) :
                Log.d(DEBUG_TAG,"Action was DOWN");
                return true;
            case (MotionEvent.ACTION_MOVE) :
                Log.d(DEBUG_TAG,"Action was MOVE");
                return true;
            case (MotionEvent.ACTION_UP) :
                Log.d(DEBUG_TAG,"Action was UP");
                return true;
            case (MotionEvent.ACTION_CANCEL) :
                Log.d(DEBUG_TAG,"Action was CANCEL");
                return true;
            case (MotionEvent.ACTION_OUTSIDE) :
                Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                        "of current screen element");
                return true;
            default :
                return super.onTouchEvent(event);
        }
    }
// ...
}

You can do your own processing on these events to determine if a gesture occurred. However, it’s hard to control various kinds of gestures with touch events. You have to gather touch events and interpret the MotionEvent to get the gestures. I would recommend to use GestureDetector.

If your app uses common gestures such as double tap, long press, fling, and so on, you can take advantage of the GestureDetector class. GestureDetector makes it easy for you to detect common gestures without processing the individual touch events yourself.

Link : Google developers - Detect common gestures

GestureDetector

MainActivity.java

    private ScaleGestureDetector mScaleGestureDetector;
    private GestureDetector mGestureDetector;

    /**
     * Init controls
     */
    private void initControls() {
        if (mScaleGestureDetector == null) {
            mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
        }

        if (mGestureDetector == null) {
            mGestureDetector = new GestureDetector(this, gestureListener);
        }

        if (mFrameLayout == null) {
            mFrameLayout = (FrameLayout)findViewById(R.id.am_fl_main);
            mFrameLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    mScaleGestureDetector.onTouchEvent(event);
                    mGestureDetector.onTouchEvent(event);
                    return true;
                }
            });
        }
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector scaleGestureDetector){
            // Do whatever you want
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            // Do whatever you want
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            // Do whatever you want
        }
    }

    private final GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            // Do whatever you want
            return false;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // Do whatever you want
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            // Do whatever you want
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    };

Tags:

Categories:

Updated:

Leave a comment