SoFunction
Updated on 2025-03-11

Android Glide 4.0+ usage details

Download and Setup

Android SDK requirements

Minimum usage requirements - Use Glide requires the SDK version to be API 14 (Ice Cream Sandwich) and above.

Minimum Compilation Requirements - Compiling Glide must be API 26 (Oreo) and above in the SDK version.

jar

You can directlyGitHub Download the latest jar package

Gradle

If you are using Gradle, you can add dependencies on Glide from Maven Central or JCenter. Similarly, you also need to add dependencies for the Android support library.

repositories {
 mavenCentral()
 maven { url '' }
}
dependencies {
  compile ':glide:4.1.1'
  annotationProcessor ':compiler:4.1.1'
}

Kotlin

If you use Glide annotation in a class written by Kotlin, you need to introduce a kapt dependency instead of the regular annotationProcessor dependency:

dependencies {
 kapt ':compiler:4.1.1'
}

Get started

Basic usage

Loading pictures

(fragment)
  .load(myUrl)
  .into(imageView);

Cancel load the image

(fragment).clear(imageView);

Use in RecyclerView

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
  String url = (position);
  (fragment)
    .load(url)
    .into();
}

Loading placeholder map

Placeholder during loading

(fragment)
 .load(url)
 .placeholder()
 .into(view);

Image displayed after loading failure (Error)

(fragment)
 .load(url)
 .error()
 .into(view);

Picture conversion

Most settings in Glide can be applied to the program through the RequestOptions class and the apply() method. Use request options to implement (including but not limited to):

  1. Placeholders
  2. Transformations
  3. Caching Strategies
  4. Component-specific settings, such as encoding quality, or Bitmap's decoding configuration, etc.

Loading a circular picture

 (this)
        .load(url)
        .apply(())
        .into(ivTest);

Loading the animation effect of the picture with fade in and out

 (this)
        .load(url)
        .transition(withCrossFade())
        .into(ivTest);

There are many conversion effects, you can try it one by one
cache

Glide's default cache policy is AUTOMATIC,
On disk cache

(fragment)
 .load(url)
 .diskCacheStrategy()
 .into(imageView);

Load images from cache only

(fragment)
 .load(url)
 .onlyRetrieveFromCache(true)
 .into(imageView);

Skip memory cache

(fragment)
 .load(url)
 .skipMemoryCache(true)
 .into(view);

Skip disk cache

(fragment)
 .load(url)
 .diskCacheStrategy()
 .into(view);

Skip all caches

(fragment)
 .load(url)
 .diskCacheStrategy()
 .skipMemoryCache(true)
 .into(view);

Clean up the disk cache

(applicationContext).clearDiskCache();

Advanced Usage

Load a picture as a Gaussian blur effect

How to use

Copy the codeThe code is as follows:

(getActivity()).load("/it/u=594559231,2167829292&fm=27&gp=").apply((new GlideBlurformation(getActivity()))).into(ivTest);
  

Other tools used

package ;
import ;
import ;
import ;
import .bitmap_recycle.BitmapPool;
import ;
import ;
/**
 * Created by yukuoyuan on 2017/9/29.
 */
public class GlideBlurformation extends BitmapTransformation {
  private Context context;
  public GlideBlurformation(Context context) {
     = context;
  }
  @Override
  protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
    return ().blurBitmap(context, toTransform, 20,outWidth,outHeight);
  }
  @Override
  public void updateDiskCacheKey(MessageDigest messageDigest) {
  }
}
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
 * Created by yukuoyuan on 2017/9/29.
 */
public class BlurBitmapUtil {
  private static BlurBitmapUtil sInstance;
  private BlurBitmapUtil() {
  }
  public static BlurBitmapUtil instance() {
    if (sInstance == null) {
      synchronized () {
        if (sInstance == null) {
          sInstance = new BlurBitmapUtil();
        }
      }
    }
    return sInstance;
  }
  /**
    * @param context context object
    * @param image Required blurry image
    * @param outWidth input width
    * @param outHeight The height of the output
    * @return Bitmap after fuzzy processing
    */
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  public Bitmap blurBitmap(Context context, Bitmap image, float blurRadius, int outWidth, int outHeight) {
    // Use the reduced image as a pre-rendered image    Bitmap inputBitmap = (image, outWidth, outHeight, false);
    // Create a rendered output image    Bitmap outputBitmap = (inputBitmap);
    // Create RenderScript kernel object    RenderScript rs = (context);
    // Create a fuzzy effect RenderScript tool object    ScriptIntrinsicBlur blurScript = (rs, Element.U8_4(rs));
    // Since RenderScript does not use VM to allocate memory, it is necessary to use the Allocation class to create and allocate memory space    // When creating the Allocation object, the memory is actually empty. You need to use copyTo() to fill the data in.    Allocation tmpIn = (rs, inputBitmap);
    Allocation tmpOut = (rs, outputBitmap);
    // Set the blur level of rendering, 25f is the maximum blur level    if (.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      (blurRadius);
    }
    // Set the input memory of the blurScript object    (tmpIn);
    // Save the output data to the output memory    (tmpOut);
    // Populate data into Allocation    (outputBitmap);
    return outputBitmap;
  }
}

Write the specific other effects yourself. In fact, the principle is very simple. It is to inherit the BitmapTransformation interface and then process the bitmap into the desired effect. There is no overly complicated process.

References:Glide official documentation

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.