我有一个自定义视图,我使用onDraw()绘制到我的画布上.我正在画布上画一个图像.
我想将图像颠倒,有点像在水平线上翻转作为参考.这与将图像旋转180度或-180度不同.
同样,我想镜像或翻转sidways,即垂直线作为它的枢轴或参考.同样,这与canvas.rotate()提供的不同.
我想知道该怎么做.我应该使用矩阵还是画布提供任何方法来执行它像“旋转”.
谢谢.
解决方法
你无法直接使用Canvas.在绘制之前,您需要实际修改位图(使用Matrix).幸运的是,这是一个非常简单的代码:
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,src.getWidth(),src.getHeight(),matrix,true);
}