Skip to content

12 配置ImageEngine,支持所有图片加载库(fresco除外)

joker-fu edited this page Jul 3, 2019 · 1 revision

配置ImageEngine

为支持所有图片加载库(fresco除外)以及任意图片加载库的任意版本,EasyPhotos设计了图片加载引擎接口ImageEngine,大家可以通过对ImageEngine接口的实现来配置自己所使用的图片加载库。 ImageEngine接口文件如下:

/**
 * 自定义图片加载方式
 * Created by huan on 2018/1/15.
 */
public interface ImageEngine {
    /**
     * 加载图片到ImageView
     *
     * @param context   上下文
     * @param photoPath 图片路径
     * @param imageView 加载到的ImageView
     */
    void loadPhoto(Context context, String photoPath, ImageView imageView);

    /**
     * 加载gif动图图片到ImageView,gif动图不动
     *
     * @param context   上下文
     * @param gifPath   gif动图路径
     * @param imageView 加载到的ImageView
     *                  <p>
     *                  备注:不支持动图显示的情况下可以不写
     */
    void loadGifAsBitmap(Context context, String gifPath, ImageView imageView);

    /**
     * 加载gif动图到ImageView,gif动图动
     *
     * @param context   上下文
     * @param gifPath   gif动图路径
     * @param imageView 加载动图的ImageView
     *                  <p>
     *                  备注:不支持动图显示的情况下可以不写
     */
    void loadGif(Context context, String gifPath, ImageView imageView);

    /**
     * 获取图片加载框架中的缓存Bitmap
     *
     * @param context 上下文
     * @param path    图片路径
     * @param width   图片宽度
     * @param height  图片高度
     * @return Bitmap
     * @throws Exception 异常直接抛出,EasyPhotos内部处理
     */
    Bitmap getCacheBitmap(Context context, String path, int width, int height) throws Exception;


}
    
ImageEngine接口实现举例
Glide4.x的实现方式:

/**
 * Glide4.x的加载图片引擎实现,单例模式
 * Glide4.x的缓存机制更加智能,已经达到无需配置的境界。如果使用Glide3.x,需要考虑缓存机制。
 * Created by huan on 2018/1/15.
 */

public class GlideEngine implements ImageEngine {
    //单例
    private static GlideEngine instance = null;
    //单例模式,私有构造方法
    private GlideEngine() {
    }
    //获取单例
    public static GlideEngine getInstance() {
        if (null == instance) {
            synchronized (GlideEngine.class) {
                if (null == instance) {
                    instance = new GlideEngine();
                }
            }
        }
        return instance;
    }

    /**
     * 加载图片到ImageView
     *
     * @param context   上下文
     * @param photoPath 图片路径
     * @param imageView 加载到的ImageView
     */
    @Override
    public void loadPhoto(Context context, String photoPath, ImageView imageView) {
        Glide.with(context).load(photoPath).transition(withCrossFade()).into(imageView);
    }

    /**
     * 加载gif动图图片到ImageView,gif动图不动
     *
     * @param context   上下文
     * @param gifPath   gif动图路径
     * @param imageView 加载到的ImageView
     *                  <p>
     *                  备注:不支持动图显示的情况下可以不写
     */
    @Override
    public void loadGifAsBitmap(Context context, String gifPath, ImageView imageView) {
        Glide.with(context).asBitmap().load(gifPath).into(imageView);
    }

    /**
     * 加载gif动图到ImageView,gif动图动
     *
     * @param context   上下文
     * @param gifPath   gif动图路径
     * @param imageView 加载动图的ImageView
     *                  <p>
     *                  备注:不支持动图显示的情况下可以不写
     */
    @Override
    public void loadGif(Context context, String gifPath, ImageView imageView) {
        Glide.with(context).asGif().load(gifPath).transition(withCrossFade()).into(imageView);
    }


    /**
     * 获取图片加载框架中的缓存Bitmap
     *
     * @param context 上下文
     * @param path    图片路径
     * @param width   图片宽度
     * @param height  图片高度
     * @return Bitmap
     * @throws Exception 异常直接抛出,EasyPhotos内部处理
     */
    @Override
    public Bitmap getCacheBitmap(Context context, String path, int width, int height) throws Exception {
        return Glide.with(context).asBitmap().load(path).submit(width, height).get();
    }

}

以GlideEngine为图片加载引擎启动EasyPhotos相册的示例:


EasyPhotos.createAlbum(this, false, GlideEngine.getInstance())//参数说明:上下文,是否显示相机按钮,[配置Glide为图片加载引擎]
                        .start(callback);