TinyImage:开源图片框架的封装以及开源图片框架的切换使用

  1. 新建一个GlideOptions类,专门用来配置Glide要求的图片参数信息:

public class GlideOptions extends BaseImageOptions {

private DiskCacheStrategy diskCacheStrategy;

private int width;

private int height;

private boolean isGif;

private GlideOptions(Builder builder) {

this.resId = builder.resId;

this.url = builder.url;

this.imageView = builder.imageView;

this.placeholder = builder.placeholder;

this.errorImage = builder.errorImage;

this.diskCacheStrategy = builder.cache;

this.width = builder.width;

this.height = builder.height;

this.isGif = builder.isGif;

}

public DiskCacheStrategy getCacheStrategy() {

return diskCacheStrategy;

}

public int getWidth() {

return width;

}

public int getHeight() {

return height;

}

public boolean isGif() {

return isGif;

}

public static Builder builder() {

return new Builder();

}

// Builder模式

public static final class Builder {

private int resId;

private String url;

private ImageView imageView;

private int placeholder;

private int errorImage;

private DiskCacheStrategy cache;

private int width;

private int height;

private boolean isGif;

private Builder() {

}

public Builder resId(int resId) {

this.resId = resId;

return this;

}

public Builder url(String url) {

this.url = url;

return this;

}

public Builder imageView(ImageView imageView) {

this.imageView = imageView;

return this;

}

public Builder placeholder(int placeholder) {

this.placeholder = placeholder;

return this;

}

public Builder errorImage(int errorImage) {

this.errorImage = errorImage;

return this;

}

public Builder cache(DiskCacheStrategy cache) {

this.cache = cache;

return this;

}

public Builder width(int width) {

this.width = width;

return this;

}

public Builder height(int height) {

this.height = height;

return this;

}

public Builder gif(boolean isGif) {

this.isGif = isGif;

return this;

}

public GlideOptions build() {

return new GlideOptions(this);

}

}

}

  1. 新建一个GlideManager单例类,用来加载图片:

public class GlideManager {

private static volatile GlideManager sInstance = null;

private GlideManager() {

}

public static GlideManager getInstance() {

if (sInstance == null) {

synchronized (GlideManager.class) {

if (sInstance == null) {

sInstance = new GlideManager();

}

}

}

return sInstance;

}

public void display(Context context, GlideOptions options) {

if (context != null && options != null) {

RequestManager requestManager = Glide.with(context);

DrawableTypeRequest request = getDrawableTypeRequest(options, requestManager);

// 设定图片长宽

if (options.getWidth() != 0 && options.getHeight() != 0) {

request.override(options.getWidth(), options.getHeight());

}

// 磁盘存储

if (options.getCacheStrategy() != null) {

request.diskCacheStrategy(options.getCacheStrategy());

}

// gif动画

if(options.isGif()){

request.asGif();

}

// imageView控件

if (options.getImageView() != null) {

request.into(options.getImageView());

}

}

}

private DrawableTypeRequest getDrawableTypeRequest(GlideOptions options, RequestManager requestManager) {

DrawableTypeRequest request = null;

if (!TextUtils.isEmpty(options.getUrl())) { // 网络加载

request = requestManager.load(options.getUrl());

Log.e(“TAG”, "getUrl : " + options.getUrl());

} else if (options.getResId() > 0) { // 本地加载

request = requestManager.load(options.getResId());

Log.e(“TAG”, "getResId : " + options.getResId());

}

return request;

}

}

  1. 使用:

GlideOptions options = GlideOptions.builder().url(url).imageView(iv).build();

GlideManager.getInstance().display(this, options);

拓展:切换图片框架

==================================================================

上面已经完成了对Glide的封装功能,但是如果哪一天项目中使用了其它的图片加载库,比如Picasso,或者公司中间件部门自己封装的图片加载库,那我们是不是又要重新写一套XXXOptions、XXXManager呢?

对于这种无缝切换图片框架的实现,我们想要实现的目的是,在调用方不用更改代码,只需要切换不同代码框架的配置信息即可。

通过接口,可以屏蔽这些不同。

public interface BaseStrategy {

void displayImage(Context context, T options);

}

实现Glide的Strategy:

public class GlideStrategy implements BaseStrategy {

@Override

public void displayImage(Context context, GlideOptions options) {

if (context != null && options != null) {

RequestManager requestManager = Glide.with(context);

DrawableTypeRequest request = getDrawableTypeRequest(options, requestManager);

// 设定图片长宽

if (options.getWidth() != 0 && options.getHeight() != 0) {

request.override(options.getWidth(), options.getHeight());

}

// 磁盘存储

if (options.getCacheStrategy() != null) {

request.diskCacheStrategy(options.getCacheStrategy());

}

// gif动画

if (options.isGif()) {

request.asGif();

}

// imageView控件

if (options.getImageView() != null) {

request.into(options.getImageView());

}

}

}

private DrawableTypeRequest getDrawableTypeRequest(GlideOptions options, RequestManager requestManager) {

DrawableTypeRequest request = null;

if (!TextUtils.isEmpty(options.getUrl())) { // 网络加载

request = requestManager.load(options.getUrl());

Log.e(“TAG”, "getUrl : " + options.getUrl());

} else if (options.getResId() > 0) { // 本地加载

request = requestManager.load(options.getResId());

Log.e(“TAG”, "getResId : " + options.getResId());

}

return request;

}

}

实现ImageLoader,通用的图片调用类:

public class ImageLoader {

private static volatile ImageLoader sInstance = null;

private BaseStrategy strategy;

public void setStrategy(BaseStrategy strategy) {

this.strategy = strategy;

}

private ImageLoader() {

}

public static ImageLoader getInstance() {

if (sInstance == null) {

synchronized (ImageLoader.class) {

if (sInstance == null) {

sInstance = new ImageLoader();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值