图片加载引擎框架封装

一、新建接口编写加载方式

public interface IImageLoader {
    void load(@NonNull ImageView imageView, @NonNull Object imageUrl);
    void load(@NonNull ImageView imageView, @NonNull Object imageUrl,int width, int height);
    void load(@NonNull ImageView imageView, @NonNull Object imageUrl,  int errorImage,int width, int height);
    void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int defaultImage, int errorImage,int width, int height);
    void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int defaultImage, int errorImage,int width, int height,@NonNull Object transformation);
    void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int errorImage,int width, int height,@NonNull Object transformation);
    void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int width, int height,@NonNull Object transformation);
    void load(@NonNull ImageView imageView, @NonNull Object imageUrl,@NonNull Object transformation);
}

二、创建实现类实现接口去加载

public class ImageLoaderImpl implements IImageLoader{

    private static IImageLoader imageLoader;
    private static ImageLoaderImpl imageLoaderImpl;

    public static void init(IImageLoader imageLoader2){
        imageLoader = imageLoader2;

    }

    private ImageLoaderImpl() {
    }

    public static ImageLoaderImpl getInstance() {
        if (imageLoader == null) {
            throw new NullPointerException("必须在入口初始化imageLoader加载器引擎");
        }
        if(null==imageLoaderImpl){
            synchronized (ImageLoaderImpl.class){
                if(null==imageLoaderImpl) {
                    imageLoaderImpl = new ImageLoaderImpl();
                }
            }
        }
        return imageLoaderImpl;
    }



    @Override
    public void load(@NonNull ImageView imageView, @NonNull Object imageUrl) {
        imageLoader.load(imageView,imageUrl);
    }

    @Override
    public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int width, int height) {
        imageLoader.load(imageView,imageUrl,width,height);
    }

    @Override
    public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int errorImage, int width, int height) {
        imageLoader.load(imageView,imageUrl,errorImage,width,height);
    }

    @Override
    public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int defaultImage, int errorImage, int width, int height) {
        imageLoader.load(imageView,imageUrl,defaultImage,errorImage,width,height);
    }

    @Override
    public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int defaultImage, int errorImage, int width, int height, @NonNull Object transformation) {
        imageLoader.load(imageView,imageUrl,defaultImage,errorImage,width,height,transformation);
    }

    @Override
    public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int errorImage, int width, int height, @NonNull Object transformation) {
        imageLoader.load(imageView,imageUrl,errorImage,width,height,transformation);
    }

    @Override
    public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int width, int height, @NonNull Object transformation) {
        imageLoader.load(imageView,imageUrl,width,height,transformation);
    }

    @Override
    public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, @NonNull Object transformation) {
        imageLoader.load(imageView,imageUrl,transformation);
    }
}

三、在程序入口处初始化实现类且自定义引擎

一般在application的onCreate()方法里,当然如果为了启动速度,也可以把初始化放到mainActivity里

  • 这里我使用glide演示
@SuppressLint("CheckResult")
    private void initImageLoader() {
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .encodeQuality(98).dontAnimate();
        Application application = getApplication();
        ImageLoaderImpl.init(new IImageLoader() {
            @Override
            public void load(@NonNull ImageView imageView, @NonNull Object imageUrl) {
                Glide.with(application).load(imageUrl).thumbnail(0.3f).apply(options).into(imageView);
            }

            @Override
            public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int width, int height) {
                options.override(width,height);
                Glide.with(application).load(imageUrl).thumbnail(0.3f).apply(options).into(imageView);
            }

            @Override
            public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int errorImage, int width, int height) {
                options.override(width,height).error(errorImage);
                Glide.with(application).load(imageUrl).thumbnail(0.3f).apply(options).into(imageView);
            }

            @Override
            public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int defaultImage, int errorImage, int width, int height) {
                options.override(width,height).error(errorImage).placeholder(defaultImage);
                Glide.with(application).load(imageUrl).thumbnail(0.3f).apply(options).into(imageView);
            }

            @Override
            public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int defaultImage, int errorImage, int width, int height, @NonNull Object transformation) {
                options.override(width,height).error(errorImage).placeholder(defaultImage).transform((Transformation) transformation);
                Glide.with(application).load(imageUrl).thumbnail(0.3f).apply(options).into(imageView);
            }

            @Override
            public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int errorImage, int width, int height, @NonNull Object transformation) {
                options.override(width,height).error(errorImage).transform((Transformation) transformation);
                Glide.with(application).load(imageUrl).thumbnail(0.3f).apply(options).into(imageView);
            }

            @Override
            public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, int width, int height, @NonNull Object transformation) {
                options.override(width,height).transform((Transformation) transformation);
                Glide.with(application).load(imageUrl).thumbnail(0.3f).apply(options).into(imageView);
            }

            @Override
            public void load(@NonNull ImageView imageView, @NonNull Object imageUrl, @NonNull Object transformation) {
                options.transform((Transformation) transformation);
                Glide.with(application).load(imageUrl).thumbnail(0.3f).apply(options).into(imageView);
            }
        });
    }

你可能感兴趣的:(原创文章,Android用法,glide,图片加载引擎,图片框架封装)