title: How to reset position of ImageView?
body: <p>In my program can I move image across screen? Sometimes I am loosing it from site and cannot find it. I wish to add function which will place imag on original position. 
  My own aproach wich does not work (<a href="https://stackoverflow.com/questions/17694347/is-there-history-or-matrix-translation">and related question to it to make it work</a>):</p>
  
  <pre><code>ImageView iv = current;
  Matrix matrix = iv.getImageMatrix;
  float[] values = new float[9];
  matrix.getValues(values);
  int a = values[Matrix.MTRANS_X];
  int b = values[Matrix.MTRANS_Y];
  matrix.postTranslate(-a,-b);
  iv.setImageMatrix(matrix);
  </code></pre>
  
  <p><strong>EDIT:</strong> Latest code</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.concurrent.ExecutionException;
  
  import ua.mirkvartir.android.frontend.UILApplication;
  import android.app.Activity;
  import android.app.Fragment;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Matrix;
  import android.graphics.PointF;
  import android.graphics.drawable.BitmapDrawable;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.support.v4.view.MotionEventCompat;
  import android.util.FloatMath;
  import android.util.Log;
  import android.view.Gravity;
  import android.view.MotionEvent;
  import android.view.ScaleGestureDetector;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.View.OnTouchListener;
  import android.view.ViewGroup;
  import android.view.ViewGroup.LayoutParams;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemSelectedListener;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.Gallery;
  import android.widget.ImageView;
  import android.widget.ImageView.ScaleType;
  import android.widget.LinearLayout;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  
  public class ImageShowActivity extends Activity {
      int p = 0;
      Activity app;
      ImageAdapterr ia;
      ImageView imView;
      int imgPos = 0;
      Bitmap bm = null;
      int geg = 90;
      public int width = 0;
      public int hight = 0;
      Gallery gallery;
      Matrix matrix = new Matrix();
      Matrix shift = new Matrix();
      private int INVALID_POINTER_ID = -1;
      private int mActivePointerId = INVALID_POINTER_ID;
      private ScaleGestureDetector mScaleDetector;
      private float mLastTouchX = 0;
      private float mLastTouchY = 0;
      private float mPosX = 0;
      private float mPosY = 0;
      ImageView current;
      public List&lt;ImageView&gt; images;
      public int reset = 0;
      HashMap&lt;Integer, Bitmap&gt; map = new HashMap&lt;Integer, Bitmap&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.image_show);
          app = this;
          images = new ArrayList&lt;ImageView&gt;(
                  UILApplication.photo_buffer_big.size());
          Log.d("images", UILApplication.photo_buffer_big.size() + "");
          Log.d("images", images.size() + "");
          gallery = (Gallery) findViewById(R.id.gallery);
          // EDGES ARE INVISIBLE
  
          gallery.setHorizontalFadingEdgeEnabled(false);
  
          ia = new ImageAdapterr(this);
  
          gallery.setAdapter(ia);
          final int length = UILApplication.photo_buffer_big.size();
          Button back_btn = (Button) findViewById(R.id.analitics_back_btn);
          back_btn.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  finish();
              }
          });
          final TextView img_counter_tv = (TextView) findViewById(R.id.img_counter);
          img_counter_tv.setText(p + 1 + "/" + length);
          Button nextButton = (Button) findViewById(R.id.next_btn);
          nextButton.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  if (p &lt; length - 1) {
                      p++;
                  } else {
                      p = 0;
                  }
                  gallery.setSelection(p, true);
                  img_counter_tv.setText(p + 1 + "/" + length);
              }
          });
  
          Button backButton = (Button) findViewById(R.id.back_btn);
          backButton.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  if (p == 0) {
                      p = length - 1;
                  } else {
                      p--;
                  }
                  gallery.setSelection(p, true);
                  img_counter_tv.setText(p + 1 + "/" + length);
  
              }
          });
  
  
      }
  
      public void rotateS(View v) {
          ImageView iv = current;
          Bitmap b = ((BitmapDrawable) iv.getDrawable()).getBitmap();
          Matrix matrix = new Matrix();
          matrix.postRotate(geg);
          Bitmap bMapRotate = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                  b.getHeight(), matrix, true);
          iv.setImageBitmap(bMapRotate);
      }
  
      public void extendS(View v) {
  
          reset = 1;
          Log.d("restart", "yes");
  
          runOnUiThread(new Runnable() {
              public void run() {
                  View viewToUpdate = gallery.getChildAt(p - gallery.getFirstVisiblePosition());
                  viewToUpdate.invalidate();
  //              ia.notifyDataSetChanged();
  
              }
          });
  
  
  
      }
  
      public static Bitmap resizeBitmap(Bitmap photo, float x, float y) {
  
          try {
              // get current bitmap width and height
              int width = photo.getWidth();
              int height = photo.getHeight();
  
              // determine how much to scale
              float scaleWidth = x / width;
              float scaleHeight = y / height;
              Log.d("aspect3", "w: " + scaleWidth + " h: " + scaleHeight);
              // create the matrix for the manipulation
              Matrix matrix = new Matrix();
              // resize the bitmap
              matrix.postScale(scaleWidth, scaleHeight);
  
              // recreate the new bitmap
              Bitmap resizebitmap = Bitmap.createBitmap(photo, 0, 0, width,
                      height, matrix, false);
              return resizebitmap;
  
          } catch (NullPointerException e) {
              e.printStackTrace();
          } catch (OutOfMemoryError e) {
              e.printStackTrace();
              System.gc();
          }
          return null;
      }
  
      class ImageAdapterr extends BaseAdapter {
  
          /** The parent context */
          private Context myContext;
  
          /** Simple Constructor saving the 'parent' context. */
          public ImageAdapterr(Context c) {
              this.myContext = c;
          }
          Matrix savedMatrix = new Matrix();
          /** Returns the amount of images we have defined. */
          public int getCount() {
              return UILApplication.photo_buffer_big.size();
          }
  
          /* Use the array-Positions as unique IDs */
          public Object getItem(int position) {
              return position;
          }
  
          public long getItemId(int position) {
              return position;
          }
  
          /**
           * Returns a new ImageView to be displayed, depending on the position
           * passed.
           */
  
  
          public View getView(final int position, View convertView,
                  ViewGroup parent) {
  
              ImageView imView = new ImageView(this.myContext);
              current = imView;
              imgPos = position;
              if (bm==null){
              AsyncLoad imLoad = new AsyncLoad();
              imLoad.execute();
              try {
                  bm = imLoad.get();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (ExecutionException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              }
              if (bm != null) {
                  imView.setImageBitmap(bm);
              } else if (bm == null) {
                  imView.setImageResource(R.drawable.logo);
              }
              /* Image should be scaled as width/height are set. */
              imView.setScaleType(ImageView.ScaleType.FIT_CENTER);
              /* Set the Width/Height of the ImageView. */
              imView.setLayoutParams(new Gallery.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
              imView.setOnTouchListener(new OnTouchListener() {
                  private static final String TAG = "Touch";
                  // These matrices will be used to move and zoom image
  
  
                  PointF start = new PointF();
                  public PointF mid = new PointF();
  
                  // We can be in one of these 3 states
                  public static final int NONE = 0;
                  public static final int DRAG = 1;
                  public static final int ZOOM = 2;
                  public int mode = NONE;
  
                  float oldDist;
  
                  public boolean onTouch(View v, MotionEvent event) {
  
                      ImageView view = (ImageView) v;
                      view.setScaleType(ImageView.ScaleType.MATRIX);
                      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_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;
  
                      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; 10f) {
  
                                  matrix.set(savedMatrix);
                                  float scale = newDist / oldDist;
                                  matrix.postScale(scale, scale, mid.x, mid.y);
                              }
                          }
                          break;
  
                      case MotionEvent.ACTION_UP:
                      case MotionEvent.ACTION_POINTER_UP:
  
                          mode = NONE;
                          Log.d(TAG, "mode=NONE");
                          break;
                      }
  
                      // Perform the transformation
                      Log.d("point",
                              (event.getX() - start.x) + " "
                                      + (event.getY() - start.y));
                      // Log.d("point",start.x +" "+start.y);
                      float[] values = new float[9];
                      matrix.getValues(values);
                      float a = values[Matrix.MTRANS_X];
                      float b = values[Matrix.MTRANS_Y];
                      Log.d("touch matrix", values[Matrix.MPERSP_0] + " "
                              + values[Matrix.MPERSP_1] + " "
                              + values[Matrix.MPERSP_2]);
                      Log.d("touch matrix scale", values[Matrix.MSCALE_X] + " "
                              + values[Matrix.MSCALE_Y]);
                      Log.d("touch matrix scew", values[Matrix.MSKEW_X] + " "
                              + values[Matrix.MSKEW_Y]);
                      Log.d("touch matrix trans", values[Matrix.MTRANS_X] + " "
                              + values[Matrix.MTRANS_Y]);
                      if (reset == 1) {
                          matrix.reset();
                          savedMatrix.reset();
                      }
  
                      view.setImageMatrix(matrix);
                      reset = 0;
                      // images.set(position, view);
                      return true; // indicate event was handled
                  }
  
                  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);
                  }
  
              });
  
              return imView;
          }
  
          /**
           * Returns the size (0.0f to 1.0f) of the views depending on the
           * 'offset' to the center.
           */
          public float getScale(boolean focused, int offset) {
              /* Formula: 1 / (2 ^ offset) */
              return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
          }
      }
  
      class AsyncLoad extends AsyncTask&lt;Void, Void, Bitmap&gt; {
          ProgressDialog pd;
  
          @Override
          protected void onPreExecute() {
              pd = new ProgressDialog(app);
              pd.setOwnerActivity(app);
              pd.setTitle("Идет загрузка...");
              pd.setCancelable(true);
              pd.show();
          }
  
          @Override
          protected Bitmap doInBackground(Void... arg0) {
              // TODO Auto-generated method stub
              try {
                  /*
                   * Open a new URL and get the InputStream to load data from it.
                   */
                  URL aURL = new URL(UILApplication.photo_buffer_big.get(imgPos));
                  URLConnection conn = aURL.openConnection();
                  conn.connect();
                  InputStream is = conn.getInputStream();
                  /* Buffered is always good for a performance plus. */
                  BufferedInputStream bis = new BufferedInputStream(is);
                  /* Decode url-data to a bitmap. */
                  bm = BitmapFactory.decodeStream(bis);
                  bis.close();
                  is.close();
                  /* Apply the Bitmap to the ImageView that will be returned. */
                  // imView.setImageBitmap(bm);
              } catch (IOException e) {
                  // imView.setImageResource(R.drawable.logo);
                  bm = null;
                  Log.e("DEBUGTAG", "Remote Image Exception", e);
              }
              map.put(imgPos, bm);
              Log.d("map", map.size() + "");
              return bm;
          }
  
          @Override
          protected void onPostExecute(Bitmap arg0) {
              pd.dismiss();
          }
  
      }
  }
  </code></pre>
  
  <p>XML
      
      </p>
  
  <pre><code>    &lt;RelativeLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/top_tab_bg"
          android:padding="10dp" &gt;
  
          &lt;Button
              android:id="@+id/analitics_back_btn"
              style="@style/ButtonText"
              android:layout_marginRight="5dip"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_toLeftOf="@+id/back_btn"
              android:background="@drawable/btn_clk_selector"
              android:text="Назад" /&gt;
  
          &lt;Button
              android:id="@+id/btn_rotate"
              style="@style/ButtonText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginRight="5dip"
              android:layout_toLeftOf="@+id/analitics_back_btn"
              android:background="@drawable/btn_clk_selector"
              android:onClick="rotateS"
              android:text="Повернуть" /&gt;
          &lt;Button
              android:id="@+id/btn_ex"
              style="@style/ButtonText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginRight="5dip"
              android:layout_toLeftOf="@+id/btn_rotate"
              android:background="@drawable/btn_clk_selector"
              android:onClick="extendS"
              android:text="рас" /&gt;
  
          &lt;Button
              android:id="@+id/next_btn"
              style="@style/ButtonText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:layout_marginLeft="5dp"
              android:background="@drawable/next_img_btn"
              android:paddingRight="10dp" /&gt;
  
          &lt;TextView
              android:id="@+id/img_counter"
              style="@style/ButtonText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerVertical="true"
              android:layout_toLeftOf="@+id/next_btn"
              android:text="1/10" /&gt;
  
          &lt;Button
              android:id="@+id/back_btn"
              style="@style/ButtonText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginRight="5dp"
              android:layout_toLeftOf="@+id/img_counter"
              android:background="@drawable/back_img_btn" /&gt;
      &lt;/RelativeLayout&gt;
  
  &lt;RelativeLayout
      android:id="@+id/gal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" &gt;
  
              &lt;Gallery
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:id="@+id/gallery"
                  android:adjustViewBounds="true"
                  android:spacing="10dp"
              /&gt;
  &lt;/RelativeLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong> problem with new code - image is not rested after enlargeS was called. It is reseted after enlargeS call + tab on screen. Also image is partially reseted - it goes to original position and scale, but it goes to its original resolution. So usually i bacamos much smaller then screen, while i need it to fill screen.</p>