Sign in to follow this  
tom_mai78101

2D Bitmap Matrix manipulation: How does one flip a bitmap?

Recommended Posts

tom_mai78101    693
Currently, I have created a simple SurfaceView that blits a bitmap onto the screen, like how Canvas does in Java. I'm currently playing around the Matrix variable, so that I can pass it to the Canvas.drawBitmap() as a parameter.

The matrix manipulation, something that is used prominently in bitmap blitting, is a bit difficult for me to grasp correctly. Especially at the point where the Matrix involves pre- or post- when doing calculations (preScale(), preSkew(), preTranslate(), etc., and postScale(), postSkew(), etc.)

I'm using the class "android.graphics.Matrix", and not the "android.opengl.Matrix", just to clarify.

[source lang="java"] Matrix matrix = new Matrix();
matrix.reset();[/source]
I think this initializes the matrix variable.

[source lang="java"]Canvas c = holder.lockCanvas();
c.drawRGB(128, 128, 120);
matrix.postScale(-1, 1);
c.drawBitmap(bitmap, matrix, null);
holder.unlockCanvasAndPost©;[/source]
After that, the bitmap itself does nothing. What am I doing wrong? Isn't it?:

[source lang="java"] 1 0 * [-1] = -1 0
0 1 [ 1] 0 1[/source] Edited by tom_mai78101

Share this post


Link to post
Share on other sites
tom_mai78101    693
Update [Solved]:

I found the algorithm to matrix manipulation here. Quoted from there below:
[quote]
Usually it is [b]scale * rotation * translation. [/b]However, if you want to rotate an object around a certain point, then it is [b]scale * point_translation * rotation * object_translation[/b].

Why: First you want to scale the object so that the translations work properly. Then you rotate the axes so the translation takes place on the adjusted axes. Finally you translate the object to it's position.
[/quote]

Sorry to waste your time. In the end, I learned something. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

EDIT:

The "point_translation" is a bit confusing for me, but I managed it. The origin of an identity matrix is at (0, 0), with X (right = positive), Y (down = positive), which is also the top left corner of a bitmap. (Don't know how to type that...)

To create the point_translation, we need to move the origin in the opposite direction we intended to move on the bitmap object. That means if we focus on the bitmap only, the origin should be set to move to a spot on the bitmap, then multiply the offset values by -1.

It will move the origin backwards. That's all the explanation I can say. It's a tricky and confusing one to why it works, but that's math for me. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by tom_mai78101

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this