Reputation: 1
i am using this code to scale my canvas around a focus point
private class MyScaleDetector
extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
float focusx=detector.getFocusX();
float focusy=detector.getFocusY();
return super.onScaleBegin(detector);
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
float factor=detector.getScaleFactor();
scale *= factor;
scale = Math.max(0.4f, Math.min(scale, 20.0f));
return true;
}
}
and this is the code i use to scale inside the ondraw method
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.scale(scale,scale,focusx,focusy);
}
my problem is when i start scaling around the focus point (200,200) for example ,at first it start going there but than while increasing the scale it start going away from the point . so my problem is how i scale toward a specific point so that that this point becomes the center of the viewport while scaling .
I think i should use canvas.translate() with it but i don't know how much i should move the x and y position of canvas while scaling .
Edit:the image below summarize what i am trying to say
Upvotes: 0
Views: 1641
Reputation: 611
There are several things to bear in mind here:
The broad approach I have used in the past is:
Note that in onDraw():
Edit to address more specific request:
If all you really need to do is simply scale around a specific redefined point p (px, py) by an amount s, and keep p visible, then you can do the following:
canvas.translate(px, py);
canvas.scale(s,s);
canvas.translate(-px*s, py*s);
This should keep p in the same pixel-position it was in prior to scaling. This will suffer from all the problems alluded to above as well as the risks of drift due to successive scale events around a moving focus (based on your original code). You are far better off doing what I described above.
Upvotes: 1