Android 位图缓存
我有一个运行大量(~100)位图的应用程序 - 即音乐封面艺术。位图有两种使用方式 - 作为大背景和小(50dip)图标。 将两种尺寸预加载并缓存为单独的位图是否有意义? 我已经实现了这两种方法(使用大位图作为图标|缓存两种尺寸),但我看不到实际的性能差异。 这种情况的最佳做法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
缓存两种图像大小是没有意义的,它占用太多内存。
最佳实践是(以我的拙见):
catch(Exception e)
子句不会捕获它。There's no sense in caching both image sizes, it takes too much memory.
Best practices would be (to my humble opinion):
catch(Exception e)
clause will not catch it.就我个人而言,我会尽力限制内存中保留的位图数量。如果图标作为较大位图的缩小副本看起来不错,我会喜欢这种方法。由于我的个人经验,这可能是一个有偏见的观点,但我在 Android 方面遇到的最大问题是在使用位图时内存不足
Personally, I would do my best to limit the number of Bitmaps I'm keeping around in memory. If the icon looks fine as a scaled down copy of the larger Bitmap, I'd favor that approach. This is probably a biased opinion because of my personal experience, but the biggest problems I've had in my experience with Android has been running out of memory when working with Bitmaps
不得保存两个文件,保存较大的文件。
尝试着致力于内存缓存的内存管理。
内存不足异常发生
之前,使用recycle()
刷新内存缓存。在内部,此
recycle()
工作在引用计数机制上,该机制检查:它使用引用计数(在变量
mDisplayRefCount
和mCacheRefCount
中)来跟踪位图当前是否正在显示或在缓存中。当满足以下条件时,代码将回收位图:请参阅此处的实现
BitmapFactory.Options
类对象中的inBitmap
字段来保留位图以供以后使用。其中,位图从 LruCache 中逐出,对位图的软引用被放置在 HashSet 中,以便稍后与 inBitmap 重用:
3. 使用 BitmapFactory.Options 类有效地将大位图加载到小图像视图中。通过计算 inSampleSize 并将其提供在 options.inSampleSize 字段中。
inSampleSize 是决定将大图像降低为小图像视图的级别的因素,通过选项字段中的高度和宽度计算
您可以参考这里实现
Must not save both files, save the larger one.
Try to work on memory management of memory cache.
recycle()
to flush out the memory cache beforeout of memory exception occurs
.Internally this
recycle()
work on reference count mechanism which checks:It uses reference counting (in the variables
mDisplayRefCount
andmCacheRefCount
) to track whether a bitmap is currently being displayed or in the cache. The code recycles the bitmap when these conditions are met:Refer Implementation here
inBitmap
field inBitmapFactory.Options
class object to keep the bitmap for later use.Where, a bitmap is evicted from the LruCache, a soft reference to the bitmap is placed in a HashSet, for possible reuse later with inBitmap:
3. Using BitmapFactory.Options Class load large bitmap in small imageviews efficently. by calculating inSampleSize and providing it in options.inSampleSize field.
inSampleSize is a factor which determines level of lowering a large image into small imageview calculated via height and width in options field
You may refer here for implementation