How to mirror an image file? (2.2+)

后端 未结 2 1069
花落未央
花落未央 2020-12-11 02:34

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 mir

相关标签:
2条回答
  • 2020-12-11 03:23

    You can simply use View.setScaleX()

    For example

     public void mirrorView(View v){
        v.setScaleX(-1.0f);
    }
    
    0 讨论(0)
  • 2020-12-11 03:31

    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();
    
    0 讨论(0)
提交回复
热议问题