ImageLoader依赖
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
1.简单配置
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext())
.diskCache(new UnlimitedDiskCache(new File(String.valueOf(getCacheDir().getAbsolutePath()))))
.diskCacheSize(10*1024)
.memoryCache(new LruMemoryCache(10*1024))
.memoryCacheSize(10*1024)
.build();
ImageLoader.getInstance().init(configuration);
}
}
DisplayImageOptions obtions = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.build();
2.详细配置
package com.example.myapplication.application;
import android.app.Application;
import android.graphics.Bitmap;
import com.example.myapplication.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import java.io.File;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
initLoder();
}
//初始化ImageLoder
private void initLoder() {
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
// See the sample project how to use ImageLoader correctly.
//缓存目录
File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "/images");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, null)
.threadPoolSize(3) // default线程池数量
.threadPriority(Thread.NORM_PRIORITY - 2) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiskCache(cacheDir)) // default内存卡缓存
.diskCacheSize(50 * 1024 * 1024)//内存卡大小
.diskCacheFileCount(100)//缓存文件数量
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
//.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.defaultDisplayImageOptions(createDisplayOption())
.writeDebugLogs()
.build();
ImageLoader.getInstance().init(config);
}
private DisplayImageOptions createDisplayOption() {
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
// See the sample project how to use ImageLoader correctly.
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher_background) // resource or drawable
.showImageForEmptyUri(R.drawable.ic_launcher_background) // resource or drawable
.showImageOnFail(R.drawable.ic_launcher_background) // resource or drawable
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(1000)
.cacheInMemory(true) // default是否开启内存缓存
.cacheOnDisk(true) // default是否开启SD卡缓存
.considerExifParams(false) // default
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
.bitmapConfig(Bitmap.Config.RGB_565) // default
//.displayer(new SimpleBitmapDisplayer()) // default
.displayer(new CircleBitmapDisplayer())
//.displayer(new RoundedBitmapDisplayer(40))
.build();
return options;
}
}