GlideUtil.java
4.75 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
package com.chudiangameplay.android.util;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.GifRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.rockerhieu.emojicon.emoji.Objects;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
/**
* Created by zt on 2017/5/16.
* 图片加载工具类
*/
public class GlideUtil {
public static DrawableRequestBuilder<Object> getMode(Context context, Object url, int loadingImg, int compress) {
boolean isGif = false;
boolean isJPG = false;
if (url instanceof String) {
String urlStr = ((String) url).toLowerCase();
if (urlStr.endsWith(".gif")) {
isGif = true;
} else if (urlStr.endsWith(".jpg") || urlStr.endsWith(".jpeg") || urlStr.endsWith(".png")) {
isJPG = true;
}
//oss压缩处理
if (compress > 0) {
url = url + "?x-oss-process=image/resize,m_lfit,h_" + compress + ",w_" + compress;
}
}
DrawableTypeRequest<Object> builder = Glide.with(context).load(url);
if (isGif) {
builder.asGif();
builder.skipMemoryCache(true);
} else if (isJPG) {
builder.asBitmap();
} else {
builder.diskCacheStrategy(DiskCacheStrategy.NONE);
}
builder.placeholder(loadingImg) //loading 图片
.error(loadingImg)
.animate(android.R.anim.fade_in); //或者int animationId
return builder;
}
//传入尺寸
public static void displayImgWithSize(Context context, Object url, ImageView imageView, int loadingImg, int width, int height) {
try {
DrawableRequestBuilder<Object> builder = getMode(context, url, loadingImg, 0);
builder.override(width, height).diskCacheStrategy(DiskCacheStrategy.RESULT);
builder.into(imageView);
} catch (Exception e) {
}
}
public static void displayImageCompress(final Context context, final Object url, final ImageView imageView, final int loadingImg) {
imageView.post(new Runnable() {
@Override
public void run() {
DrawableRequestBuilder<Object> builder = getMode(context, url, loadingImg, imageView.getWidth() * 2);
builder.into(imageView);
}
});
}
/**
* @param context
* @param url 图片路径(网络图片/本地资源文件)
* @param imageView ImageView
* @param loadingImg 加载中图片
*/
public static void displayImage(Context context, Object url, ImageView imageView, int loadingImg) {
try {
DrawableRequestBuilder<Object> builder = getMode(context, url, loadingImg, 0);
builder.into(imageView);
} catch (Exception e) {
}
}
public static void displayGifOnce(Context context, Object url, ImageView imageView, int loadingImg, RequestListener<Object, GlideDrawable> listener) {
try {
DrawableRequestBuilder<Object> builder = getMode(context, url, loadingImg, 0);
builder.listener(listener).into(new GlideDrawableImageViewTarget(imageView, 1));
} catch (Exception e) {
}
}
/**
* @param context
* @param url 图片路径(网络图片/本地资源文件)
* @param imageView ImageView
* @param loadingImg 加载中图片
*/
public static void displayImageNoAnim(Context context, Object url, ImageView imageView, int loadingImg) {
try {
DrawableRequestBuilder<Object> builder = getMode(context, url, loadingImg, 0);
builder.dontAnimate();
builder.into(imageView);
} catch (Exception e) {
}
}
/**
* @param context
* @param imageView ImageView
* @param url 图片路径(网络图片/本地资源文件)
* @param loadingImg 加载中图片
* @param coner 圆角大小
*/
public static void displayImage(Context context, Object url, ImageView imageView, int loadingImg, int coner) {
try {
DrawableRequestBuilder<Object> builder = getMode(context, url, loadingImg, 0);
builder.bitmapTransform(new RoundedCornersTransformation(context, Util.dip2px(context, coner), 0));
builder.into(imageView);
} catch (Exception e) {
}
}
}