此方法解码位图时不会产生OutOfMemory
异常:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
{
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inInputShareable = true;
options.inPurgeable = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
但是字段inInputShareable
和inPurgeable
在 sdk=21 中已弃用。
如何更改上述方法以使用BitmapFactory.Options.inBitmap
?
它会在 21 岁之前的设备上正确运行吗?
如何更改上述方法以使用 BitmapFactory.Options.inBitmap?
设置options.inBitmap
为Bitmap
您想要重用作为解码目标的现有对象。
它会在 21 岁之前的设备上正确运行吗?
这将完全取决于您提供的内容Bitmap
以及您如何“正确”定义:
在 API 级别 19+ 上,Bitmap
需要与Bitmap
您尝试解码的大小相同或更大。否则,Bitmap
将被忽略,您将获得一个新的Bitmap
.
在较旧的设备上,Bitmap
需要与Bitmap
您尝试解码的大小(分辨率)相同。否则,Bitmap
将被忽略,您将获得一个新的Bitmap
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句