package com.android.app_base.utils;
|
|
import android.content.Context;
|
import android.widget.ImageView;
|
|
import com.android.app_base.R;
|
import com.android.app_base.base.BaseApplication;
|
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
|
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
import com.bumptech.glide.request.RequestOptions;
|
|
/**
|
* @author Ljj
|
* @date 2023.04.02. 23:16
|
* @desc Glide工具类
|
*/
|
public class GlideUtil {
|
|
/**
|
* 加载图片到 ImageView 中
|
*
|
* @param imageUrl 图片 URL
|
* @param imageView ImageView 对象
|
*/
|
public static void loadImage(String imageUrl, ImageView imageView) {
|
RequestOptions options = new RequestOptions()
|
.placeholder(com.luck.picture.lib.R.drawable.ps_image_placeholder)
|
.error(com.luck.picture.lib.R.drawable.ps_image_placeholder)
|
.diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();
|
Glide.with(BaseApplication.getInstance())
|
.load(imageUrl)
|
.apply(options)
|
.into(imageView);
|
}
|
//圆形
|
public static <T> void loadCircleImage(T t, ImageView img) {
|
RequestOptions options = new RequestOptions()
|
.circleCrop()
|
.placeholder(com.luck.picture.lib.R.drawable.ps_image_placeholder)
|
.error(com.luck.picture.lib.R.drawable.ps_image_placeholder)
|
.diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();
|
Glide.with(BaseApplication.getInstance())
|
.load(t)
|
.apply(options)
|
.into(img);
|
}
|
|
//圆角
|
public static <T> void loadCornersImage(T t, ImageView img,int radius) {
|
Glide.with(BaseApplication.getInstance())
|
.load(t)
|
.diskCacheStrategy(DiskCacheStrategy.ALL)
|
.dontAnimate()
|
.transform(new CenterCrop(),new RoundedCorners(radius))
|
.into (img);
|
}
|
//透明度
|
public static <T> void loadAlphaImage(T t, ImageView img,float alpha) {
|
img.setAlpha(alpha);
|
RequestOptions options = new RequestOptions().centerCrop().placeholder(com.luck.picture.lib.R.drawable.ps_image_placeholder)
|
.diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();
|
Glide.with(BaseApplication.getInstance())
|
.load(t)
|
.apply(options)
|
.into (img);
|
}
|
|
/**
|
* 加载视频某时间的帧图片
|
* @param t 视频地址
|
* @param img 加载的控件
|
* @param radius 圆角,不需要传负数
|
* @param frame 需要加载的帧时间,单位微秒
|
* @param <T>
|
*/
|
public static <T> void loadVideoCover(T t, ImageView img,int radius,int frame) {
|
RequestOptions options = new RequestOptions().centerCrop()
|
.frame(frame)
|
.diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();
|
if (radius > 0){
|
RoundedCorners roundedCorners = new RoundedCorners(radius);
|
options.bitmapTransform(roundedCorners);
|
}
|
Glide.with(BaseApplication.getInstance())
|
.load(t)
|
.apply(options)
|
.into (img);
|
}
|
|
/**
|
* 清除 Glide 缓存
|
*
|
* @param context Context 对象
|
*/
|
public static void clearCache(Context context) {
|
Glide.get(context).clearMemory();
|
new Thread(() -> Glide.get(context).clearDiskCache()).start();
|
}
|
}
|