Andriod pinch to zoom image

When you create a gallery application, you will allow the user to zoom the image in and out through pinch (moving a finger and thumb a part or bringing them together). In this Android tip, I am going show you how to zoom an ImageView when the user makes a pinch on the screen.



To detect the pinch action, you need to create an instance of ScaleGestureDetector class. Its constructor accepts two parameters: the current context and an instance of a class that implements OnScaleGestureListener interface. You must override the View or Activity's onTouchEvent() method, and pass along all observed events to the ScaleGestureDetector instance in order the ScaleGestureDetector to receive events. The OnScaleGestureListener notifies the application when a pinch event happens. You need to override its onScale() method to get the scale or zoom factor.
Now to have an example application on pinch to zoom image, you create a new Android project. Then in activity_main.xml file, you add an ImageView.

<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"
   tools:context="com.example.inputdialog.MainActivity" >  

<ImageView
android:id="@+id/imgview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher"


/>

</RelativeLayout>



In the MainActivity class, you write code to detect the pinch gesture and override the onScale() method to get the scale factor and zoom the ImageView.




public class MainActivity extends Activity{
ImageView imgView;
ScaleGestureDetector scaleGDetector;
float scale=1f;
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     imgView=(ImageView)findViewById(R.id.imgview);
     scaleGDetector=new ScaleGestureDetector(this, new ScaleListener());
    
   }
   @Override
   public boolean onTouchEvent(MotionEvent ev) {
   scaleGDetector.onTouchEvent(ev);
     return true;
   }
  
   private class ScaleListener implements ScaleGestureDetector.OnScaleGestureListener{
   public boolean onScaleBegin(ScaleGestureDetector sgd){
  
   return true;

   }
   public void onScaleEnd(ScaleGestureDetector sgd){
  
   }
   public boolean onScale(ScaleGestureDetector sgd){
   // Multiply scale factor
   scale*= sgd.getScaleFactor();
   // Scale or zoom the imageview
   imgView.setScaleX(scale);
   imgView.setScaleY(scale);
   Log.i("Main",String.valueOf(scale));
   return true;
   }
   }
}

Andriod-pinch-to-zoom-image

Posted by: Dara | post date: 01-17-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.