Use of Android Glide 4.0 +

  • 2021-08-28 21:08:52
  • OfStack

Download and setup

Android SDK Requirements

Use Minimum Requirements-Use Glide requires SDK version API 14 (Ice Cream Sandwich) and above.

Minimum Requirements for Compilation-Glide should be compiled with SDK version API 26 (Oreo) and above.

jar

You can download the latest jar package directly from GitHub

Gradle

If you use Gradle, you can add a dependency on Glide from Maven, Central or JCenter. Similarly, you need to add dependencies to the Android support library.


repositories {
 mavenCentral()
 maven { url 'https://maven.google.com' }
}
dependencies {
  compile 'com.github.bumptech.glide:glide:4.1.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1'
}

Kotlin

If you use Glide annotations in classes written by Kotlin, you need to introduce an kapt dependency instead of the regular annotationProcessor dependency:


dependencies {
 kapt 'com.github.bumptech.glide:compiler:4.1.1'
}

Start using

Basic usage

Load pictures


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

Cancel loading pictures


Glide.with(fragment).clear(imageView);

Used in RecyclerView


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

Load placeholder

Occupation during loading (Placeholder)


Glide.with(fragment)
 .load(url)
 .placeholder(R.drawable.placeholder)
 .into(view);

Picture displayed after load failure (Error)


Glide.with(fragment)
 .load(url)
 .error(R.drawable.error)
 .into(view);

Conversion of pictures

Most of the settings in Glide can be applied to your program through the RequestOptions class and the apply () method. Using request options, you can achieve (including but not limited to):

Placeholder map (Placeholders) Conversion (Transformations) Cache Policy (Caching Strategies) Component-specific settings, such as encoding quality, or Bitmap decoding configuration, etc.

Load a circular picture


 Glide.with(this)
        .load(url)
        .apply(RequestOptions.circleCropTransform())
        .into(ivTest);

Load pictures with fade-in and fade-out animation effects


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

And so on a lot of conversion effect, specific oneself can try one by one
Cache

The default cache policy for Glide is AUTOMATIC,
Cached on disk


GlideApp.with(fragment)
 .load(url)
 .diskCacheStrategy(DiskCacheStrategy.ALL)
 .into(imageView);

Load pictures only from the cache


dependencies {
 kapt 'com.github.bumptech.glide:compiler:4.1.1'
}
0

Skip memory cache


dependencies {
 kapt 'com.github.bumptech.glide:compiler:4.1.1'
}
1

Skip disk cache


dependencies {
 kapt 'com.github.bumptech.glide:compiler:4.1.1'
}
2

Skip all caches


GlideApp.with(fragment)
 .load(url)
 .diskCacheStrategy(DiskCacheStrategy.NONE)
 .skipMemoryCache(true)
 .into(view);

Clean up the disk cache


dependencies {
 kapt 'com.github.bumptech.glide:compiler:4.1.1'
}
4

Advanced usage

Load 1 picture for Gaussian blur effect

Usage


Glide.with(getActivity()).load("http://img1.imgtn.bdimg.com/it/u=594559231,2167829292&fm=27&gp=0.jpg").apply(RequestOptions.bitmapTransform(new GlideBlurformation(getActivity()))).into(ivTest);

Other tool classes used


dependencies {
 kapt 'com.github.bumptech.glide:compiler:4.1.1'
}
5

dependencies {
 kapt 'com.github.bumptech.glide:compiler:4.1.1'
}
6

Specific other effects on their own write it, in fact, the principle is very simple, is through the inheritance of BitmapTransformation interface, and then in the bitmap processing for their desired effect. There is no too complicated process.

Reference: Glide official document


Related articles: