我有兩個圖像,我想合併成一個。 (例如,在「street.png」之上的「House.png」)在Android中覆蓋圖像
我如何在Android中實現此目的?我只想合併圖像並將它們導出到文件。
This example將圖像設置爲ImageView,但我希望將其導出。
This other example在Android中不起作用,因爲類不可用。
我有兩個圖像,我想合併成一個。 (例如,在「street.png」之上的「House.png」)在Android中覆蓋圖像
我如何在Android中實現此目的?我只想合併圖像並將它們導出到文件。
This example將圖像設置爲ImageView,但我希望將其導出。
This other example在Android中不起作用,因爲類不可用。
我想嘗試這樣的:
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
你可以這樣做...............
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;
}