title: Android how to reset ImageView layout to original after zooming and drag
body: <p>I need to implement zooming, panning and then reset the ImageView to original scale/location.  I use view.setImageMatrix(matrix) to perform zooming and moving in FrameLayout. Works perfectly so far.  To restore to original layout, I tried to save the original matrix and reset to it,   It doesn't work and I am kinda stuck. Appreciate any suggestions or tips!</p>
  
  <p>The following are my current code:</p>
  
  <ul>
  <li>a ViewPager contains fragments per Image</li>
  </ul>
  
  <p></p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;android.support.v4.view.ViewPager
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <ul>
  <li>The fragment contains ImageView</li>
  </ul>
  
  <p></p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;ImageView
          android:id="@+id/ivPage"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_gravity="center"
          android:scaleType="matrix"
          android:src="@drawable/catalogue_test" /&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <ul>
  <li>The java code does the zooming and panning work just fine, that actually was original from a tutorial page:</li>
  </ul>
  
  <p></p>
  
  <pre><code>@Override
  public boolean onTouch(View v, MotionEvent event) {
  
        ImageView view = (ImageView) v;
  
        switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
           Log.d(tag, "ACTION_DOWN");
           isDoubleTap = false;
  
           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:
           Log.d(tag, "ACTION_POINTER_UP");
           mode = NONE;
           Log.d(tag, "mode=NONE");
           break;
        case MotionEvent.ACTION_MOVE:
           Log.d(tag, "ACTION_MOVE");
           if (mode == DRAG) {
              matrix.set(savedMatrix);
              matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
           } else if (mode == ZOOM) {
              double newDist = spacing(event);
              Log.d(tag, "newDist=" + newDist);
              if (newDist &gt; 10f) {
                 matrix.set(savedMatrix);
                 float scale = (float) (newDist / oldDist);
                 matrix.postScale(scale, scale, mid.x, mid.y);
              }
           }
  
           if(isDoubleTap == false) {
              view.setImageMatrix(matrix);
              Log.d(tag, String.format("(%d, %d) - scale(%f, %f) xy(%f, %f)", view.getTop(), view.getLeft(),  view.getScaleX(), view.getScaleY(), view.getX(), view.getY()));
           }
  
           break;
  
        case MotionEvent.ACTION_POINTER_DOWN:
           oldDist = spacing(event);
           Log.d(tag, "oldDist=" + oldDist);
           if (oldDist &gt; 10f) {
              savedMatrix.set(matrix);
              midPoint(mid, event);
              mode = ZOOM;
              Log.d(tag, "mode=ZOOM");
           }
           break;
        }
  
        gestureDetector.onTouchEvent(event);
        return true; // indicate event was handled
     }
  </code></pre>