Implement Scalegesturedetector On Private Imageview

Posted on

Last representative present Implementing ScaleGestureDetector on whole activeness screen, to railroad train scale of a ImageView. Now nosotros alter to apply ScaleGestureDetector to private ImageView, to scale the selected ImageView only.


Remark:
It tin hold upward noticed inward the video, pinch-to-zoom cannot hold upward break afterward sometimes – I don’t know why, but tap-and-slide yet business office every moment expected.

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.view.View; import android.widget.ImageView;  populace shape MainActivity extends AppCompatActivity {      ImageView myImage1, myImage2;      private ScaleGestureDetector scaleGestureDetector1, scaleGestureDetector2;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         myImage1 = (ImageView)findViewById(R.id.myimage1);         myImage2 = (ImageView)findViewById(R.id.myimage2);          scaleGestureDetector1 = novel ScaleGestureDetector(                 this, novel MySimpleOnScaleGestureListener(myImage1));         scaleGestureDetector2 = novel ScaleGestureDetector(                 this, novel MySimpleOnScaleGestureListener(myImage2));          myImage1.setOnTouchListener(new View.OnTouchListener() {             @Override             populace boolean onTouch(View v, MotionEvent event) {                 scaleGestureDetector1.onTouchEvent(event);                 supply true;             }         });          myImage2.setOnTouchListener(new View.OnTouchListener() {             @Override             populace boolean onTouch(View v, MotionEvent event) {                 scaleGestureDetector2.onTouchEvent(event);                 supply true;             }         });     }       private shape MySimpleOnScaleGestureListener             extends ScaleGestureDetector.SimpleOnScaleGestureListener{          ImageView viewMyImage;          float factor;          populace MySimpleOnScaleGestureListener(ImageView iv) {             super();             viewMyImage = iv;             element = 1.0f;         }          @Override         populace boolean onScale(ScaleGestureDetector detector) {              float scaleFactor = detector.getScaleFactor() - 1;             element += scaleFactor;             viewMyImage.setScaleX(factor);             viewMyImage.setScaleY(factor);             supply 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" />      <LinearLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1"         android:gravity="center"         android:background="@android:color/black">         <ImageView             android:id="@+id/myimage1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:src="@mipmap/ic_launcher"             android:background="#A00000"/>     </LinearLayout>     <LinearLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1"         android:gravity="center"         android:background="@android:color/darker_gray">         <ImageView             android:id="@+id/myimage2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:src="@mipmap/ic_launcher"             android:background="#0000A0"/>     </LinearLayout>  </LinearLayout>