LeeImageLoader.java
7.83 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.gimi.common.cinema.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LargestLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;
import com.nostra13.universalimageloader.utils.MemoryCacheUtils;
import com.xgimi.gimicinema.BuildConfig;
import com.xgimi.gimicinema.R;
import com.xgimi.gimicinema.model.ThreadUtils;
import java.io.File;
/**
* Created by wugian on 2016/7/26
*/
public class LeeImageLoader {
private static ImageLoader globalImageLoader = ImageLoader.getInstance();
private static DisplayImageOptions globalOptions;
private static ImageLoaderConfiguration config;
private LeeImageLoader() {
}
//当前需求不需要重载
public static void loadImage(Context context, String url, ImageView imageView) {
if (!BuildConfig.USE_GLIDE) {
loadByUIL(context, url, imageView);
} else {
loadByGlide(context, url, imageView);
}
}
//当前需求不需要重载
public static void loadImage(
Context context, String url, ImageView imageView, boolean glide) {
if (glide) {
loadByGlide(context, url, imageView);
} else {
loadByUIL(context, url, imageView);
}
}
private static void loadByGlide(Context context, String url, ImageView imageView) {
Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.RESULT)./*skipMemoryCache(true).*/error(R.drawable.ic_movie_default).into(imageView);
}
public static void loadBackground(Context context, String url, ImageView imageView) {
Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.RESULT)
.transform(new BlurTransformation(context)).centerCrop()
.error(R.drawable.ic_movie_default).into(imageView);
}
private static void loadByUIL(Context context, String url, ImageView imageView) {
if (config == null || globalImageLoader == null) {
initUIL(context.getApplicationContext());
}
if (imageView == null) {
throw new NullPointerException("传递参数imageView为空");
}
if (TextUtils.isEmpty(url)) {
globalImageLoader.displayImage(url, imageView, globalOptions);
return;
}
// if (url.endsWith("gif")) {
// loadByGlide(context, url, imageView);
// }
if (url.startsWith("file://")) {
globalImageLoader.displayImage(url, imageView, globalOptions);
} else if (url.startsWith("/mnt")) {
globalImageLoader.displayImage("file://" + url, imageView, globalOptions);
} else if (url.startsWith("drawable://")) {
globalImageLoader.displayImage(url, imageView, globalOptions);
} else {
globalImageLoader.displayImage(url, imageView, globalOptions);
}
}
public static void clearCache(final Context context) {
if (!BuildConfig.USE_GLIDE) {
// globalImageLoader.clearDiskCache();
// globalImageLoader.clearMemoryCache();
} else {
Glide.get(context).clearMemory();
ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
@Override
public void doSomeThing() {
Glide.get(context).clearDiskCache();
}
});
System.gc();
System.runFinalization();
}
}
public static void clearCache(String url) {
if (!BuildConfig.USE_GLIDE) {
DiskCacheUtils.removeFromCache("file://" + url, globalImageLoader.getDiskCache());
MemoryCacheUtils.removeFromCache("file://" + url, globalImageLoader.getMemoryCache());
}
}
private static void initUIL(Context context) {
globalOptions = new DisplayImageOptions.Builder()
// .showImageOnLoading(R.drawable.ic_movie_default) // 设置图片在下载期间显示的图片
.showImageForEmptyUri(R.drawable.ic_movie_default)// 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.ic_movie_default) // 设置图片加载/解码过程中错误时候显示的图片
.cacheInMemory(false)// 设置下载的图片是否缓存在内存中
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
.resetViewBeforeLoading(true)// 设置图片在下载前是否重置,复位
.displayer(new RoundedBitmapDisplayer(40))// 是否设置为圆角,弧度为多少
.displayer(new FadeInBitmapDisplayer(50))// 是否图片加载好后渐入的动画时间
.build();
config = new ImageLoaderConfiguration
.Builder(context)
.memoryCacheExtraOptions(240, 336)
// max width, max height,即保存的每个缓存文件的最大长宽
// .discCacheExtraOptions(480, 800, null)
// Can slow ImageLoader, use it carefully (Better don't use
// it)/设置缓存的详细信息,最好不要设置这个
.threadPoolSize(3)
.defaultDisplayImageOptions(globalOptions)
// 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.diskCacheFileNameGenerator(new Md5FileNameGenerator()) //缓存文件名的保存方式
.memoryCache(new LargestLimitedMemoryCache(8 * 1024 * 1024))
.discCache(new LimitedAgeDiskCache(new File("/sdcard/cinema_cache"), 2 * 24 * 60 * 60))
.discCacheFileCount(200) //缓存的文件数量
// You can pass your own memory cache
// implementation/你可以通过自己的内存缓存实现
// .memoryCacheSize(2 * 1024 * 1024)
// .discCacheSize(50 * 1024 * 1024)
//.discCacheFileNameGenerator(new Md5FileNameGenerator())
// 将保存的时候的URI名称用MD5 加密
.tasksProcessingOrder(QueueProcessingType.FIFO)
// .discCacheFileCount(100)
// 缓存的文件数量
//.discCache(new UnlimitedDiscCache(cacheDir))
// 自定义缓存路径
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.imageDownloader(
new BaseImageDownloader(context,
5 * 1000, 30 * 1000)) // connectTimeout
// (5
// s),
// readTimeout
// (30
// s)超时时间
//.writeDebugLogs() // Remove for release app
.build();
globalImageLoader.init(config);
}
}