2011-07-25 149 views
1

我有两个图像,我想合并成一个。 (例如,在“street.png”之上的“House.png”)在Android中覆盖图像

我如何在Android中实现此目的?我只想合并图像并将它们导出到文件。

This example将图像设置为ImageView,但我希望将其导出。
This other example在Android中不起作用,因为类不可用。

回答

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; 
} 

(没有测试过,我只是写在这里,可能是一些简单的错误在那里)

基本上你要做的就是创建一个空的第三位,在其上绘制底部图像,然后在其上绘制顶部图像。

至于保存到一个文件,这里有几个例子:Save bitmap to location

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; 
    }