以下方法是拼接两个Bitmap的方法;
但发现这个方法生成的位图背景色为黑色。
原因是 Bitmap.Config.RGB_565;
若要使背景为透明,必须设置为Config.ARGB_4444,或者Config.ARGB_8888, 而不是Bitmap.Config.RGB_565。
/**
* 把两个位图覆盖合成为一个位图,上下拼接
*
* @param topBitmap
* @param bottomBitmap
* @param isBaseMax 是否以高度大的位图为准,true则小图等比拉伸,false则大图等比压缩
* @return
*/
public static Bitmap mergeBitmap_TB(Bitmap topBitmap, Bitmap bottomBitmap, boolean isBaseMax) {
if (topBitmap == null || topBitmap.isRecycled()
|| bottomBitmap == null || bottomBitmap.isRecycled()) {
return null;
}
int width = 0;
if (isBaseMax) {
width = topBitmap.getWidth() > bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
} else {
width = topBitmap.getWidth() < bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
}
Bitmap tempBitmapT = topBitmap;
Bitmap tempBitmapB = bottomBitmap;
if (topBitmap.getWidth() != width) {
tempBitmapT = Bitmap.createScaledBitmap(topBitmap, width, (int) (topBitmap.getHeight() * 1f / topBitmap.getWidth() * width), false);
} else if (bottomBitmap.getWidth() != width) {
tempBitmapB = Bitmap.createScaledBitmap(bottomBitmap, width, (int) (bottomBitmap.getHeight() * 1f / bottomBitmap.getWidth() * width), false);
}
int height = tempBitmapT.getHeight() + tempBitmapB.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Rect topRect = new Rect(0, 0, tempBitmapT.getWidth(), tempBitmapT.getHeight());
Rect bottomRect = new Rect(0, 0, tempBitmapB.getWidth(), tempBitmapB.getHeight());
Rect bottomRectT = new Rect(0, tempBitmapT.getHeight(), width, height);
canvas.drawBitmap(tempBitmapT, topRect, topRect, null);
canvas.drawBitmap(tempBitmapB, bottomRect, bottomRectT, null);
return bitmap;
}