Art*_*ine 2 memory performance android drawable
我遇到了问题.
我动态地将9加载drawables到9 imageViews.(drawables各自不同的时间)的可绘制的ID被存储在该对象类,所以当我加载此抽拉我设置ImageRessource到imageView通过使用imageView.setImageRessource(myObject.getRessourceId());
一切正常,但9绘项目被装载时,我看到在Android内存监视器,所述分配内存达到80MB,我认为这不正常......(是吗?)
我尝试了不同的东西来解决它:
BitmapFactory.decodeResssource在imageView上创建一个Bitmap,然后设置setImageBitmap.使用我尝试的所有技术,它需要80MB的分配内存.
我尝试使用不同的图像分辨率,所以在ldpi(~30Ko /图像)和xhdpi(~87Ko /图像),但它不会改变任何加载的每个图像,它需要大约5MB的分配内存...
所以我的问题是:如何减少为图像分配的内存?
提前谢谢,如果有必要,我可以提供部分代码.
问候
PS:ImageViews在onCreate()方法中动态创建.
感谢Bojan Kseneman的链接,我将分配的内存减少到30Mb.我正在使用这个:
imageView.setImageBitmap(Util.decodeSampledBitmapFromResource(getResources(), id, 150, 150));
Run Code Online (Sandbox Code Playgroud)
Util是我项目的Utility类
用这些方法:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
Run Code Online (Sandbox Code Playgroud)
和
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Run Code Online (Sandbox Code Playgroud)