Android How to - Flip a Bitmap








Question

We would like to know how to flip a Bitmap.

Answer

/*  w ww  .  j a  v  a 2 s.co m*/
import android.graphics.Bitmap;
import android.graphics.Matrix;

public class Main {

  public static final int FLIP_HORIZONTAL = 0;
  public static final int FLIP_VERTICAL = 1;

  public static Bitmap flipBitmap(Bitmap image, int flipType) {
    Matrix matrix = new Matrix();

    if (flipType == FLIP_HORIZONTAL) {
      matrix.preScale(-1, 1);
    } else if (flipType == FLIP_VERTICAL) {
      matrix.preScale(1, -1);
    } else {
      return image;
    }

    return Bitmap.createBitmap(image, 0, 0, image.getWidth(),
        image.getHeight(), matrix, false);
  }
}