参考:https://muyangmin.github.io/glide-docs-cn/doc/configuration.html#应用程序
MemorySizeCalculator 配置
MemorySizeCalculator(MemorySizeCalculator.Builder builder) {
this.context = builder.context;
// arrayPoolSize = 低设备是2m,高设备是4m
arrayPoolSize =
isLowMemoryDevice(builder.activityManager)
? builder.arrayPoolSizeBytes / LOW_MEMORY_BYTE_ARRAY_POOL_DIVISOR
: builder.arrayPoolSizeBytes;
// maxSize 低设备 是总内存* 0.33,高设备是总内存*0.4
int maxSize =
getMaxSize(
builder.activityManager, builder.maxSizeMultiplier, builder.lowMemoryMaxSizeMultiplier);
int widthPixels = builder.screenDimensions.getWidthPixels();
int heightPixels = builder.screenDimensions.getHeightPixels();
int screenSize = widthPixels * heightPixels * BYTES_PER_ARGB_8888_PIXEL; // 1屏幕的大小
// bitmapPoolSize 大小 = android8之前是4个屏幕,8之后是一个屏幕,8之后用了硬件,缓存小一点
int targetBitmapPoolSize = Math.round(screenSize * builder.bitmapPoolScreens);
// 缓存大小 2个屏幕
int targetMemoryCacheSize = Math.round(screenSize * builder.memoryCacheScreens);
int availableSize = maxSize - arrayPoolSize;
if (targetMemoryCacheSize + targetBitmapPoolSize <= availableSize) {
memoryCacheSize = targetMemoryCacheSize;
bitmapPoolSize = targetBitmapPoolSize;
} else {
float part = availableSize / (builder.bitmapPoolScreens + builder.memoryCacheScreens);
memoryCacheSize = Math.round(part * builder.memoryCacheScreens);
bitmapPoolSize = Math.round(part * builder.bitmapPoolScreens);
}
低内存设备的判断
如果这是低RAM设备,则返回true。设备是否内存不足
最终取决于设备配置,但目前它通常意味着
内存小于或等于1GB的东西。这主要用于应用程序
以确定是否应该关闭某些需要更多RAM的功能
@TargetApi(Build.VERSION_CODES.KITKAT)
@Synthetic
static boolean isLowMemoryDevice(ActivityManager activityManager) {
// Explicitly check with an if statement, on some devices both parts of boolean expressions
// can be evaluated even if we'd normally expect a short circuit.
//noinspection SimplifiableIfStatement
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return activityManager.isLowRamDevice();
} else {
return true; // 4.4一下都是低端机
}
}
@GlideModule
public class YourAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
.setMemoryCacheScreens(2)
.build();
builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));
}
}
磁盘缓存
/** 250 MB of cache. */
int DEFAULT_DISK_CACHE_SIZE = 250 * 1024 * 1024;
String DEFAULT_DISK_CACHE_DIR = "image_manager_disk_cache";
InternalCacheDiskCacheFactory :
DownsampleStrategy 图片的裁剪等