以下方法是拼接兩個(gè)Bitmap的方法;
但發(fā)現(xiàn)這個(gè)方法生成的位圖背景色為黑色。
原因是 Bitmap.Config.RGB_565;
若要使背景為透明棍厌,必須設(shè)置為Config.ARGB_4444,或者Config.ARGB_8888碍遍, 而不是Bitmap.Config.RGB_565定铜。
/**
* 把兩個(gè)位圖覆蓋合成為一個(gè)位圖,上下拼接
*
* @param topBitmap
* @param bottomBitmap
* @param isBaseMax 是否以高度大的位圖為準(zhǔn)怕敬,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;
}