Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mirror an image file? (2.2+)

I have a PNG file that I want to use for an overlay - however, this file has to be mirrored (and rotated by 180°), but in order to save space, I don't want to place the mirrored file in the apk, but do this action programmatically.

How can I do this with Froyo and above?

like image 796
Force Avatar asked Dec 18 '11 14:12

Force


People also ask

How do I make a mirror image of PNG?

Upload the image that you want to flip vertically or horizontally. You can choose JPG, PNG, GIF, or a variety of other file formats. Select 'Mirror' or 'Rotate' to flip your image or video across the axis. Use the plus and minus buttons to adjust the angle of your flipped image precisely.

Can I mirror an image in Word?

Flip an object Click the object that you want to rotate. Under Drawing Tools (or Picture Tools if you're rotating a picture), on the Format tab, in the Arrange group, click Rotate, and then: To turn an object upside-down, click Flip Vertical. To create a mirror image of the object, click Flip Horizontal.


1 Answers

Scaling by -1.0 causes the image to be flipped. Assuming bmp is the bitmap you want to mirror (here on the x axis) you can do :

Matrix matrix = new Matrix(); 
matrix.preScale(-1.0f, 1.0f); 
Bitmap mirroredBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.width(), bmp.height(), matrix, false);

If you don't want to create a second bitmap, you can do the same with canvas.scale :

canvas.save();
canvas.scale(-1.0f, 1.0f);
canvas.drawBitmap(bitmap, ...); // The bitmap is flipped
canvas.restore();
like image 162
Dalmas Avatar answered Oct 17 '22 02:10

Dalmas