Linjiajia
2023-04-24 fcdddf8b9b34f9930bec454b5fffe41c0e33ba3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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();
    }
}