Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...