[英]how to scale using canvas
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 这是我用来在ondraw方法中缩放的代码
@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 . 我的问题是,例如,当我开始围绕焦点(200,200)进行缩放时,起初它开始向该点移动,但是当增大缩放比例时,它开始偏离该焦点。 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 . 我想我应该将canvas.translate()与它一起使用,但是我不知道在缩放时应该移动画布的x和y位置多少。
Edit:the image below summarize what i am trying to say 编辑:下面的图像总结了我想说的
There are several things to bear in mind here: 这里有几件事要牢记:
The broad approach I have used in the past is: 我过去使用的广泛方法是:
Note that in onDraw(): 注意在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: 如果您真正需要做的只是简单地将特定的重新定义点p(px,py)缩放s,并使p可见,那么您可以执行以下操作:
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. 这应将p保持在缩放之前的相同像素位置。 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.
您做我上面描述的事情要好得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.