Flip Bitmap over X or Y Axis, depends on direction - Android android.graphics

Android examples for android.graphics:Bitmap Operation

Description

Flip Bitmap over X or Y Axis, depends on direction

Demo Code

import android.graphics.Bitmap;
import android.graphics.Matrix;

public class Main {

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

  /**//www .j  a v  a 2 s. c om
   * Flip Bitmap over X or Y Axis, depends on direction
   *
   * @param src
   *          source bitmap
   * @param type
   *          flip direction Horizontal and Vertical
   * @return
   */
  public static Bitmap flip(Bitmap src, int type) {
    Matrix matrix = new Matrix();
    // if vertical
    if (type == FLIP_VERTICAL) {
      matrix.preScale(1.0f, -1.0f);
    }
    else if (type == FLIP_HORIZONTAL) {
      matrix.preScale(-1.0f, 1.0f);
    } else {
      return null;
    }

    return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
  }

}

Related Tutorials