我正在处理一个应用程序,我有多个图像,我想从中创建一个图像。我使用下面的代码,但单个图像的输出只是垂直格式。
String sdPath = Environment.getExternalStorageDirectory().getPath()
+ "/MERGE/";
BitmapFactory.Options options;
Bitmap bitmap;
ArrayList<Bitmap> myBitmapList = new ArrayList<Bitmap>();
int vWidth = 0;
int vHeight = 0;
for (int i = 1; i <= 40; i++) {
try {
options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(sdPath + "img" + i + ".jpg",
options);
vWidth = bitmap.getWidth() > vWidth ? bitmap.getWidth()
: vWidth;
vHeight += bitmap.getHeight();
myBitmapList.add(bitmap);
} catch (Exception e) {
e.printStackTrace();
} finally {
options = null;
bitmap = null;
System.gc();
Runtime.getRuntime().totalMemory();
Runtime.getRuntime().freeMemory();
}
}
Bitmap vTargetBitmap = Bitmap.createBitmap(vWidth, vHeight,
Bitmap.Config.ARGB_8888);
Canvas vCanvas = new Canvas(vTargetBitmap);
int vInsertY = 0;
for (int i = 1; i < 40; i++) {
vCanvas.drawBitmap(myBitmapList.get(i), (float) 0f,
(float) vInsertY, null);
vInsertY += myBitmapList.get(i).getHeight();
}
String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
OutputStream os = null;
try {
os = new FileOutputStream(Environment.getExternalStorageDirectory()
.getPath() + "/" + tmpImg);
vTargetBitmap.compress(CompressFormat.PNG, 50, os);
} catch (IOException e) {
Log.e("combineImages", "problem combining images", e);
}
Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
我想要的实际输出如下图所示。
答案 0 :(得分:1)
private Bitmap mergeMultiple(ArrayList<Bitmap> parts) {
Bitmap result = Bitmap.createBitmap(parts.get(0).getWidth() * 2, parts.get(0).getHeight() * 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
for (int i = 0; i < parts.size(); i++) {
canvas.drawBitmap(parts.get(i), parts.get(i).getWidth() * (i % 2), parts.get(i).getHeight() * (i / 2), paint);
}
return result;
}