title: How to find absolute position of click while zoomed in
body: <p>Please see each section below for a description of my problem described in three separate ways. Hopefully should help people to answer.</p>
  
  <p><strong>Problem:</strong> How do you find a pair of coordinate expressed in canvas/userspace when you only have the coordinate expressed in terms of a zoomed image, given the original scale point &amp; scale factor?</p>
  
  <p><strong>Problem in practice:</strong></p>
  
  <p>I'm currently trying to replicate the zoom functionality used in apps such as the gallery / maps, when you can pinch to zoom/zoom out with the zoom moving towards the midpoint of the pinch. </p>
  
  <p>On down I save the centre point of the zoom (which is in X,Y coordinates based on the current screen). I then have this function act when a "scale" gesture is detected:</p>
  
  <pre><code>class ImageScaleGestureDetector extends SimpleOnScaleGestureListener {
      @Override
      public boolean onScale(ScaleGestureDetector detector) {
          if(mScaleAllowed) 
              mCustomImageView.scale(detector.getScaleFactor(), mCenterX, mCenterY);
          return true;
      }
  }   
  </code></pre>
  
  <p>The scale function of the CustomImageView look like this:</p>
  
  <pre><code>public boolean scale(float scaleFactor, float focusX, float focusY) {
      mScaleFactor *= scaleFactor;
  
      // Don't let the object get too small or too large.
      mScaleFactor = Math.max(MINIMUM_SCALE_VALUE, Math.min(mScaleFactor, 5.0f));
  
      mCenterScaleX = focusX;
      mCenterScaleY = focusY;
  
      invalidate();
  
      return true;
  }
  </code></pre>
  
  <p>The drawing of the scaled image is achieved by overriding the onDraw method which scales the canvas around the centre ands draw's the image to it.</p>
  
  <pre><code>@Override
  public void onDraw(Canvas canvas) {
      super.onDraw(canvas);
  
      canvas.save();
      canvas.translate(mCenterScaleX, mCenterScaleY);
      canvas.scale(mScaleFactor, mScaleFactor);
      canvas.translate(-mCenterScaleX, -mCenterScaleY);
      mIcon.draw(canvas);
      canvas.restore();
  }
  </code></pre>
  
  <p>This all works fine when scaling from ScaleFactor 1, this is because the initial mCenterX and mCenterY are coordinates which are based on the device screen. 10, 10 on the device is 10, 10 on the canvas.</p>
  
  <p>After you have already zoomed however, then next time you click position 10, 10 it will no longer correspond to 10, 10 in the canvas because of the scaling &amp; transforming that has already been performed.</p>
  
  <p><strong>Problem in abstraction:</strong></p>
  
  <p>The image below is an example of a zoom operation around centre point A. Each box represents the position and size of the view when at that scale factor (1, 2, 3, 4, 5).</p>
  
  <p><img src="https://i.stack.imgur.com/1fi31.png" alt="Example"></p>
  
  <p>In the example if you scaled by a factor of 2 around A then you clicked on position B, the X, Y reported as B would be based on the screen position - not on the position relative to 0,0 of the initial canvas.</p>
  
  <p>I need to find a way of getting the absolute position of B.</p>