帆布矩阵变换 [英] Canvas matrix transformation

查看:147
本文介绍了帆布矩阵变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个游戏为Android。

I'm currently developing a game for android.

我已经添加按钮,以允许用户导航的相机在x轴方向和放大和缩小。

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

要我用下面的矩阵code做到这一点:

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.

谁能告诉我有什么错呢?我觉得与矩阵的工作的确很麻烦,特别是因为有许多可供选择的Matrix对象(pre,发布和设置)。

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).

感谢

推荐答案

从canvas.getMatrix()在视图中的OnDraw方法中得到的矩阵中已经存在(有些操作缩放视图的显示大小和翻译查看坐标到的ViewRoot表面坐标。

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.

通过使用matric.setScale(),而不是pre或分频器,重置矩阵恒等变换,然后应用比例。这将导致建立为OnDraw的初始变换丢失。矩阵。preTranslate()的罚款。

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.

另外,你可以保持setScale和使用canvas.concat(米)的矩阵应用到现有矩阵。

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

(我不能保持pre的操作和后期应用变换直无论是。)

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

这篇关于帆布矩阵变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆