Android 可绘制图像缓存

Android Drawable Image Caching

如果我的应用程序显示的是 ListView,而 ListView 的 ListViewItem 显示的信息很少,包括图像,这些图像是否已缓存?

换句话说,如果我的 ListView 显示 1000 个项目并且每个项目在如下布局中有一个图像并且该图像位于我的 Resources/drawable 中,图像是否会被加载 1000 次(并因此消耗 1000 x image_size 的内存) 还是将对所有 1000 张图像仅缓存和加载一次(因此仅消耗 1 x image_size内存)?

+--------------------------------------------------+
|           |             TEXT                     |             
|   IMAGE   |--------------------------------------|
|           |             TEXT          |  TEXT    |
+--------------------------------------------------+

If my application is showing a ListView and the ListView has ListViewItem that shows few pieces of info including images, are these images cached?

每次加载图像并在项目中显示它(项目应该是可见的)时,它会消耗 1 x image_size 内存,当你在屏幕上显示 10 个项目时,它会消耗 10 x image_size 的内存,但是当你有 1000 个项目要显示时,那就是另一种情况了。如果你只是显示这些图像而不使用任何 ImageLoader 逻辑,它会占用大量内存,结果会导致 OOM.

您应该编写自己的 ImageLoader 逻辑,或者只使用已存在一段时间且几乎已完善此功能的库。例如你可以使用 Picasso :

Many common pitfalls of image loading on Android are handled automatically by Picasso:

  • Handling ImageView recycling and download cancelation in an adapter.

  • Complex image transformations with minimal memory use.

  • Automatic memory and disk caching.

由于它是一个 java 库,您可以 Binding Library 在 Xamarin 中使用它。