//先初始化,不需要每次加载图片时初始化所以写一个类在开始时就初始化
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
File cacheDir = new File(getExternalCacheDir().getAbsolutePath()+"/image");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.diskCache(new UnlimitedDiskCache(cacheDir)) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.build();
ImageLoader.getInstance().init(config);
}
}
//注意谢了之后要在清单文件当中配置
//加载图片的工具类
public class ImageLodingUtil {
private DisplayImageOptions mOptions;
private ImageLodingUtil() {
//展示时的配置放在构造方法,可以只创建一次即可
mOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.icon_default) // resource or drawable
.delayBeforeLoading(500)
.cacheInMemory(true) // default
.cacheOnDisk(true) // default
.build();
}
private static volatile ImageLodingUtil sInstance;
public static ImageLodingUtil getSingleton() {
if (sInstance == null) {
synchronized (ImageLodingUtil.class) {
if (sInstance == null) {
sInstance = new ImageLodingUtil();
}
}
}
return sInstance;
}
public void showImage(String url,ImageView imageView,int resId){
mOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(resId) // resource or drawable
.delayBeforeLoading(500)
.cacheInMemory(true) // default
.cacheOnDisk(true) // default
.build();
}
ImageLoader.getInstance().displayImage(url,imageView,mOptions);
}
}
==========================================================================================================
介绍另一种Glide
compile 'com.github.bumptech.glide:glide:4.0.0'
简单用法
Glide.with(Context context) .load(String url) .into(imageview);