画布缩放到点 (0,0)
我在缩放画布时遇到问题。我制作了一个自定义视图,当我将画布缩小到位置 (0,0) 时,我正在其中绘制关系图。我看过不同的主题和问题,但找不到合适的答案。
我在 onDraw 方法中所做的是。
canvas.scale(mScaleFactor, mScaleFactor);
我还看到了 canvas.scale(x, y, px, py) 方法,但我不知道如何获取 x 和 y 的枢轴点。
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.4f, Math.min(mScaleFactor, 5.0f));
if(mScaleFactor>=1)
mScaleFactor=1f;
invalidate();
return true;
}
I am having a problem in zooming the canvas. I have made a customized view in which I am drawing relationship diagrams now when I zoom out the canvas in goes to the position (0,0). I have seen different threads and questions but could not find appropriate answer.
What i am doing in onDraw Method is.
canvas.scale(mScaleFactor, mScaleFactor);
I have also seen the canvas.scale(x, y, px, py) method but i do not know how to get the pivot points of x and y.
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.4f, Math.min(mScaleFactor, 5.0f));
if(mScaleFactor>=1)
mScaleFactor=1f;
invalidate();
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
枢轴点基本上是画布将围绕其进行变换的点,因此以 0,0 为枢轴进行缩放会使其向该点收缩。
使用以下方法,您可以将枢轴点更改为您想要的任何位置:
现在介绍新内容:
如果您希望画布向其中心缩放,您只需知道画布中间的点:
然后您可以使用这些坐标对其进行缩放:
The pivot points are basically the point that your canvas will be transformed around, so scaling with a pivot of 0,0 makes it shrink towards that point.
using the following method you can change the pivot point to wherever you want it:
Now for the new stuff:
If you want your canvas to be scaled towards its centre, you will just have to know the point in the middle of your canvas:
And then you can scale it using those coordinates:
看看这两个答案,他们很好地描述了问题及其解决方案。
缩放后的 Android 位图/画布偏移
缩放 ImageView 的图像,同时将中心点保持在同一位置
Take a look at these two answers, they describe the problem and its solutions very well.
Android Bitmap/Canvas offset after scale
Scaling image of ImageView while maintaining center point in same place