画布缩放到点 (0,0)

发布于 2024-12-26 12:14:26 字数 568 浏览 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2025-01-02 12:14:26

枢轴点基本上是画布将围绕其进行变换的点,因此以 0,0 为枢轴进行缩放会使其向该点收缩。
使用以下方法,您可以将枢轴点更改为您想要的任何位置:

canvas.scale(x, y, px, py);

现在介绍新内容:
如果您希望画布向其中心缩放,您只需知道画布中间的点:

float cX = canvas.getWidth()/2.0f; //Width/2 gives the horizontal centre
float cY = canvas.getHeight()/2.0f; //Height/2 gives the vertical centre

然后您可以使用这些坐标对其进行缩放:

canvas.scale(x, y, cX, cY);

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:

canvas.scale(x, y, px, py);

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:

float cX = canvas.getWidth()/2.0f; //Width/2 gives the horizontal centre
float cY = canvas.getHeight()/2.0f; //Height/2 gives the vertical centre

And then you can scale it using those coordinates:

canvas.scale(x, y, cX, cY);
慕烟庭风 2025-01-02 12:14:26

看看这两个答案,他们很好地描述了问题及其解决方案。

缩放后的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文