答案 0 :(得分:1)
首先,以您希望最终图像的大小创建Bitmap
:
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
然后创建一个Canvas
以从Bitmap
绘制到,并绘制背景颜色:
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
然后将原始图像绘制到画布上:
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
int top = (original.getHeight() - bitmap.getHeight()) / 2;
int left = (original.getWidth() - bitmap.getWidth()) / 2;
canvas.drawBitmap(original, top, left, paint);
最后,按照Saving Files培训将此Bitmap
保存到File
。您可以使用bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
代替outputStream.write()
来存储Bitmap
。