title: Center the image in ImageView after zoom-pinch
body: <p>I'm using <code>eclipse</code>.</p>
  
  <blockquote>
    <p>I'm having problem with getting the image in the center with matrix
    scale type.</p>
  </blockquote>
  
  <p>I mean that I want to have the matrix scale type and also have the image in the center ...
  to put it simply when I run the app for the first time it's image is scale and is in the left side of the screen</p>
  
  <p>I want that when I'm running the app for the first time ..have the image in the center of view with no scales.</p>
  
  <p>Now my first screen looks like this..</p>
  
  <p><img src="https://i.stack.imgur.com/oTnRq.jpg" alt="enter image description here"></p>
  
  <p>I want it to look like this..</p>
  
  <p><img src="https://i.stack.imgur.com/5WmL8.jpg" alt="enter image description here"></p>
  
  <ul>
  <li>have also tied these topics but couldn't find any solution.</li>
  </ul>
  
  <p><a href="https://stackoverflow.com/questions/24381775/fit-to-android-imageview-matrix-center-of-screen">Fit to Android Imageview Matrix Center of Screen</a></p>
  
  <p><a href="https://stackoverflow.com/questions/21539949/scale-and-center-matrix-on-arbitrary-point">Scale and center matrix on arbitrary point</a></p>
  
  <p><a href="https://stackoverflow.com/questions/21366354/how-to-use-scaletype-of-a-imageview-as-center-and-matrix-at-the-sametime">How to use scaletype of a ImageView as center and matrix at the sametime?</a></p>
  
  <p>And this is my code...</p>
  
  <pre><code>public class MainActivity extends Activity {
  
     private ImageView img;
     private Matrix matrix = new Matrix();
     private float scale = 1f;
     private ScaleGestureDetector SGD;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView)findViewById(R.id.imageView1);
        SGD = new ScaleGestureDetector(this,new ScaleListener());
  
     }
  
  
     @Override
     public boolean onTouchEvent(MotionEvent ev) {
        SGD.onTouchEvent(ev);
        return true;
     }
  
     private class ScaleListener extends ScaleGestureDetector.
     SimpleOnScaleGestureListener {
     @Override
     public boolean onScale(ScaleGestureDetector detector) {
        scale *= detector.getScaleFactor();
        scale = Math.max(0.1f, Math.min(scale, 5.0f));
        matrix.setScale(scale, scale);
        img.setImageMatrix(matrix);
        return true;
     }
  }
  
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
     }
  </code></pre>
  
  <p><b> Layout  XML :</b></p>
  
  <pre><code>&lt;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" &gt;
  
  &lt;ImageView
      android:id="@+id/imageView1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:scaleType="matrix"
      android:src="@drawable/gh" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>