我想将两张图片合并为一张。 (例如“street.png”之上的“House.png”)
我如何在Android中实现这一目标?我只是想合并图像并将它们导出到文件中。
This example将图像设置为ImageView,但我希望将其导出。
This other example在Android中不起作用,因为这些类不可用。
答案 0 :(得分:3)
我会尝试类似的事情:
public static Bitmap mergeImages(Bitmap bottomImage, Bitmap topImage) {
final Bitmap output = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage
.getHeight(), Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawBitmap(bottomImage, 0, 0, paint);
canvas.drawBitmap(topImage, 0, 0, paint);
return output;
}
(未经测试,我只是在这里写的,可能是一些简单的错误)
基本上你要做的是创建第3个空位图,在其上绘制底部图像,然后在其上绘制顶部图像。
至于保存到文件,以下是一些示例:Save bitmap to location
答案 1 :(得分:1)
你可以这样做...............
public Bitmap Overlay(Bitmap Bitmap1, Resources paramResources, Bitmap Bitmap2, int alpha)
{
Bitmap bmp1 = Bitmap.createScaledBitmap(Bitmap2, Bitmap1.getWidth(), Bitmap1.getHeight(), true);
Bitmap bmp2 = Bitmap.createBitmap(Bitmap1.getWidth(), Bitmap1.getHeight(), Bitmap1.getConfig());
Paint localPaint = new Paint();
localPaint.setAlpha(alpha);
Canvas localCanvas = new Canvas(bmp2);
Matrix localMatrix = new Matrix();
localCanvas.drawBitmap(Bitmap1, localMatrix, null);
localCanvas.drawBitmap(bmp1, localMatrix, localPaint);
bmp1.recycle();
System.gc();
return bmp2;
}