Android图片加载框架封装

前言

本篇文章主要介绍的是第三方图片加载框架的封装

一、概述

  • 关于 Android 图片加载框架实在是很多,而且还都很不错,目前主要流行的是 Glide 、 Picasso 、 Fresco,附上一张图,github 上的 star 排名:
Android图片加载框架封装_第1张图片
image

其中 Android-Universal-Image-Loader 已经于两年前停止更新了,所以并不推荐使用

二、如何选择

2.1 首先,先看下如何选择开源项目
  • 三方链接:帅比张
2.3 图片加载框架对比
  • 关于这方面的东西,网上大把大把的,也都写得不错,我就没必要重复造轮子了

  • 三方链接:帅比张

三、图片加载框架的封装

3.1 为什么要封装?
  • 第三方的框架都已经写的很好了,我们使用起来也很方便,拿 Glide 举个例子
//一行代码就完成了图片加载
Glide.with(this).load(url).into(imageView);

  • 假设个应用场景,随着公司的业务发展,当前的图片加载框架可能满足不了公司的需求了,需要替换一个图片加载框架,如果按上边的那种方式的话,那得该多少代码,花了几天功夫改完之后,领导又说,我觉得还是要之前的那个框架的吧,你什么感觉??

  • 不管是为了偷懒,还是为了代码的扩展性,或者是为了应付领导的需求等等,我们都需要再次进行封装。

3.2 如何封装,封装原则是啥
  • 统一接口,一个功能的实现,封装在一个类中,一旦有什么改变,也只需要改变这一个类即可,如:
public class ImageLoader {
    public static void with(Context context, String imageUrl, ImageView imageView) {
        Picasso.with(context).load(imageUrl).into(imageView); 
    }
}

项目中所有需要图片加载功能的地方都这么调用,这样改动起来也很方便。

  • 采用策略模式来封装,根据需求替换不同的图片加载策略即可。

四、具体封装步骤

只是简单封装,主要是为了介绍策略模式

用我自己的话描述一下吧

第一步,写一个接口,统一方法名,当然里边也可以有多个方法,可以自己根据需求添加
/**
 * 图片加载策略  接口
 */

public interface BaseImageLoaderStrategy {

    //第一个参数,上下文对象,
    //第二个参数,是封装的一个类,下边会介绍
    void loadImage(Context context,ImageLoader imageLoader);
}
第二步,写一个类继承自第一步的接口,并实现具体的加载策略
/**
 * 具体的加载策略, Glide 加载框架
 */

public class GlideImageLoader implements BaseImageLoaderStrategy {

    @Override
    public void loadImage(Context context, ImageLoader imageLoader) {

        Glide.with(context)
                .load(imageLoader.getUrl())
                .placeholder(imageLoader.getPlaceHolder())
                .into(imageLoader.getImgView());

    }
}
第三步,介绍 ImageLoader 类的封装及作用
  • ImageLoader 类是将常用的网络请求框架所需要的参数封装到一个类中,便于管理

  • ImageLoader 类的封装,采用的是 Builder 模式,因为参数太多,这样能保证代码规范整洁,关于 Builder模式请看:https://adsuper.github.io/2017/05/09/java%E6%A8%A1%E5%BC%8F%E4%B9%8BBuilder%E6%A8%A1%E5%BC%8F/

/**
 * 设置具体的参数,设计为 Builder 模式,方便拆分功能
 */

public class ImageLoader {

    private int type;  //类型 (大图,中图,小图)
    private String url; //需要解析的 url
    private int placeHolder; //当没有成功加载的时候显示的图片
    private ImageView imgView; //ImageView 的实例
    private int wifiStrategy;//加载策略,是否在 wifi 下才加载

    private ImageLoader(Builder builder) {
        this.type = builder.type;
        this.url = builder.url;
        this.placeHolder = builder.placeHolder;
        this.imgView = builder.imgView;
        this.wifiStrategy = builder.wifiStrategy;
    }

    public int getType() {
        return type;
    }

    public String getUrl() {
        return url;
    }

    public int getPlaceHolder() {
        return placeHolder;
    }

    public ImageView getImgView() {
        return imgView;
    }

    public int getWifiStrategy() {
        return wifiStrategy;
    }
    public static class Builder{
        private int type;  //类型 (大图,中图,小图)
        private String url; //需要解析的url
        private int placeHolder; //当没有成功加载的时候显示的图片
        private ImageView imgView; //ImageView的实例
        private int wifiStrategy;//加载策略,是否在wifi下才载

        public Builder() {
            this.type =  ImageLoadreUtils.PIC_SMALL;
            this.url = "";
            this.placeHolder = R.mipmap.ic_launcher;
            this.imgView = null;
            this.wifiStrategy = ImageLoadreUtils.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 wifiStrategy(int wifiStrategy) {
            this.wifiStrategy = wifiStrategy;
            return this;
        }
        public ImageLoader bulid(){
            return new ImageLoader(this);
        }
    }

}
第四步,策略者,调度者
  • 前边的基础都打好了,后边就该实现策略模式的优点了,看代码:
**
 * 图片加载框架 策略模式
 *
 * 设计为单例模式,并且暴露一个方法,可以设置加载模式,使用哪种图片加载框架。
 */

public class ImageLoadreUtils {
    //图片类型
    public static final int PIC_LARGE = 0;//大图
    public static final int PIC_MEDIUM = 1;//中图
    public static final int PIC_SMALL = 2;//小图
    //是否在 WIFI 下加载
    public static final int LOAD_STRATEGY_NORMAL = 0;
    public static final int LOAD_STRATEGY_ONLY_WIFI = 1;


    private static ImageLoadreUtils mInstance;

    private BaseImageLoaderStrategy imageLoaderStrategy;

    private ImageLoadreUtils() {
        //默认使用 Glide 加载模式
        imageLoaderStrategy = new GlideImageLoader();

    }

    public static ImageLoadreUtils getInstance() {
        if (mInstance == null) {
            synchronized (ImageLoadreUtils.class) {
                if (mInstance == null) {
                    mInstance = new ImageLoadreUtils();
                    return mInstance;
                }
            }
        }
        return mInstance;
    }

    /**
     * 设置使用的图片加载框架
     * @param imageLoaderStrategy
     */
    public void setImageLoaderStrategy(BaseImageLoaderStrategy imageLoaderStrategy){

        this.imageLoaderStrategy = imageLoaderStrategy;
    }

    /**
     * 加载图片
     * @param context
     * @param imageLoader
     */
    public void loadImage(Context context, ImageLoader imageLoader){
        imageLoaderStrategy.loadImage(context,imageLoader);
    }

代码中的注释写的很清楚,这样一来,就算以后想换框架,也只是加代码,而不会改动之前已经调试好的代码。

本文是参考另一篇文章所写,附上链接:
http://www.jianshu.com/p/e26130a93289

你可能感兴趣的:(Android图片加载框架封装)