[android-developers] Re: drawBitmap Flip, Scale and Translate
Keep in mind that the origin of a bitmap draw operation is the upper left corner of the bitmap. So is the origin of canvas (unless you change it with a transformation matrix).
When you tell canvas to render everything flipped horizontally and draw the bitmap on the canvas at (0;0) the bitmap will be rendered outside the canvas to the left. Just imagine that all pixel coordinates of canvas are multiplied by your scale values. Your x-axis scale value gives every pixel's x-coordinate a negative sign. So every pixel on the visible side of the canvas is flipped alongside the left edge of the canvas and become invisible. You need to translate the bitmap horizontally to compensate for that.
You can set an additional postTranslate on the transformation matrix. Or you could set the x-coordinate of the bitmap draw operation accordingly.
Matrix flipmatrix = new Matrix();
flipmatrix.setScale(-1, 1);
flipmatrix.postTranslate(-canvas.getWidth(), 0);
I haven't tried that myself and I'm sometimes also a bad with affine matrix calculations. I'm not sure about the negative sign of canvas.getWidth() so you probably need to play around a bit with that.
You also probably should try a plain bitmap draw operation without source and target rect but just coordinates in order to see if that works for you.
On Saturday, August 10, 2013 11:26:24 AM UTC-5, decode wrote:
-- When you tell canvas to render everything flipped horizontally and draw the bitmap on the canvas at (0;0) the bitmap will be rendered outside the canvas to the left. Just imagine that all pixel coordinates of canvas are multiplied by your scale values. Your x-axis scale value gives every pixel's x-coordinate a negative sign. So every pixel on the visible side of the canvas is flipped alongside the left edge of the canvas and become invisible. You need to translate the bitmap horizontally to compensate for that.
You can set an additional postTranslate on the transformation matrix. Or you could set the x-coordinate of the bitmap draw operation accordingly.
Matrix flipmatrix = new Matrix();
flipmatrix.setScale(-1, 1);
flipmatrix.postTranslate(-canvas.getWidth(), 0);
I haven't tried that myself and I'm sometimes also a bad with affine matrix calculations. I'm not sure about the negative sign of canvas.getWidth() so you probably need to play around a bit with that.
You also probably should try a plain bitmap draw operation without source and target rect but just coordinates in order to see if that works for you.
On Saturday, August 10, 2013 11:26:24 AM UTC-5, decode wrote:
I have my code as below :
canvas.drawBitmap(bitmap, src, dest,p); (p is null. using default paint, src is null aswell)
src and dest are Rect
I am trying to flip the bitmap horizontally by using canvas.scale as below
canvas.save();
canvas.scale(-1.0f, 1.0f);
canvas.drawBitmap(bitmap, src, dest, p);
The image just diappeared.
I am trying to use matrices. But I feel there should be some easier way around.
I want to flip the image and draw in the rect dest(positon and scaling)
Code I am trying now
Matrix flipmatrix = new Matrix();
flipmatrix .setScale(-1, 1);
flipmatrix .postScale(calcScaleX, calcScaleY);
flipmatrix .postTranslate(calcPosX,calcPosY);
c.drawBitmap(bitmap, flipmatrix , null);
Can anyone help me with this. I am not good with matrices. I think there is a easier way out
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home