Android 上用于缓存图像的最佳 BitmapLRU图像缓存大小是多少?我的目标是 sdk 2.3.5+ 基本上 10+ 设备。目前我正在采取这两个计算中的较大数量,如下所示。1/8 是避免内存不足错误的安全百分比吗?
这是我现在的计算方式:
private static int calculateImageCacheSize(Context ctx)
{
// Gets the dimensions of the device's screen
DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
// Assuming an ARGB_8888 pixel format, 4 bytes per pixel
int size = screenWidth * screenHeight * 4;
// 3 bitmaps to store therefore multiply bitmap size by 3
int cacheSize = size * 3;
int cachePercentOfMemory = getPercentageOfTotalMemory(8);
int retCacheSize = cacheSize;
if (retCacheSize < cachePercentOfMemory)
retCacheSize = cachePercentOfMemory;
return retCacheSize;
}
public static int getPercentageOfTotalMemory(int divider)
{
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / divider;
return cacheSize;
}