Glide的缓存资源分为两种:
1.原图(SOURCE) :原始图片
2.处理图(RESULT) :经过压缩和变形等处理后的图片
磁盘缓存策略(diskCacheStrategy)
1.ALL:缓存原图(SOURCE)和处理图(RESULT)
2.NONE:什么都不缓存
3.SOURCE:只缓存原图(SOURCE)
4.RESULT:只缓存处理图(RESULT) —默认值
内存缓存:
glide默认使用了内存缓存,需要不使用内存缓存,调用
Glide.with(this).load(mUrl).skipMemoryCache(true).into(mIv);
清除内存缓存:
Glide.get(this).clearMemory();
清除磁盘缓存:
Glide.get(MainActivity.this).clearDiskCache();
演示图:从上到下依次为加载默认图、圆形图、圆角图、指定尺寸图、高斯模糊图、gif图。
Glide加载演示引入依赖:
implementation 'com.github.bumptech.glide:glide:4.8.0'
图片加载工具类:
/**
* create by libo
* create on 2018/12/26
* description Glide图片加载工具类
*/
public class ImageLoaderManager {
/**
* 默认加载方式
*
* @param context
* @param url
* @param imageView
*/
public static void loadImage(Context context, String url, ImageView imageView) {
RequestOptions requestOptions = new RequestOptions()
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.dontAnimate();
Glide.with(context)
.load(url)
.apply(requestOptions)
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
}
/**
* 加载圆形图片
*
* @param context
* @param url
* @param imageView
*/
public static void loadCircleImage(Context context, String url, ImageView imageView) {
RequestOptions requestOptions = new RequestOptions()
.priority(Priority.HIGH)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.bitmapTransform(new CircleCrop());
Glide.with(context)
.load(url)
.apply(requestOptions)
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
}
/**
* 加载圆角图片
* @param context
* @param url
* @param imageView
* @param radius 圆角大小
*/
public static void loadRoundImage(Context context, String url, ImageView imageView, int radius) {
RequestOptions requestOptions = new RequestOptions()
.priority(Priority.HIGH)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.transforms(new CenterCrop(), new RoundedCorners(radius));
Glide.with(context)
.load(url)
.apply(requestOptions)
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
}
/**
* 加载图片指定大小
*
* @param context
* @param url
* @param imageView
* @param width
* @param height
*/
public static void loadSizeImage(Context context, String url, ImageView imageView, int width, int height) {
RequestOptions requestOptions = new RequestOptions()
.priority(Priority.HIGH)
.override(width, height)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
Glide.with(context)
.load(url)
.apply(requestOptions)
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
}
/**
* 加载本地图片文件
*
* @param context
* @param file
* @param imageView
*/
public static void loadFileImage(Context context, File file, ImageView imageView) {
RequestOptions requestOptions = new RequestOptions()
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop();
Glide.with(context)
.load(file)
.apply(requestOptions)
.into(imageView);
}
/**
* 加载高斯模糊
*
* @param context
* @param url
* @param imageView
* @param radius 模糊级数 最大25
*/
public static void loadBlurImage(Context context, String url, ImageView imageView, int radius) {
RequestOptions requestOptions = new RequestOptions()
.override(300)
.transforms(new BlurTransformation(radius));
Glide.with(context)
.load(url)
.apply(requestOptions)
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
}
/**
* 加载gif图
* @param context
* @param url
* @param imageView
*/
public static void loadGifImage(Context context, String url, ImageView imageView) {
Glide.with(context)
.load(url)
.into(imageView);
}
}
activity调用代码:
public class MainActivity extends AppCompatActivity {
private ImageView ivDefault;
private ImageView ivCircle;
private ImageView ivRound;
private ImageView ivSize;
private ImageView ivBlur;
private ImageView ivGif;
private String url = "http://5b0988e595225.cdn.sohucs.com/images/20181129/b927a5afe1854667a5cf9b09dcebb1ee.jpeg";
private String gifUrl = "https://img.haomeiwen.com/i8669504/930fc652956b3f0e.gif";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
loadImage();
}
private void initView() {
ivDefault = findViewById(R.id.iv_default);
ivCircle = findViewById(R.id.iv_circle);
ivRound = findViewById(R.id.iv_round);
ivSize = findViewById(R.id.iv_size);
ivBlur = findViewById(R.id.iv_blur);
ivGif = findViewById(R.id.iv_gif);
}
private void loadImage() {
ImageLoaderManager.loadImage(getApplicationContext(), url, ivDefault);
ImageLoaderManager.loadCircleImage(getApplicationContext(), url, ivCircle);
ImageLoaderManager.loadRoundImage(getApplicationContext(), url, ivRound, 20);
ImageLoaderManager.loadSizeImage(getApplicationContext(), url, ivSize, 150, 150);
ImageLoaderManager.loadBlurImage(getApplicationContext(), url, ivBlur, 10);
ImageLoaderManager.loadGifImage(getApplicationContext(), gifUrl, ivGif);
}
}
调用ImageLoaderManager工具类对应方法即可,完整代码在github代码直通车。
网友评论