如何找到Canvas中当前的翻译位置?

发布于 2024-10-15 12:25:56 字数 892 浏览 1 评论 0原文

如何从画布获取当前翻译位置?我正在尝试绘制我的坐标是相对坐标(彼此之间)和绝对坐标(相对于画布)的混合的东西。

可以说我想做

canvas.translate(x1, y1);
canvas.drawSomething(0, 0); // will show up at (x1, y1), all good
// now i want to draw a point at x2,y2
canvas.translate(x2, y2);
canvas.drawSomething(0, 0); // will show up at (x1+x2, y1+y2)
// i could do
canvas.drawSomething(-x1, -y1);
// but i don't always know those coords

这工作但很脏:

private static Point getCurrentTranslate(Canvas canvas) {
    float [] pos = new float [2];
    canvas.getMatrix().mapPoints(pos);
    return new Point((int)pos[0], (int)pos[1]);
}
...
Point p = getCurrentTranslate(canvas);
canvas.drawSomething(-p.x, -p.y);

画布有一个 getMatrix 方法,它有一个 setTranslate 但没有 getTranslate 。我不想使用 canvas.save()canvas.restore() 因为我绘制东西的方式有点棘手(而且可能很混乱.. .)

有没有更干净的方法来获取这些当前坐标?

How do I get the current translate position from a Canvas? I am trying to draw stuff where my coordinates are a mix of relative (to each other) and absolute (to canvas).

Lets say I want to do

canvas.translate(x1, y1);
canvas.drawSomething(0, 0); // will show up at (x1, y1), all good
// now i want to draw a point at x2,y2
canvas.translate(x2, y2);
canvas.drawSomething(0, 0); // will show up at (x1+x2, y1+y2)
// i could do
canvas.drawSomething(-x1, -y1);
// but i don't always know those coords

This works but is dirty:

private static Point getCurrentTranslate(Canvas canvas) {
    float [] pos = new float [2];
    canvas.getMatrix().mapPoints(pos);
    return new Point((int)pos[0], (int)pos[1]);
}
...
Point p = getCurrentTranslate(canvas);
canvas.drawSomething(-p.x, -p.y);

The canvas has a getMatrix method, it has a setTranslate but no getTranslate. I don't want to use canvas.save() and canvas.restore() because the way I'm drawing things it's a little tricky (and probably messy ...)

Is there a cleaner way to get these current coordinates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

眉目亦如画i 2024-10-22 12:25:56

您需要首先重置变换矩阵。我不是 Android 开发人员,查看 android canvas 文档,没有重置矩阵,但有一个 setMatrix(android.graphics.Matrix)。它表示如果给定的矩阵为空,它将把当前矩阵设置为单位矩阵,这就是你想要的。所以我认为你可以通过以下方式重置你的位置(以及比例和倾斜):

canvas.setMatrix(null);

也可以通过 getMatrix 获取当前翻译。有一个 mapVectors() 方法,您可以使用 矩阵 来查看在哪里点 [0,0] 将被映射到,这将是您的翻译。但就你而言,我认为重置矩阵是最好的。

You need to reset the transformation matrix first. I'm not an android developer, looking at the android canvas docs, there is no reset matrix, but there is a setMatrix(android.graphics.Matrix). It says if the given matrix is null it will set the current matrix to the identity matrix, which is what you want. So I think you can reset your position (and scale and skew) with:

canvas.setMatrix(null);

It would also be possible to get the current translation through getMatrix. There is a mapVectors() method you could use for matrices to see where the point [0,0] would be mapped to, this would be your translation. But in your case I think resetting the matrix is best.

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