标签:
public abstract class BaseImageLoaderProvider { public abstract void loadImage(Context ctx, ImageLoader img); }
public class GlideImageLoaderCorpProvider extends BaseImageLoaderProvider { @Override public void loadImage(Context ctx, ImageLoader img) { int strategy = img.getStrategy(); if (strategy == ImageLoaderHelp.LOAD_STRATEGY_ONLY_WIFI) { String netType = NetworkStateHelp.getNetWorkModel(App.getApplication()); //如果是在wifi下才加载图片,并且当前网络是wifi,直接加载 if (NetworkStateHelp.MODE_WIFI.equals(netType)) { 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()).centerCrop().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()); } }
public class GlideImageLoaderProvider extends BaseImageLoaderProvider { @Override public void loadImage(Context ctx, ImageLoader img) { int strategy = img.getStrategy(); if (strategy == ImageLoaderHelp.LOAD_STRATEGY_ONLY_WIFI) { String netType = NetworkStateHelp.getNetWorkModel(App.getApplication()); //如果是在wifi下才加载图片,并且当前网络是wifi,直接加载 if (NetworkStateHelp.MODE_WIFI.equals(netType)) { 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()); } }
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 = ImageLoaderHelp.PIC_SMALL; this.url = ""; this.placeHolder = R.drawable.icon_fmoren; this.imgView = null; this.strategy = ImageLoaderHelp.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); } } }
/** * ImageLoader包装类,对外提供接口使用 */ public class ImageLoaderHelp { 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 ImageLoaderHelp mInstance; private BaseImageLoaderProvider mProvider; private BaseImageLoaderProvider mCorpProvider; private ImageLoaderHelp() { mProvider = new GlideImageLoaderProvider(); mCorpProvider = new GlideImageLoaderCorpProvider(); } //single instance public static ImageLoaderHelp getInstance() { if (mInstance == null) { synchronized (ImageLoaderHelp.class) { if (mInstance == null) { mInstance = new ImageLoaderHelp(); return mInstance; } } } return mInstance; } public void loadImage(Context context, ImageLoader img) { mProvider.loadImage(context, img); } public void loadImage(Context context, String uri, ImageView imageView) { ImageLoader imageLoader = new ImageLoader.Builder().url(uri).imgView(imageView).build(); mProvider.loadImage(context, imageLoader); } public void loadImageCorp(Context context, String uri, ImageView imageView) { ImageLoader imageLoader = new ImageLoader.Builder().url(uri).imgView(imageView).build(); mCorpProvider.loadImage(context, imageLoader); } public void loadQuestionHeadPic(Context context, String uri, ImageView imageView) { ImageLoader imageLoader = new ImageLoader.Builder().placeHolder(R.drawable.moren_tx_tiwenqu).url(uri).imgView(imageView).build(); mProvider.loadImage(context, imageLoader); } public void loadLiveroomHeadPic(Context context, String uri, ImageView imageView) { ImageLoader imageLoader = new ImageLoader.Builder().placeHolder(R.drawable.moren_tx_liaotianshi).url(uri).imgView(imageView).build(); mProvider.loadImage(context, imageLoader); } public void loadLiveroomPic(Context context, String uri, ImageView imageView) { ImageLoader imageLoader = new ImageLoader.Builder().placeHolder(R.drawable.img_shibai).url(uri).imgView(imageView).build(); mProvider.loadImage(context, imageLoader); } public void loadTopicInfoGuestHeadPic(Context context, String uri, ImageView imageView) { ImageLoader imageLoader = new ImageLoader.Builder().placeHolder(R.drawable.moren_tx_jinruzhibojian).url(uri).imgView(imageView).build(); mProvider.loadImage(context, imageLoader); } public void loadTopicInfoBannerPic(Context context, String uri, ImageView imageView) { ImageLoader imageLoader = new ImageLoader.Builder().placeHolder(R.drawable.banner_zhibo).url(uri).imgView(imageView).build(); mCorpProvider.loadImage(context, imageLoader); } }
加载图片调用
ImageLoader imageLoader = new ImageLoader.Builder().url(myCourseEntity.getImgUrl()).imgView(myCourseViewHolder.myCourseImage).build(); ImageLoaderHelp.getInstance().loadImage(context,imageLoader);
标签:
原文地址:http://www.cnblogs.com/anni-qianqian/p/5734796.html