universal-image-loader实现图片的异步加载与缓存

 universal-image-loader实现图片的异步加载与缓存


在mvp模式中写:在application中初始化,写:

File cacheDirectory = StorageUtils.getOwnCacheDirectory(this, Environment.getExternalStorageDirectory().getPath());


ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this)
        .diskCacheFileCount(500)
        .diskCacheSize(100 * 1024 * 1024)
        .diskCacheFileNameGenerator(new Md5FileNameGenerator())
        .diskCache(new UnlimitedDiskCache(cacheDirectory))
        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
        .threadPoolSize(3)
        .build();

ImageLoader.getInstance().init(imageLoaderConfiguration);

utils中写:

public static void setImage(String url, ImageView imageView){
    DisplayImageOptions build = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.ic_launcher)
            .showImageOnFail(R.mipmap.ic_launcher)
            .cacheOnDisk(true)
            .cacheInMemory(false)
            .build();


    ImageLoader.getInstance().displayImage(url, imageView, build);
}

在适配器中的最后一个set图片的地方写上:

 
  
Utils.setImage(list.get((position % list.size())).getPic(), viewHoder.imageView);






你可能感兴趣的:(universal-image-loader实现图片的异步加载与缓存)