通过使用此代码,我们可以旋转图像:
public static Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
但是我们怎样才能水平或垂直翻转图像呢?
最佳答案
假设 cx,cy
是图像的中心:
翻转x:
matrix.postScale(-1, 1, cx, cy);
翻转y:
matrix.postScale(1, -1, cx, cy);
总共:
public static Bitmap createFlippedBitmap(Bitmap source, boolean xFlip, boolean yFlip) {
Matrix matrix = new Matrix();
matrix.postScale(xFlip ? -1 : 1, yFlip ? -1 : 1, source.getWidth() / 2f, source.getHeight() / 2f);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
关于java - 水平或垂直翻转位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36493977/