Canvas matrix transformation
I'm currently developing a game for android.
I have added buttons to allow the use开发者_JAVA技巧r 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
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.)
精彩评论