How to rotate an image sideways or upside down?

I have a custom view and I use onDraw () to paint on my canvas. I draw an image on this canvas.

I want to flip the image upside down, like flip on a horizontal line as a link. This is not the same as rotating the image 180 degrees or -180 degrees.

Similarly, I want to flip or flip sidways ie with a vertical line as it rotates or links. Again, this is not the same as canvas.rotate () provides.

I am wondering how to do this. Should I use a matrix or does the canvas provide any way to do this the same as a "rotation".

Thanks.

+6
source share
1 answer

You cannot do this directly with Canvas. You will need to actually modify the bitmap (using the matrix) before drawing it. Fortunately, this is a very simple code for this:

public enum Direction { VERTICAL, HORIZONTAL }; /** Creates a new bitmap by flipping the specified bitmap vertically or horizontally. @param src Bitmap to flip @param type Flip direction (horizontal or vertical) @return New bitmap created by flipping the given one vertically or horizontally as specified by the <code>type</code> parameter or the original bitmap if an unknown type is specified. **/ public static Bitmap flip(Bitmap src, Direction type) { Matrix matrix = new Matrix(); if(type == Direction.VERTICAL) { matrix.preScale(1.0f, -1.0f); } else if(type == Direction.HORIZONTAL) { matrix.preScale(-1.0f, 1.0f); } else { return src; } return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); } 
+23
source

Source: https://habr.com/ru/post/921114/


All Articles