画布矩阵变换

发布于 2024-10-06 09:12:48 字数 439 浏览 2 评论 0原文

我目前正在开发一款安卓游戏。

我添加了按钮,允许用户在 x 轴上导航相机并进行放大和缩小。

为此,我使用以下矩阵代码:

// c is the canvas..
Matrix m = c.getMatrix();
// Make sure that the ground is always at the bottom of the screen
m.setScale(zoom,zoom,0.0f,height);
m.preTranslate(camera_x, 0); // Change offset in x-direction
c.setMatrix(m);

这适用于模拟器,但在我的真实设备上给出了一些奇怪的结果。

谁能告诉我有什么问题吗?我发现使用矩阵很棘手,特别是因为矩阵对象有很多可用选项(前、后和设置)。

谢谢

I'm currently developing a game for android.

I have added buttons to allow the user to navigate the camera in the x-axis and zooming in and out.

To do this I'm using the following matrix code:

// c is the canvas..
Matrix m = c.getMatrix();
// Make sure that the ground is always at the bottom of the screen
m.setScale(zoom,zoom,0.0f,height);
m.preTranslate(camera_x, 0); // Change offset in x-direction
c.setMatrix(m);

This works on the emulator but gives me some weird results on my real device.

Can anyone tell me what's wrong with it? I find working with matrixes to be tricky, especially since there are many options available for the Matrix object (pre,post and set).

Thanks

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

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

发布评论

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

评论(1

没有你我更好 2024-10-13 09:12:49

在 View 的 onDraw 方法中从 canvas.getMatrix() 获得的矩阵已经包含了一些操作(将视图缩放到显示尺寸并将 View 的坐标转换为 ViewRoot Surface 坐标)。

通过使用 matric.setScale(),而不是与 pre 或 postScale 相比,您将矩阵重置为恒等变换,然后应用缩放,这会导致为 onDraw 设置的初始变换丢失。

或者,您可以保留 setScale 并使用。 canvas.concat(m) 将矩阵应用到现有矩阵

(我也无法保持应用变换之前和之后的操作。)

The matrix you get from canvas.getMatrix() in a View's onDraw method already has some manipulations in it (to scale the view to your display size and translate the View's coordinates to the ViewRoot Surface coordinates.

By using matric.setScale(), rather than pre or postScale, you reset the matrix to the identity transform and then apply the scaling. This causes the initial transformation set up for onDraw to be lost. The matrix.preTranslate() is fine.

Alternatively, you could keep the setScale and use canvas.concat(m) to apply your matrix to the existing matrix.

(I can't keep the operation of pre and post applying transformations straight either.)

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