Android将drawable加载到ImageView中会占用大量内存 [英] Android loading drawable into ImageView taking a lot of memory

查看:462
本文介绍了Android将drawable加载到ImageView中会占用大量内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题了.

我正在将9 drawables动态加载到9 imageViews中. (每次drawables都不相同)可绘制对象的ID存储在对象类中,因此在加载此可绘制对象时,我使用imageView.setImageRessource(myObject.getRessourceId());ImageRessource设置为imageView 一切正常,但是当加载9个可绘制对象时,我在Android内存监视器上看到分配的内存达到80MB,我认为这是不正常的……(是吗?)

我尝试了不同的方法来解决它:

  • 使用Picasso库加载图形对象.
  • 使用BitmapFactory.decodeResssource创建位图,然后 在imageView上设置setImageBitmap.

使用我尝试过的所有技术,它将占用80MB的已分配内存.

我尝试使用不同的图像分辨率,因此在ldpi(〜30Ko/image)和xhdpi(〜87Ko/image)中使用它,但是加载的每张图像都不会改变任何内容,大约需要5MB的已分配内存...

所以我的问题是:如何减少那些图像的分配内存?

预先感谢您,如果有必要,我可以提供部分代码.

致谢

PS:ImageViews是通过onCreate()方法动态创建的.

解决方案

这很正常,您记忆中最大的敌人是图像.另请注意,映像占用的内存空间要大于磁盘占用的空间.加载需要很长时间也是正常的.解决方案是仅加载所看到的大图像,并在下一次使用缓存来更快地加载它们(您无需再次下采样).这是一篇带有示例项目的文章,内容仅为该内容: http://developer .android.com/training/displaying-bitmaps/load-bitmap.html

I'm having a problem.

I'm loading 9 drawables into 9 imageViews dynamically. (drawables are different each time) The drawable id is stored in the object class so when I load this drawable I set the ImageRessource to the imageView by using imageView.setImageRessource(myObject.getRessourceId()); Everything is working fine but when the 9 drawables are loaded, I see on the Android Memory Monitor that the allocate memory reaches 80MB and I think this isn't normal... (Is it?)

I tried different things to solve it:

  • Load the drawable with the library Picasso.
  • Use BitmapFactory.decodeResssource to create a Bitmap and then setImageBitmap on the imageView.

With all the techniques I tried, it takes 80MB of allocated memory.

I tried using different image resolution so in ldpi (~30Ko/image) and in xhdpi (~87Ko/image) but it doesn't change anything for each image loaded it takes about 5MB of allocated memory...

So my question is : How can I reduce the allocated memory for thoses images?

Thank you in advance, I can give parts of code if it's necessary.

Regards

PS: The ImageViews are created dynamically in the onCreate() method.

解决方案

This is normal, the biggest enemy of your memory are images. Also note that images take more space in memory than on the disk. It is also normal that it takes long time to load. The solution is to only load as big image as it is seen and using caches to load them faster next time (you don't need to downsample again). Here is an article with a sample project with just that: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

这篇关于Android将drawable加载到ImageView中会占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆