title: Android Pinch and Zoom Image in Activity
body: <p>I have a ListView with set of images. On clicking any of them, the clicked image will appear on a new activity and at the center of the activity in ImageView, also it is Zoomable. The Problem is when i zoom the image it gains Zoom but also moves like drag and drop. I should eliminate that movement of the image and should only be zoomable from the center of the activity.</p>
  
  <p><strong>ZoomInZoomOutActivity.java</strong></p>
  
  <pre><code>public class ZoomInZoomOutActivity extends Activity implements OnTouchListener 
  {
      private static final String TAG = "Touch";
      @SuppressWarnings("unused")
      private static final float MIN_ZOOM = 1f,MAX_ZOOM = 1f;
  
      Matrix matrix = new Matrix();
      Matrix savedMatrix = new Matrix();
      static final int NONE = 0;
      static final int DRAG = 1;
      static final int ZOOM = 2;
      int mode = NONE;
      PointF start = new PointF();
      PointF mid = new PointF();
      float oldDist = 1f;
      @Override
      public void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.imagedisp);
  
          Bundle extras = getIntent().getExtras();
  
          long id =extras.getLong("ID");
          String s=String.valueOf(id);
          ImageView view;
          view = (ImageView) findViewById(R.id.imageView1);
          view.setOnTouchListener(this);
  
          if (s.equals("0")) 
          {
              view.setImageResource(R.drawable.img1);
          }
          else if (s.equals("1"))
          {
              view.setImageResource(R.drawable.img2);
          }
          else if (s.equals("2"))
          {
              view.setImageResource(R.drawable.img3);
          } 
          else 
          {
              view.setImageResource(R.drawable.img4);
          }
  
      }
  
  
      public boolean onTouch(View v, MotionEvent event) 
      {
          ImageView view = (ImageView) v;
          view.setScaleType(ImageView.ScaleType.MATRIX);
          float scale;
  
          switch (event.getAction() &amp; MotionEvent.ACTION_MASK) 
          {
              /*case MotionEvent.ACTION_DOWN:   
                                                  savedMatrix.set(matrix);
                                                  start.set(event.getX(), event.getY());
                                                  Log.d(TAG, "mode=DRAG"); 
                                                  mode = DRAG;
                                                  break; */
              case MotionEvent.ACTION_UP:
  
              case MotionEvent.ACTION_POINTER_UP:
  
                                                  mode = NONE;
                                                  Log.d(TAG, "mode=NONE");
                                                  break;
  
              case MotionEvent.ACTION_POINTER_DOWN: 
  
                                                  oldDist = spacing(event);
                                                  Log.d(TAG, "oldDist=" + oldDist);
                                                  if (oldDist &gt; 5f) {
                                                      savedMatrix.set(matrix);
                                                      midPoint(mid, event);
                                                      mode = ZOOM;
                                                      Log.d(TAG, "mode=ZOOM");
                                                  }
                                                  break;
  
              case MotionEvent.ACTION_MOVE:
  
                                                  if (mode == DRAG) 
                                                  { 
                                                      matrix.set(savedMatrix);
                                                      matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); 
                                                  } 
                                                  else if (mode == ZOOM) 
                                                  { 
  
                                                      float newDist = spacing(event);
                                                      Log.d(TAG, "newDist=" + newDist);
                                                      if (newDist &gt; 5f) 
                                                      {
                                                          matrix.set(savedMatrix);
                                                          scale = newDist / oldDist; 
                                                          matrix.postScale(scale, scale, mid.x, mid.y);
                                                      }
                                                  }
                                                  break;
          }
  
          view.setImageMatrix(matrix); 
          return true; 
      }
  
  
  
      private float spacing(MotionEvent event) 
      {
          float x = event.getX(0) - event.getX(1);
          float y = event.getY(0) - event.getY(1);
          return FloatMath.sqrt(x * x + y * y);
      }
  
  
      private void midPoint(PointF point, MotionEvent event) 
      {
          float x = event.getX(0) + event.getX(1);
          float y = event.getY(0) + event.getY(1);
          point.set(x / 2, y / 2);
      }
  
  }
  </code></pre>
  
  <p><strong>imagedisp.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:gravity="center"&gt;
  
      &lt;ImageView android:layout_width="fill_parent" 
          android:id="@+id/imageView1"
          android:src="@drawable/img1" 
          android:layout_height="fill_parent"
          android:scaleType="centerInside"&gt;
  
      &lt;/ImageView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>