我正在尝试将位图的大小调整为自定义X和Y,而我使用的代码有时会运行到 OutOfMemory 异常中。我环顾四周,找到了一些如何使用 InputStream 来调整位图大小的解决方案,但是我无法找到任何方法如何将其大小调整为自定义X和Y维度。有人可以给我一个建议吗?
这是我的代码:
try {
Point scr = Sys.screenSize(getActivity());
// MY CUSTOM X and Y
int newWidth = (int) Sys.convertDpToPixel(linheight, getActivity())
* lines;
int newHeight = scr.x;
Bitmap bitmap = BitmapFactory.decodeResource(getActivity()
.getResources(), R.drawable.field);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
bm = Bitmap
.createBitmap(bitmap, 0, 0, width, height, matrix, false);
bitmap.recycle();
matrix = null;
iv.setImageBitmap(bm);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
答案 0 :(得分:1)
我认为你想要的是createScaledBitmap:
Bitmap resizedBitmap =
Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);