Detailed explanation of Glide4 efficient loading picture configuration

  • 2021-08-31 08:59:20
  • OfStack

This article introduces the configuration details of Glide4 efficient loading pictures, and shares them with you as follows:

Adding glide dependencies to build. gradle


// glide  Dependency 
compile 'com.github.bumptech.glide:glide:4.6.1'
// glide  Related annotations, generate GlideApp Code 
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
// Glide The network library is configured to okhttp3
compile ('com.github.bumptech.glide:okhttp3-integration:4.6.1') {
  transitive = false
}

Glide Global Configuration Class


package com.leo.demo;

import android.content.Context;
import android.support.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;

/**
 * Created by kangyi on 2018/3/20.
 *
 * Glide Global configuration, using GlideModule Annotations perform automatic code generation, generating GlideApp , follow-up Glide
 *  Calls need to be replaced with GlideApp.with(context).load(url).into(imageView)  The way of 
 *
 */
@GlideModule
public class GlobalGlideConfig extends AppGlideModule {

  @Override
  public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
    super.registerComponents(context, glide, registry);
  }

  @Override
  public boolean isManifestParsingEnabled() {
    return false;
  }

  @Override
  public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
    super.applyOptions(context, builder);

    /**
     * DiskCacheStrategy.NONE :   Indicates that nothing is cached. 
     * DiskCacheStrategy.DATA :   Indicates that only the original picture is cached. 
     * DiskCacheStrategy.RESOURCE :   Indicates that only converted pictures are cached. 
     * DiskCacheStrategy.ALL  :   Indicates that both the original picture and the converted picture are cached. 
     * DiskCacheStrategy.AUTOMATIC :   Indicate that let Glide Intelligently choose which to use according to picture resources 1 A cache policy (default option). 
     */
    builder.setDefaultRequestOptions(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE));

    /**
     *  Prefer external storage as disk cache directory to prevent internal storage files from being too large 
     *  The default address of the external storage directory is: /sdcard/Android/data/com.sina.weibolite/cache/image_manager_disk_cache
     */
    builder.setDiskCache(new ExternalCacheDiskCacheFactory(context));
  }
}

Code parsing

Disk cache policy

缓存常量 说明
DiskCacheStrategy.NONE 表示不缓存任何内容。
DiskCacheStrategy.DAT 表示只缓存原始图片。
DiskCacheStrategy.RESOURCE 表示只缓存转换过后的图片。
DiskCacheStrategy.ALL 表示既缓存原始图片,也缓存转换过后的图片。
DiskCacheStrategy.AUTOMATIC 表示让Glide根据图片资源智能地选择使用哪1种缓存策略(默认选项)。


Related articles: