
版权声明:本文为博主原创文章,未经博主允许不得转载。
本篇文章项目github地址:MVPCommon
本文章原地址:简书博客
1 有哪些常用的图片加载库?
当下使用的主要有Piccaso、Fresco、Android-Universal-Image-Loader、Glide、Volley这五个图片加载框架。
关于这些图片加载框架的对比,网上可以找到很多文章。这里不做过多赘述。具体请参考5中的参考链接,肯定会对你有帮助。
2 为什么要封装?
这个段落的答案,摘抄自Stormzhang的文章 如何正确使用开源项目?
计算机史上有个万能的解决方案就是,如果原有层面解决不了问题,那么就请再加一层!
对于开源项目,我们知道有些库设计的确实很棒,使用者调用起来非常方便,一行代码直接搞定,拿图片加载库 Picasso 举个例子:
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
public class ImageLoader {
public static void with(Context context, String imageUrl, ImageView imageView) {
Picasso.with(context).load(imageUrl).into(imageView);
}
}
- 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
- 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
ImageLoader imageLoader =new ImageLoader.Builder().url(“img url”).imgView(mImgView).build();
ImageLoaderUtil.getInstance().loadImage(context,imageLoader);
- 1
- 2
- 1
- 2
package edu.com.mvplibrary.ui.widget.imageloader;
import android.content.Context;
/**
* Created by Anthony on 2016/3/3.
* Class Note:use this class to load image,single instance
*/
public class ImageLoaderUtil {
public static final int PIC_LARGE = 0;
public static final int PIC_MEDIUM = 1;
public static final int PIC_SMALL = 2;
public static final int LOAD_STRATEGY_NORMAL = 0;
public static final int LOAD_STRATEGY_ONLY_WIFI = 1;
private static ImageLoaderUtil mInstance;
private BaseImageLoaderProvider mProvider;
private ImageLoaderUtil(){
mProvider =new GlideImageLoaderProvider();
}
//single instance
public static ImageLoaderUtil getInstance(){
if(mInstance ==null){
synchronized (ImageLoaderUtil.class){
if(mInstance == null){
mInstance = new ImageLoaderUtil();
return mInstance;
}
}
}
return mInstance;
}
public void loadImage(Context context,ImageLoader img){
mProvider.loadImage(context,img);
}
}
- 1
- 2
- 3
- 1
- 2
- 3
package edu.com.mvplibrary.ui.widget.imageloader;
import android.content.Context;
/**
* Created by Anthony on 2016/3/3.
* Class Note:
* abstract class defined to load image
*/
public abstract class BaseImageLoaderProvider {
public abstract void loadImage(Context ctx, ImageLoader img);
}
- 1
- 2
- 3
- 1
- 2
- 3
package edu.com.mvplibrary.ui.widget.imageloader;
import android.content.Context;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import Java.io.IOException;
import java.io.InputStream;
import edu.com.mvplibrary.AbsApplication;
import edu.com.mvplibrary.util.AppUtils;
import edu.com.mvplibrary.util.SettingUtils;
/**
* Created by Anthony on 2016/3/3.
* Class Note:
* provide way to load image
*/
public class GlideImageLoaderProvider extends BaseImageLoaderProvider {
@Override
public void loadImage(Context ctx, ImageLoader img) {
boolean flag= SettingUtils.getOnlyWifiLoadImg(ctx);
//如果不是在wifi下加载图片,直接加载
if(!flag){
loadNormal(ctx,img);
return;
}
int strategy =img.getStrategy();
if(strategy == ImageLoaderUtil.LOAD_STRATEGY_ONLY_WIFI){
int netType = AppUtils.getNetWorkType(AbsApplication.app());
//如果是在wifi下才加载图片,并且当前网络是wifi,直接加载
if(netType == AppUtils.NETWORKTYPE_WIFI) {
loadNormal(ctx, img);
} else {
//如果是在wifi下才加载图片,并且当前网络不是wifi,加载缓存
loadCache(ctx, img);
}
}else{
//如果不是在wifi下才加载图片
loadNormal(ctx,img);
}
}
/**
* load image with Glide
*/
private void loadNormal(Context ctx, ImageLoader img) {
Glide.with(ctx).load(img.getUrl()).placeholder(img.getPlaceHolder()).into(img.getImgView());
}
/**
*load cache image with Glide
*/
private void loadCache(Context ctx, ImageLoader img) {
Glide.with(ctx).using(new StreamModelLoader<String>() {
@Override
public DataFetcher<InputStream> getResourceFetcher(final String model, int i, int i1) {
return new DataFetcher<InputStream>() {
@Override
public InputStream loadData(Priority priority) throws Exception {
throw new IOException();
}
@Override
public void cleanup() {
}
@Override
public String getId() {
return model;
}
@Override
public void cancel() {
}
};
}
}).load(img.getUrl()).placeholder(img.getPlaceHolder()).diskCacheStrategy(DiskCacheStrategy.ALL).into(img.getImgView());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
package edu.com.mvplibrary.ui.widget.imageloader;
import android.widget.ImageView;
import edu.com.mvplibrary.R;
/**
* Created by Anthony on 2016/3/3.
* Class Note:
* encapsulation of ImageView,Build Pattern used
*/
public class ImageLoader {
private int type; //类型 (大图,中图,小图)
private String url; //需要解析的url
private int placeHolder; //当没有成功加载的时候显示的图片
private ImageView imgView; //ImageView的实例
private int strategy;//加载策略,是否在wifi下才加载
private ImageLoader(Builder builder) {
this.type = builder.type;
this.url = builder.url;
this.placeHolder = builder.placeHolder;
this.imgView = builder.imgView;
this.strategy = builder.strategy;
}
public int getType() {
return type;
}
public String getUrl() {
return url;
}
public int getPlaceHolder() {
return placeHolder;
}
public ImageView getImgView() {
return imgView;
}
public int getStrategy() {
return strategy;
}
public static class Builder {
private int type;
private String url;
private int placeHolder;
private ImageView imgView;
private int strategy;
public Builder() {
this.type = ImageLoaderUtil.PIC_SMALL;
this.url = "";
this.placeHolder = R.drawable.default_pic_big;
this.imgView = null;
this.strategy = ImageLoaderUtil.LOAD_STRATEGY_NORMAL;
}
public Builder type(int type) {
this.type = type;
return this;
}
public Builder url(String url) {
this.url = url;
return this;
}
public Builder placeHolder(int placeHolder) {
this.placeHolder = placeHolder;
return this;
}
public Builder imgView(ImageView imgView) {
this.imgView = imgView;
return this;
}
public Builder strategy(int strategy) {
this.strategy = strategy;
return this;
}
public ImageLoader build() {
return new ImageLoader(this);
}
}
}
“`
5 参考链接
Introduction to Glide, Image Loader Library for Android, recommended by Google
FaceBook推出的Android图片加载库-Fresco
StackOverflow–>Picasso v/s Imageloader v/s Fresco vs Glide
本篇文章项目github地址:MVPCommon