user1398593
user1398593

Reputation: 63

Android combine pictures

Hy, I tried to search this but not very much luck the most similar is this one http://ketankantilal.blogspot.com/2011/03/how-to-combine-images-and-store-to.html Anyhow, I'm developing for android. The question is I have images in png format (or jpg as bmp is quite large for my app). How can I combine three images from top to bottom. I do not need to save them on sd just to display them. Thanks, and sorry if a similar question with answer exists.

Upvotes: 0

Views: 1103

Answers (1)

soost
soost

Reputation: 91

You could use a Canvas and then draw each Bitmap (assuming each image is loaded into a Bitmap object) using appropriate top and left offsets.

You would increase the top offset of the next bitmap by the total size of the previously drawn Bitmaps.

Check out http://developer.android.com/reference/android/graphics/Canvas.html

example:

public void stackImages(Context ctx)
{
    // base image, if new images have transparency or don't fill all pixels
    // whatever is drawn here will show.
    Bitmap result = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);

    // b1 will be on top
    Bitmap b1 = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);

    // b2 will be below b1
    Bitmap b2 = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(result);

    c.drawBitmap(b1, 0f, 0f, null);

    // notice the top offset
    c.drawBitmap(b2, 0f, 200f, null);

    // result can now be used in any ImageView
    ImageView iv = new ImageView(ctx);
    iv.setImageBitmap(result);

    // or save to file as png
    // note: this may not be the best way to accomplish the save
    try {
           FileOutputStream out = new FileOutputStream(new File("some/file/name.png"));
           result.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (Exception e) {
           e.printStackTrace();
    }
}

Upvotes: 2

Related Questions