We will get control in the application when the user pinches to zoom using the ScaleGestureDetector.OnScaleGestureListener interface. We should implement the onScale method in this GestureListener interface. A Matrix is constructed and scaled based on the user’s pinch gesture scale factor and then the ImageView set based on this scaled Matrix.

PinchZoomActivity.java
package com.javapapers.android.pinchzoom.app; 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 PinchZoomActivity extends Activity { private ImageView imageView; private ScaleGestureDetector scaleGestureDetector; private Matrix matrix = new Matrix(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView)findViewById(R.id.imageView); 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) { float scaleFactor = detector.getScaleFactor(); scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f)); matrix.setScale(scaleFactor, scaleFactor); imageView.setImageMatrix(matrix); return true; } } }
activity_main.xml
<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: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" > <TextView android:id="@+id/pinchTitleText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pinch_title" /> <ImageView android:id="@+id/imageView" android:layout_width="300dp" android:layout_height="300dp" android:layout_below="@+id/pinchTitleText" android:scaleType="matrix" android:src="@drawable/zoom" android:maxHeight="100dp" android:maxWidth="50dp" /> </RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.javapapers.android.pinchzoom.app" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.javapapers.android.pinchzoom.app.PinchZoomActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
