Flips the pixels in a Bitmap to be mirrored vertically - Android android.graphics

Android examples for android.graphics:Canvas

Description

Flips the pixels in a Bitmap to be mirrored vertically

Demo Code

import android.graphics.Bitmap;

public class Main {

  /**//from w w  w  .  ja  v a  2s  .c om
   * Flips the pixels in a Bitmap to be mirrored vertically. This is needed for
   * OpenGL to draw the texture properly as loads texture bottom up.
   * 
   * @return a new Bitmap that is identical to the argument but flipped.
   * @param bitmap
   *          The Bitmap to flip the pixels of
   */
  static public Bitmap flipBitmap(Bitmap bitmap) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    int[] pixels = new int[w * h];
    bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        pixels[y * w + x] = bitmap.getPixel(x, h - 1 - y);
      }
    }
    bitmap = Bitmap.createBitmap(pixels, w, h, Bitmap.Config.ARGB_8888);
    return bitmap;
  }

}

Related Tutorials