Android Gesture

Android Gesture in Android Studio

Project Outline: Android Gesture

The primary objective of this illustration is to share an idea about Android Gestures. This tutorial was carried out in Android Studio.

We will create a basic pinch-zoom application here.

Touch gesture: It is the point at which a client puts at least one finger on the touch screen and the application figures out that as a sign or GESTURE. It has two main phases:

  1. Detect and Track data about touch events.
  2. Translation of that data as a specific gesture by the application.

Android presents various common touch events like double tap, pinch, scrolls, flinch, long presses, and more. All these are known as gestures.

Android provides GestureDetector class to detect some of the well-known motion events like Single Tap up, Long Press, and more. It puts together data and informs us whether it is a gesture or not.

To be able to utilize it, you need to create an object of GestureDetector and proceed to extend another class with GestureDetector.SimpleOnGestureListener to act as a listener and override some methods.

Syntax: Android Gesture

				
					GestureDetector myG;
myG = new GestureDetector(this,new Gesture());
 
class Gesture extends GestureDetector.SimpleOnGestureListener{
public boolean onSingleTapUp(MotionEvent ev) {
}
public void onLongPress(MotionEvent ev) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
}
}
}
				
			

In other to handle a few gestures like pinch zoom you need to use ScaleGestureDetector class.

We will make a basic application for pinch zoom.

Step 1: activity_main.xml

Take imageview and set scaletype as matrix.

				
					<span style="font-family: Lato, sans-serif;"><span style="font-size: 16px; line-height: 24px; white-space: normal;"> </span></span><RelativeLayout 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:orientation="vertical" >
 
<ImageView
android:id="@+id/ivDemo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
				
			

Step 2: Create a ScaleGestureDetector class object.

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

ScaleListner class was passed. It has an extension of  ScaleGestureDetector.SimpleOnScaleGestureListener

				
					private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
return false;
}
}
				
			

To learn more about ScaleGestureDetector please use the following link:

To learn more about ScaleGestureDetector.SimpleOnScaleGestureListener please use the following link:

This class will receive a Pinch zoom event.

Step 3: Register ScaleGestureDetector class to TouchEvent method.

To proceed, we are Overriding the TouchEvent method so that TouchEvent of pinch can be detected by ScaleGestureDetector.

				
					@Override
public boolean onTouchEvent(MotionEvent ev) {
scaleGestureDetector.onTouchEvent(ev);
return true;
}
				
			

Step 4: MainActivity.java file

Paste the below code in MainActivity.java

				
					package com.tag.blog.androidgesturesdemo;
 
import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
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 scaleGestureDetector;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.ivDemo);
scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
}
 
@Override
public boolean onTouchEvent(MotionEvent ev) {
scaleGestureDetector.onTouchEvent(ev);
return true;
}
 
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scale *= detector.getScaleFactor();
scale = Math.max(0.1f, Math.min(scale, 5.0f));
matrix.setScale(scale, scale);
img.setImageMatrix(matrix);
return true;
}
}
}
				
			

Step 5: Run Application - Android Gesture

Launch the application on a real device. Go ahead to use pinch zoom and see that the image zooms and shrinks as per the gesture.

Don’t forget to share this post!

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Article