articles

Home / DeveloperSection / Articles / Gesture in Android

Gesture in Android

Manoj Pandey3526 11-Mar-2015

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:

·        Gathering data about touch events.

·         Interpreting the data to see if it meets the criteria for any of the gestures your app supports.

When a user interacts with the display of an Android device, the onTouchEvent() method of the currently active application is called by the system and passed MotionEvent objects containing data about the user’s contact with the screen. This data can be interpreted to identify if the motion on the screen matches a common gesture such as a tap or a swipe. This can be achieved with very little programming effort by making use of the Android GestureDetectorCompat class. This class is designed specifically to receive motion event information from the application and to trigger method calls based on the type of common gesture, if any, detected.

The basic steps in detecting common gestures are as follows:

1. Declaration of a class which implements the GestureDetector.OnGestureListener interface including the required onFling(), onDown(), onScroll(), onShowPress, onSingleTapUp() and onLongPress() callback methods. Note that this can be either an entirely new class, or the enclosing activity class. In the event that double tap gesture detection is required, the class must also implement the GestureDetector.OnDoubleTapListener interface and include the corresponding onDoubleTap() method.

2. Creation of an instance of the Android GestureDetectorCompat class, passing through an instance of the class created in step 1 as an argument.

3. An optional call to the setOnDoubleTapListener() method of the GestureDetectorCompat instance to enable double tap detection if required.

4. Implementation of the onTouchEvent() callback method on the enclosing activity which, in turn, must call the onTouchEvent() method of the GestureDetectorCompat instance, passing through the current motion event object as an argument to the method.

Here I am creating sample of Image Zooming via use gesture

1.      Create an android project

2.      Add image in layout 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_gravity="center"

    android:gravity="center"

    android:orientation="horizontal"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <ImageView

        android:padding="80sp"

        android:id="@+id/imageView1"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:scaleType="matrix"

        android:src="@android:drawable/sym_def_app_icon" />

 

</LinearLayout>

 

3.      Add following code in MainActivity.class

package com.example.androidgesture;

import android.app.Activity;

import android.graphics.Matrix;

import android.os.Bundle;

import android.view.Menu;

import android.view.MotionEvent;

import android.view.ScaleGestureDetector;

import android.widget.ImageView;

public class MainActivity extends Activity {

                private ImageView img;

                private Matrix matrix = new Matrix();

                private float scale = 1f;

                private ScaleGestureDetector SGD;

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_main);

                                img = (ImageView) findViewById(R.id.imageView1);

                                // A convenience class to extend when you only want to listen for a

                                // subset of scaling-related events.

                                SGD = new ScaleGestureDetector(this, new ScaleListener());

                }

 

                @Override

                public boolean onTouchEvent(MotionEvent ev) {

                                SGD.onTouchEvent(ev);

                                return true;

                }

                private class ScaleListener extends

                                                ScaleGestureDetector.SimpleOnScaleGestureListener {

                                @Override

                                public boolean onScale(ScaleGestureDetector detector) {

                                                // Return the scaling factor from the previous scale event to the

                                                // current event.

                                                scale *= detector.getScaleFactor();

                                                scale = Math.max(0.1f, Math.min(scale, 5.0f));

                                                matrix.setScale(scale, scale);

                                                img.setImageMatrix(matrix);

                                                return true;

                                }

                }

}

 

Now run your application 

Gesture in Android

Zoom image

 Gesture in Android

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By