尝试仅显示位图的缩放子集并尽可能少地解码。我管理了类似的事情。这些是对我帮助很大的代码 sn-ps:
要计算您需要的所有值(例如比例因子、像素数等),您可以使用 inJustDecodeBounds 来获取位图的大小,而无需分配任何内存。:
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opt);
int width = opt.outWidth;
int height = opt.outHeight;
要仅解码位图的子集,请使用:
Bitmap.createBitmap(
source,
xCoordinateOfFirstPixel,
yCoordinateOfFirstPixel,
xNumberOfPixels,
yNumberOfPixels
);
创建缩放位图:
Bitmap.createScaledBitmap(
source,
dstWidth,
dstHeight,
filter
);
绘制位图的子集:
Canvas.drawBitmap(
source,
new Rect(
subsetLeft,
subsetTop,
subsetRight,
subsetBottom
),
new Rect(0,0,dstWidth, dstHeight),
paint
);
编辑:
我忘了提到这个用于创建缩放图像的片段。为了节省内存,这就是你想要的:
BitmapFactory.Options opt = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap scaledBitmap = BitmapFactory.decodeFile(path, opt);
因为 inSampleSize 必须是整数,所以我使用 createScaledBitmap 来稍微调整位图。