Android 位图缓存

发布于 2024-10-17 11:43:44 字数 151 浏览 4 评论 0原文

我有一个运行大量(~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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2024-10-24 11:43:44

缓存两种图像大小是没有意义的,它占用太多内存。

最佳实践是(以我的拙见):

  1. 确保您的缓存使用 SoftReferences ,这样您就可以确保不会耗尽内存,并且始终可以加载新位图,而代价是丢失旧位图。
  2. 使用Canvas 的drawBitmap 方法将大尺寸位图绘制得更小。
  3. 确保防范 OutOfMemoryError,并注意它是 Throwable 的子类,而不是 Exception 的子类,因此 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):

  1. 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.
  2. Use the Canvas' drawBitmap methods to draw the large-scale bitmaps smaller.
  3. 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.
奢望 2024-10-24 11:43:44

就我个人而言,我会尽力限制内存中保留的位图数量。如果图标作为较大位图的缩小副本看起来不错,我会喜欢这种方法。由于我的个人经验,这可能是一个有偏见的观点,但我在 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

热情消退 2024-10-24 11:43:44

不得保存两个文件,保存较大的文件。

尝试着致力于内存缓存的内存管理。

  1. 内存不足异常发生之前,使用recycle()刷新内存缓存。

在内部,此 recycle() 工作在引用计数机制上,该机制检查:

它使用引用计数(在变量 mDisplayRefCountmCacheRefCount 中)来跟踪位图当前是否正在显示或在缓存中。当满足以下条件时,代码将回收位图:

  • mDisplayRefCount 和 mCacheRefCount 的引用计数均为 0。
  • 位图不为 null,尚未被回收。

请参阅此处的实现

  1. 您可以使用 BitmapFactory.Options 类对象中的 inBitmap 字段来保留位图以供以后使用。

其中,位图从 LruCache 中逐出,对位图的软引用被放置在 HashSet 中,以便稍后与 inBitmap 重用:

Set<SoftReference<Bitmap>> mReusableBitmaps;

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.

  1. Use recycle() to flush out the memory cache before out of memory exception occurs.

Internally this recycle() work on reference count mechanism which checks:

It uses reference counting (in the variables mDisplayRefCount and mCacheRefCount) to track whether a bitmap is currently being displayed or in the cache. The code recycles the bitmap when these conditions are met:

  • The reference count for both mDisplayRefCount and mCacheRefCount is 0.
  • The bitmap is not null, and it hasn't been recycled yet.

Refer Implementation here

  1. You may use inBitmap field in BitmapFactory.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:

Set<SoftReference<Bitmap>> mReusableBitmaps;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文