2

All, I've extended the ImageView in order to implement pinch and zoom scaling on the image. This is done by modifying the matrix and applying it to the image. Now, I am also overwriting the onDraw() to draw primitives (i.e. rectangles and circles). I've applied the matrix to the canvas and it appears to have handled the scaling properly, but the only problem is that that position is off on the drawn items. How do I go about translating the positions of the drawn items to reflect the new scale?

serveace6
  • 87
  • 1
  • 7

1 Answers1

1

There is an aproach without matrix, you can implement the pinch and zoom directly in the onDraw method. Check this blog post: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html

  @Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.translate(mPosX, mPosY);
    mIcon.draw(canvas);
    canvas.restore();
}
ovonel
  • 232
  • 3
  • 12
  • thanks that actually worked well. However, once the translation and/or scaling is done, my touch code to catch touches on the primitives do not work anymore. Is there a way to translate values of the onTouch(MotionEvent) to match the on currently on the canvas? – serveace6 Jan 28 '12 at 21:16
  • you have to calculate the position of your objects after the translation. – ovonel Jan 30 '12 at 17:57
  • I was able to translate the position by subtracting the touch event's position by an offset but any ideas on factoring the scale? – serveace6 Feb 01 '12 at 00:32
  • I have used something like this: (x- lastMoveX) * (1/(mScaleFactor)) and the same for y. But this calculation is without scale pivot. If you have equation with pivot, let me know... – ovonel Feb 01 '12 at 19:50
  • http://stackoverflow.com/questions/6835224/android-bitmap-canvas-offset-after-scale check this out for the offset after scale – ovonel Feb 01 '12 at 20:01
  • I actually did something like this (x-xOffset)/mScaleFactor and that work without a pivot scale. I'm trying to find out how to do it but I'll let you know if I do. – serveace6 Feb 01 '12 at 20:15
  • sure it works without pivot calculation because the default pivot is zero. But if you like to use it e.g to zoom into the middle of the pic then you have to calculate it. Btw. I guess you can mark this question as solved :) – ovonel Feb 01 '12 at 21:13