Android 网络本地显示图片Picasso优化封装使用
2017-01-20 14:06
393 查看
Picasso在使用过程中,经常忘记一些设置,比如图片的加载方式.centerCrop(),图片的加载优化.fit(),以及对图片的url的处理,因此对图片加载进行统一封装,使用相同的加载方式更有利于后期的维护和扩展。
图片的加载来源主要是String,file,Resource,我们都有相应的封装,
以下仅仅介绍String方式,以下同时有centerCrop和centerInside,同样我只是介绍centerCrop方式。
示例代码,对应三种加载方式:
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
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
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
这些常用的方式都有封装,如果统一调用更方便代码的扩展和维护。
图片的加载来源
图片的加载来源主要是String,file,Resource,我们都有相应的封装,
为了介绍方便
以下仅仅介绍String方式,以下同时有centerCrop和centerInside,同样我只是介绍centerCrop方式。 示例代码,对应三种加载方式:
public static void loadCenterCrop(@NonNull Context context, String path, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { Picasso.with(context) .load(checkAndHandleUrl(path)) .placeholder(placeholderResId) .error(errorResId) .centerCrop() .fit() //.tag(context) .into(target); } public static void loadCenterCrop(@NonNull Context context, @NonNull int resourceId, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { if (resourceId == 0) { target.setImageDrawable(context.getResources().getDrawable(placeholderResId)); } else { Picasso.with(context) .load(resourceId) .placeholder(placeholderResId) .error(errorResId) .centerCrop() .fit() .into(target); } } public static void loadCenterCrop(@NonNull Context context, File file, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { if (file == null) { target.setImageDrawable(context.getResources().getDrawable(placeholderResId)); } else { Picasso.with(context) .load(file) .placeholder(placeholderResId) .error(errorResId) .centerCrop() .fit() .into(target); } }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
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
主要的封装场景
1.正常加载图片
public static void loadCenterCrop(@NonNull Context context, String path, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { Picasso.with(context) .load(checkAndHandleUrl(path)) .placeholder(placeholderResId) .error(errorResId) .centerCrop() .fit() //.tag(context) .into(target); }1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
加载带圆角的图片
public static void loaderRoundConnerCenterCrop(@NonNull Context context, String path, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target, @NonNull int radius, @NonNull int margin) { if (path == null) { target.setImageDrawable(context.getResources().getDrawable(placeholderResId)); } else { Picasso.with(context) .load(checkAndHandleUrl(path)) .placeholder(placeholderResId) .error(errorResId) .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin)) .centerCrop() .fit() .into(target); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
加载圆形图片(常用于圆形头像)
public static void loaderCircleCenterCrop(@NonNull Context context, String path, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { if (path == null || path.trim().isEmpty()) { target.setImageDrawable(context.getResources().getDrawable(placeholderResId)); } else { Picasso.with(context) .load(checkAndHandleUrl(path)) .placeholder(placeholderResId) .error(errorResId) .transform(new CropCircleTransformation()) .centerCrop() .fit() .into(target); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
这些常用的方式都有封装,如果统一调用更方便代码的扩展和维护。
核心代码如下:
public class NewImageLoader {
private NewImageLoader() {
throw new IllegalStateException("no instance");
}
private static <T> void checkNull(T object, String message) {
if (object == null) {
throw new NullPointerException(message);
}
}
/**
* 检查传入的url或者数组中第一个url是不是为空
*/
@SuppressWarnings("uncheck") public static boolean isUrlsEmpty(String... urls) {
return urls == null || urls.length == 0 || urls[0] == null || urls[0].trim().isEmpty();
}
@SuppressWarnings("uncheck") public static String checkAndHandleUrl(String... urls) {
if (isUrlsEmpty(urls)) {
return "empty_url";
}
return urls[0];
}
public static Picasso getInstance(Context context) {
return Picasso.with(context);
}
public static void load(@NonNull Context context, String path, @DrawableRes int placeholderResId,
@NonNull ImageView target) {
Picasso.with(context)
.load(checkAndHandleUrl(path))
.placeholder(placeholderResId)
.error(placeholderResId)
.into(target);
}
/**
* 默认图为{@link R.drawable.img_defult}
*/
public static void loadCenterCrop(@NonNull Context context, String path,
@NonNull ImageView target) {
loadCenterC
12114
rop(context, path, R.drawable.img_defult, target);
}
public static void loadCenterCrop(@NonNull Context context, String path,
@DrawableRes int placeholderResId, @NonNull ImageView target) {
loadCenterCrop(context, path, placeholderResId, placeholderResId, target);
}
public static void loadCenterCrop(@NonNull Context context, String path, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { Picasso.with(context) .load(checkAndHandleUrl(path)) .placeholder(placeholderResId) .error(errorResId) .centerCrop() .fit() //.tag(context) .into(target); } public static void loadCenterCrop(@NonNull Context context, @NonNull int resourceId, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { if (resourceId == 0) { target.setImageDrawable(context.getResources().getDrawable(placeholderResId)); } else { Picasso.with(context) .load(resourceId) .placeholder(placeholderResId) .error(errorResId) .centerCrop() .fit() .into(target); } } public static void loadCenterCrop(@NonNull Context context, File file, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { if (file == null) { target.setImageDrawable(context.getResources().getDrawable(placeholderResId)); } else { Picasso.with(context) .load(file) .placeholder(placeholderResId) .error(errorResId) .centerCrop() .fit() .into(target); } }
/**
* 以下三个方法是centerCrop方式加载带圆角的图片
*/
public static void loaderRoundConnerCenterCrop(@NonNull Context context, String path, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target, @NonNull int radius, @NonNull int margin) { if (path == null) { target.setImageDrawable(context.getResources().getDrawable(placeholderResId)); } else { Picasso.with(context) .load(checkAndHandleUrl(path)) .placeholder(placeholderResId) .error(errorResId) .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin)) .centerCrop() .fit() .into(target); } }
public static void loaderRoundConnerCenterCrop(@NonNull Context context, int resourceId,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
@NonNull int radius, @NonNull int margin) {
if (resourceId == 0) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(resourceId)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
.centerCrop()
.fit()
.into(target);
}
}
public static void loaderRoundConnerCenterCrop(@NonNull Context context, File file,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
@NonNull int radius, @NonNull int margin) {
if (file == null) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(file)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
.centerCrop()
.fit()
.into(target);
}
}
/**
* 以下三个方法是centerCrop方式加载圆形的图片
*/
public static void loaderCircleCenterCrop(@NonNull Context context, String path, @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) { if (path == null || path.trim().isEmpty()) { target.setImageDrawable(context.getResources().getDrawable(placeholderResId)); } else { Picasso.with(context) .load(checkAndHandleUrl(path)) .placeholder(placeholderResId) .error(errorResId) .transform(new CropCircleTransformation()) .centerCrop() .fit() .into(target); } }
public static void loaderCircleCenterCrop(@NonNull Context context, @NonNull int resourceId,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
if (resourceId == 0) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(resourceId)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new CropCircleTransformation())
.centerCrop()
.fit()
.into(target);
}
}
public static void loaderCircleCenterCrop(@NonNull Context context, File file,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
if (file == null) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(file)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new CropCircleTransformation())
.centerCrop()
.fit()
.into(target);
}
}
/****************************** CenterInside ***************************************/
public static void loaderCenterInside(@NonNull Context context, String path,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
if (path == null || path.trim().isEmpty()) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(checkAndHandleUrl(path))
.placeholder(placeholderResId)
.error(errorResId)
.centerInside()
.fit()
.into(target);
}
}
public static void loaderCenterInside(@NonNull Context context, @NonNull int resourceId,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
if (resourceId == 0) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(resourceId)
.placeholder(placeholderResId)
.error(errorResId)
.centerInside()
.fit()
.into(target);
}
}
public static void loaderCenterInside(@NonNull Context context, File file,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
if (file == null) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(file)
.placeholder(placeholderResId)
.error(errorResId)
.centerInside()
.fit()
.into(target);
}
}
/**
* 以下三个方法是centerInside方式加载带圆角的图片
*/
public static void loaderRoundConnerCenterInside(@NonNull Context context, String path,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
@NonNull int radius, @NonNull int margin) {
if (path == null) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(checkAndHandleUrl(path))
.placeholder(placeholderResId)
.error(errorResId)
.transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
.centerInside()
.fit()
.into(target);
}
}
public static void loaderRoundConnerCenterInside(@NonNull Context context, int resourceId,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
@NonNull int radius, @NonNull int margin) {
if (resourceId == 0) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(resourceId)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
.centerInside()
.fit()
.into(target);
}
}
public static void loaderRoundConnerCenterInside(@NonNull Context context, File file,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
@NonNull int radius, @NonNull int margin) {
if (file == null) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(file)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
.centerInside()
.fit()
.into(target);
}
}
/**
* 以下三个方法是centerInside方式加载圆形的图片
*/
public static void loaderCircleCenterInside(@NonNull Context context, String path,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
if (path == null || path.trim().isEmpty()) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(path)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new CropCircleTransformation())
.centerInside()
.fit()
.into(target);
}
}
public static void loaderCircleCenterInside(@NonNull Context context, @NonNull int resourceId,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
if (resourceId == 0) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(resourceId)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new CropCircleTransformation())
.centerInside()
.fit()
.into(target);
}
}
public static void loaderCircleCenterInside(@NonNull Context context, File file,
@DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
if (file == null) {
target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
} else {
Picasso.with(context)
.load(file)
.placeholder(placeholderResId)
.error(errorResId)
.transform(new CropCircleTransformation())
.centerInside()
.fit()
.into(target);
}
}
}
相关文章推荐
- 串口并口
- http请求协议
- Linux网络防火墙的简单指令
- angular $http 传输图片,以及后台接收
- 网络通信中的异常分析
- GET,POST,PUT,DELETE的区别
- 生成对抗网络GANs理解(附代码)
- 学习整理HttpClient4.5成http访问服务类
- BZOJ 3931 网络吞吐量
- 浅谈angularjs $http提交数据探索
- 简析TCP的三次握手与四次分手
- HTTP请求时connectionRequestTimeout 、connectionTimeout、socketTimeout三个超时时间的含义
- httpclient4.5 https请求 忽略身份验证
- linux下常用的一些网络配置命令(待更新)
- httpClient 大概的执行/使用流程
- https原理:证书传递、验证和数据加密、解密过程解析
- HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
- java net https获取302跳转后地址
- PHP SERVER_NAME和HTTP_HOST的区别
- 网络性能测试