java - 水平或垂直翻转位图图像

标签 java android image matrix android-bitmap

通过使用此代码,我们可以旋转图像:

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/

相关文章:

分类前图像居中?

ios - 了解iOS中的图片大小

java - 在 Android 中翻译短信正文

java - 如何在配置单元中获取给定时区的当前时间

java - Android 中的 Parcelable 和继承

android - 在android中获取日期信息

html - 相对于图像定位 block

java - Eclipse [桌面 headless ]

java - java中的多线程使用3个线程打印abc

android - Android Bottom Sheet 上的静态页脚 View