如何将图像横盘或倒置? [英] how to turn an image sideways or upside-down?
问题描述
我有一个自定义视图和我使用的onDraw()来绘制到我的画布。我这个画布上绘制图像。
I have a custom view and i am using onDraw() to draw onto my canvas. I am drawing an image on this canvas.
我想打开图像上下颠倒有点像翻盖上的水平线作为参考。这是不一样的180度或-180度的旋转图像。
I want to turn the image upside down kinda like flip on a horizontal line as a reference. This is not the same as rotating the image by 180 deg or -180 deg.
同样,我希望镜像或以垂直线翻转sidways即因为它的支点或引用。再次,这是不一样的canvas.rotate()提供
Likewise, i want to mirror or flip sidways i.e with a vertical line as it's pivot or reference. Again this is not the same as the canvas.rotate() provides.
我想知道如何做到这一点。我应该用一个矩阵还是帆布提供任何方法来那样做的旋转。
I am wondering how to do it. Should i use a matrix or does canvas provide any method to do it like that of a "rotate".
感谢。
推荐答案
您不能直接帆布做的。你需要绘制它之前实际修改的位图(使用矩阵)。幸运的是,这是一个非常简单的code要做到这一点:
You cannot do it with Canvas directly. You will need to actually modify the Bitmap (using Matrix) before drawing it. Luckily, it's a very simple code to do 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);
}
这篇关于如何将图像横盘或倒置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!