0

I have a custom view where I load a bitmap image. Then I do some operations like zoom in, zoom out, rotation, drag and change position etc. After these operations I want to draw on the bitmap using finger. I use canvas.drawPath(mPath, mPaint) for this. If I load the image in the screen and start drawing it perfectly works, but if I change position of the image or rotate it or zoom in or out; my path is drawn in different position than I touched. My question is what can be the cause of this problem and how can I solve it.

Mazedul Islam
  • 1,543
  • 1
  • 11
  • 15

2 Answers2

0

Before making changes to the image you're displaying, save the canvas' position. After, restore its original position. Something like this :

public void onDraw(Canvas c) {
  c.save();
  //Do your "image manipulation" logic
  c.restore();
  c.drawPath(mPath, mPaint);
}
NSimon
  • 5,212
  • 2
  • 22
  • 36
0

My problem is solved for dragging the image and repositioning and scaling up and down by the answer of this question.

I have put below code in onTouchEvent method.

float x = event.getX() / drawImage.getScaleX() - rect.left / drawImage.getScaleX();
float y = (event.getY()) / drawImage.getScaleY() - rect.top / drawImage.getScaleY();
float p = drawImage.getCenterX() / drawImage.getScaleX() - rect.left / drawImage.getScaleX();
float q = drawImage.getCenterY() / drawImage.getScaleY() - rect.top / drawImage.getScaleY();
float x1 = (float) ((x - p) * Math.cos(drawImage.getAngle()) + (y - q) * Math.sin(drawImage.getAngle()) + p);
float y1 = (float) ((y - q) * Math.cos(drawImage.getAngle()) - (x - p) * Math.sin(drawImage.getAngle()) + q);

Here drawImage is my custom drawable. x1, y1 are the actual coordinate I was searching for.

Mazedul Islam
  • 1,543
  • 1
  • 11
  • 15