一、横向(两个)
/**
* 横向拼接
* <功能详细描述>
* @param first
* @param second
* @return
*/
private Bitmap add2Bitmap(Bitmap first, Bitmap second) {
int width = first.getWidth() + second.getWidth();
int height = Math.max(first.getHeight(), second.getHeight());
Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
canvas.drawBitmap(second, first.getWidth(), 0, null);
return result;
}
二、纵向(两个)
/**
* 纵向拼接
* <功能详细描述>
* @param first
* @param second
* @return
*/
private Bitmap addBitmap(Bitmap first, Bitmap second) {
int width = Math.max(first.getWidth(),second.getWidth());
int height = first.getHeight() + second.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
canvas.drawBitmap(second, 0, first.getHeight(),null);
return result;
}
三、纵向(多个)
/**
* 将bitmap集合上下拼接
*
* @return
*/
private Bitmap drawMulti(ArrayList<Bitmap> bitmaps) {
int width = bitmaps.get(0).getWidth();
int height = bitmaps.get(0).getHeight();
for (int i = 1;i<bitmaps.size();i++) {
if (width < bitmaps.get(i).getWidth()) {
width = bitmaps.get(i).getWidth();
}
height = height+bitmaps.get(i).getHeight();
}
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(bitmaps.get(0), 0, 0, paint);
int h = 0;
for (int j = 1;j<bitmaps.size();j++) {
h = bitmaps.get(j).getHeight()+h;
canvas.drawBitmap(bitmaps.get(j), 0,h, paint);
}
return result;
}
本文介绍了如何使用Android平台上的Bitmap类实现图片的横向和纵向拼接。包括两个图片的横向和纵向拼接方法,以及多个图片纵向拼接的方法。通过这些方法可以轻松地将多张图片合并成一张。
2137





