Click here to Skip to main content
Click here to Skip to main content

Bitmap combination

, 27 Aug 2013
Rate this:
Please Sign up or sign in to vote.
Create a new bitmap by combining many bitmaps.

Introduction

Sometimes we must create a new bitmap by combining many bitmaps. This article will explain about the ways to do that using Canvas.

Using the code

Suppose that we have two bitmaps left, right, and left is bigger than right. To create new bitmap which is combined left and right horizontally, code will as below:

private void horizontalCombine() {
    int width = left.getWidth() + right.getWidth();
    int height = Math.max(left.getHeight(), right.getHeight());
    Bitmap leftRight = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(leftRight);
    canvas.drawBitmap(left, 0, 0, new Paint());
    canvas.drawBitmap(right, left.getWidth(), 0, new Paint());
    imageView.setImageBitmap(leftRight);
} 

I will explain more about code above.

int width = left.getWidth() + right.getWidth();
int height = Math.max(left.getHeight(), right.getHeight());
Bitmap leftRight = Bitmap.createBitmap(width, height, Config.ARGB_8888); 

This will create new empty bitmap leftRight which has:

  • width = left.getWidth() + right.getWidth()
  • height = left.getHeight()(because left bitmap is taller than)
 Canvas canvas = new Canvas(leftRight);

canvas.drawBitmap(left, 0, 0, new Paint());  

canvas.drawBitmap(right, left.getWidth(), 0, new Paint()); 

And this is the result in device.

With above explanation, I think you can create bitmap by combine many bitmaps vertically as below.

private void verticalCombine() {
int width = Math.max(left.getWidth(), right.getWidth());
    int height = left.getHeight() + right.getHeight();
    Bitmap leftRight = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(leftRight);
    canvas.drawBitmap(left, 0, 0, new Paint());
    canvas.drawBitmap(right, 0, left.getHeight(), new Paint());
    imageView.setImageBitmap(leftRight);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

huyletran

Vietnam Vietnam
I'm Java developer.
I started Java web coding from 2007 to 2010. After that, I began to develop Android from 2010 to now.
In my thinking, sharing and connecting is the best way to develop.
I would like to study more via yours comments and would like to have relationship with more developers in the world.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 27 Aug 2013
Article Copyright 2013 by huyletran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid