Android的位图缓存 [英] Android bitmap caching
问题描述
我有一个应用程序,它运行的位图的大量(〜100) - 即音乐封面。位图采用两种方式 - 作为一个大背景和小(50dip)图标。 这是否有意义preLOAD和缓存两种规格作为单独的位图? 我实现了这两种方法(使用大图作为图标|缓存这两个大小),但我看不出实际的性能差异。 什么是这种情况的最佳做法?
I have an app which operates a large quantity (~100) of bitmaps - i.e. music cover art. Bitmaps are used in two ways - as a large background and a small (50dip) icon. Does it make sense to preload and cache two sizes as separate bitmaps? I've implemented both approaches (use large bitmap as the icon | cache both sizes), but I can't see actual performance difference. What is the best practice for such situation?
推荐答案
有没有在这两个缓存图像大小没有意义的,它需要太多的记忆。
There's no sense in caching both image sizes, it takes too much memory.
最好的做法是(以我愚见):
Best practices would be (to my humble opinion):
- 确保你的缓存使用 SoftReferences ,这种方式可以确保你不耗尽内存,并且可以随时加载新位图上失去旧的代价。
- 使用Canvas的drawBitmap方法绘制大型位图小。
- 确保您防范的OutOfMemoryError ,并注意它是Throwable的子类,没有一个子类异常的情况,所以
赶上(例外五)
子句不会赶上它。
- Make sure your cache uses SoftReferences, this way you can make sure that you don't run out of memory, and can always load new bitmaps on the "expense" of losing old ones.
- Use the Canvas' drawBitmap methods to draw the large-scale bitmaps smaller.
- Make sure you guard against OutOfMemoryError, and notice that it's a subclass of Throwable, and not a subclass of Exception, so a
catch(Exception e)
clause will not catch it.
这篇关于Android的位图缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!