title: centered zoom in custom view - calculating canvas coordinates
body: <p>I am working on my first "real" Android application, a graphical workflow editor. The drawing is done in a custom class, that is a subclass of View.At the moment my elements are rectangles, which are drawn on a canvas. To detect actions on elements I compare the coordinates and check for elements on the touch location. </p>
  
  <p>To implement a zoom gesture I tried <a href="http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html" rel="nofollow">http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html</a></p>
  
  <p>With the 4 argument canvas.scale(...) function the centered zooming works well, but I lose the ability to calculate the canvas coordinates using the offset with mPosX and mPosY to detect if the touch after a zoom is on an element.</p>
  
  <p>I tried to change the example in the blogpost above to center the canvas on the zoom gesture with:</p>
  
  
  
  <pre><code>canvas.save();
  canvas.translate(mPosX, mPosY);
  canvas.scale(mScaleFactor, mScaleFactor, mScalePivotX, mScalePivotY);
  //drawing ....
  canvas.restore();
  </code></pre>
  
  <p>I did not find any examples on how this could be done without losing the reference offset to calculate the coordinates. Is there an easy workaround? I tried to calculate the offset with the gesture center and the scaling factor, but failed :/</p>
  
  <p>I have seen that other examples which use an ImageView often use a Matrix to transform the image. Could this be done with a custom View and a Canvas? If yes, how can I get the x and y offset to check the coordinates?</p>
  
  <p>Also, if my ideas are completely wrong, I would be very happy to see some examples on how this is done properly.</p>
  
  <p>Thx! ;)</p>