Implement Pinch-To-Zoom Amongst Scalegesturedetector

Posted on

Last post demo how to implement ScaleGestureDetector, this post demo how to implement pinch-to-zoom on ImageView amongst ScaleGestureDetector.


MainActivity.java

package com.blogspot.android_er.androidscalegesturedetector;  import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.widget.ImageView; import android.widget.TextView;  populace shape MainActivity extends AppCompatActivity {      TextView textMsg;     ImageView myImage;      private ScaleGestureDetector scaleGestureDetector;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         textMsg = (TextView)findViewById(R.id.msg);         myImage = (ImageView)findViewById(R.id.myimage);          scaleGestureDetector = novel ScaleGestureDetector(                 this, novel MySimpleOnScaleGestureListener(textMsg, myImage));     }      @Override     populace boolean onTouchEvent(MotionEvent event) {         scaleGestureDetector.onTouchEvent(event);         provide true;         //return super.onTouchEvent(event);     }      private shape MySimpleOnScaleGestureListener             extends ScaleGestureDetector.SimpleOnScaleGestureListener{          TextView viewMessage;         ImageView viewMyImage;          float factor;          populace MySimpleOnScaleGestureListener(TextView v, ImageView iv) {             super();             viewMessage = v;             viewMyImage = iv;         }          @Override         populace boolean onScaleBegin(ScaleGestureDetector detector) {             part = 1.0f;             provide true;             //return super.onScaleBegin(detector);         }          @Override         populace boolean onScale(ScaleGestureDetector detector) {              float scaleFactor = detector.getScaleFactor() - 1;             part += scaleFactor;             viewMessage.setText(String.valueOf(scaleFactor)                     + "\n" + String.valueOf(factor));             viewMyImage.setScaleX(factor);             viewMyImage.setScaleY(factor);             provide true;             //return super.onScale(detector);         }     } }  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <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:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidscalegesturedetector.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />     <TextView         android:id="@+id/msg"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="bold"         android:textSize="24sp"/>     <ImageView         android:id="@+id/myimage"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@mipmap/ic_launcher"/> </LinearLayout>  

Next:
– Implement ScaleGestureDetector on private ImageView