combine Bitmaps by column and row - Android Graphics

Android examples for Graphics:Bitmap Paint

Description

combine Bitmaps by column and row

Demo Code


import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.PointF;

public class Main {
  public static Bitmap combineBitmaps(int columns, Bitmap... bitmaps) {
    if (columns <= 0 || bitmaps == null || bitmaps.length == 0) {
      throw new IllegalArgumentException("Wrong parameters: columns must > 0 and bitmaps.length must > 0.");
    }//from   w w w . j a  v a  2  s.c o  m
    int maxWidthPerImage = 40;
    int maxHeightPerImage = 40;

    int rows = 0;

    rows = 2;
    Bitmap newBitmap = Bitmap.createBitmap(columns * maxWidthPerImage, rows * maxHeightPerImage, Config.RGB_565);

    for (int x = 0; x < rows; x++) {
      for (int y = 0; y < columns; y++) {
        int index = x * columns + y;
        if (index >= bitmaps.length)
          break;
        newBitmap = mixtureBitmap(newBitmap, bitmaps[index], new PointF(y * maxWidthPerImage, x * maxHeightPerImage));
      }
    }
    return newBitmap;
  }

  /**
   * Mix two Bitmap as one.
   * 
   * @param first
   * @param second
   * @param fromPoint
   *          where the second bitmap is painted.
   * @return
   */
  public static Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) {
    if (first == null || second == null || fromPoint == null) {
      return null;
    }
    Bitmap newBitmap = Bitmap.createBitmap(first.getWidth(), first.getHeight(), Config.ARGB_4444);
    Canvas cv = new Canvas(newBitmap);
    cv.drawBitmap(first, 0, 0, null);
    cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);
    cv.save();
    cv.restore();
    return newBitmap;
  }
}

Related Tutorials