Home » Source Code » Bitmap combination

Bitmap combination

maninwest
2015-02-11 03:19:25
The author
View(s):
Download(s): 0
Point (s): 1 
Category Category:
AndroidAndroid JavaJava

Description

Translated by  maninwest@Codeforge  Author:huyletran@ Codeproject
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(right, left.getWidth(), 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);
}



Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
.classpath475.00 B2013-08-14 14:07
.project857.00 B2013-08-16 10:42
AndroidManifest.xml894.00 B2013-08-14 14:07
BuildConfig.java168.00 B2013-08-16 10:42
R.java2.64 kB2013-08-16 13:42
ic_launcher-web.png50.19 kB2013-08-14 14:07
android-support-v4.jar543.16 kB2013-08-14 14:07
proguard-project.txt781.00 B2013-08-14 14:07
project.properties563.00 B2013-08-14 14:07
ic_launcher.png7.48 kB2013-08-14 14:07
left.png7.49 kB2013-08-16 11:39
right.png3.61 kB2013-08-16 10:49
ic_launcher.png3.69 kB2013-08-14 14:07
ic_launcher.png12.22 kB2013-08-14 14:07
ic_launcher.png24.20 kB2013-08-14 14:07
activity_main.xml693.00 B2013-08-14 14:14
main.xml409.00 B2013-08-16 13:41
dimens.xml203.00 B2013-08-14 14:07
dimens.xml277.00 B2013-08-14 14:07
styles.xml334.00 B2013-08-14 14:07
styles.xml391.00 B2013-08-14 14:07
dimens.xml220.00 B2013-08-14 14:07
strings.xml231.00 B2013-08-14 14:07
styles.xml697.00 B2013-08-14 14:07
MainActivity.java2.40 kB2013-08-16 13:42
...

Comments

(Add your comment, get 0.1 Point)
Minimum:15 words, Maximum:160 words
  • 1
  • Page 1
  • Total 1

Bitmap combination (583.67 kB)

Need 1 Point(s)
Your Point (s)

Your Point isn't enough.

Get 22 Point immediately by PayPal

Point will be added to your account automatically after the transaction.

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more Points

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D