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里
@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);
}
});
}