title: Getting (x,y) coordinates of a click after zooming has occurred
body: <p>I've got a custom view, which is essentially a grid drawn onto a canvas, for which I have implemented panning and zooming. These work fine, but I also want to be able to find the grid coordinates of a click. </p>
  
  <p>In order to do this, I must compensate for both the amount that the view has been panned, as well as the amount that it has been zoomed/scaled. This is what I am having trouble with.</p>
  
  <p>To compensate for the amount it has been panned I keep track of all translations (or displacement) that have occurred in <code>PointF dspl</code>. In the <code>onTouchEvent(MotionEvent event)</code> method of my custom view I switch on <code>event.getAction() &amp; MotionEvent.ACTION_MASK</code> and in the case <code>MotionEvent.ACTION_DOWN</code> set the starting point of the event as well as the starting displacement and some other self explanatory things:</p>
  
  <pre><code>case MotionEvent.ACTION_DOWN:
      //Remember where we started
  
      start.set(event.getX(), event.getY());   //set starting point of event
      last.set(event.getX(),event.getY());     //set coordinates of last point touched
      startDspl.set(dspl.x,dspl.y);            //set starting displacement to current displacement
  
      //save the id of this pointer
      activePointerId = event.getPointerId(0);
  
      savedMatrix.set(matrix);                 //save the current matrix
      mode = DRAG;
  
      break;
  </code></pre>
  
  <p>and then in the case<code>MotionEvent.ACTION_UP</code> I do the following:</p>
  
  <pre><code> case MotionEvent.ACTION_UP:
     mode = NONE;
     activePointerId = INVALID_POINTER_ID;
  
     distance = Math.sqrt( (start.x - last.x)*(start.x - last.x) + 
                           (start.y - last.y)*(start.y - last.y));
  
     if( distance &lt; 5){
          //need to translate x and y due to panning and zooming
  
          int [] coord = getClickCoordinates(start.x,start.y);
          Toast.makeText(context, " x = " + coord[0] + ", y = " + coord[1], 
          Toast.LENGTH_SHORT).show();
      }
  </code></pre>
  
  <p>and finally the method where I get the coordinates:</p>
  
  <pre><code>public int[] getClickCoordinates(float clickX, float clickY) {
  
      float x = (clickX - startDspl.x)/(cellWidth*scaleFactor);
      float y = nRows - (clickY - startDspl.y)/(cellHeight*scaleFactor);
  
      return new int[] { (int) x, (int) y };
  }
  </code></pre>
  
  <p>(Here <code>scaleFactor</code> is the amount that the view has been scaled overall). This method does not work for panning, as I expected it should, and I've no idea how to modify it to properly account for zooming.</p>
  
  <p>I would greatly appreciate any help as I've been struggling with this for quite a long time now, and yes I am aware of <a href="https://stackoverflow.com/questions/8180442/how-to-find-absolute-position-of-click-while-zoomed-in">this</a> question, which is a little different from what I want to do.</p>
  
  <p>Thanks!</p>