-
Notifications
You must be signed in to change notification settings - Fork 384
Expand file tree
/
Copy pathGlideImageLoader.java
More file actions
97 lines (82 loc) · 3.07 KB
/
Copy pathGlideImageLoader.java
File metadata and controls
97 lines (82 loc) · 3.07 KB
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
package com.vansz.glideimageloader;
import android.content.Context;
import androidx.annotation.Nullable;
import com.blankj.utilcode.util.FileUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.hitomi.tilibrary.loader.ImageLoader;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Vans Z on 2020-02-28.
* 暂不支持百分比进度指示器
*/
public class GlideImageLoader implements ImageLoader {
private Context context;
private Map<String, SourceCallback> callbackMap;
private static final String CACHE_DIR = "TransGlide";
private GlideImageLoader(Context context) {
this.context = context;
this.callbackMap = new HashMap<>();
}
public static GlideImageLoader with(Context context) {
return new GlideImageLoader(context);
}
@Override
public void loadSource(final String imageUrl, final SourceCallback callback) {
callbackMap.put(imageUrl, callback);
if (callback != null) callback.onStart();
Glide.with(context).download(imageUrl).listener(new RequestListener<File>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
SourceCallback callback = callbackMap.get(imageUrl);
if (callback != null)
callback.onDelivered(STATUS_DISPLAY_FAILED, null);
callbackMap.remove(imageUrl);
return false;
}
@Override
public boolean onResourceReady(final File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
if (callback != null)
callback.onDelivered(STATUS_DISPLAY_SUCCESS, resource);
callbackMap.remove(imageUrl);
return false;
}
}).preload();
}
@Override
public File getCache(String url) {
File cacheFile = new File(getCacheDir(), getCacheFileName(url));
return cacheFile.exists() ? cacheFile : null;
}
@Override
public void clearCache() {
Glide.get(context).clearMemory();
new Thread(new Runnable() {
@Override
public void run() {
Glide.get(context).clearDiskCache();
FileUtils.delete(getCacheDir());
}
}).start();
}
@Override
public File getCacheDir() {
File cacheDir = new File(context.getCacheDir(), CACHE_DIR);
if (!cacheDir.exists()) cacheDir.mkdirs();
return cacheDir;
}
@Override
public String getCacheFileName(String url) {
int paramsStartIndex = url.indexOf("?");
if (paramsStartIndex!=-1){
url = url.substring(0,paramsStartIndex);
}
String[] nameArray = url.split("/");
return nameArray[nameArray.length - 1];
}
}