参考:http://blog.csdn.net/vipzjyno1/article/details/23206387
一、GITHUB下载文件
路径:https://github.com/nostra13/Android-Universal-Image-Loader
把三个jar包放在工程目录下
二、AndroidManifest.xml设置
2.1 权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.2 application
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.hy.challenge.applaction.ChallengeApplication"
>
三、java实现
3.1 application初始化相关
public class ChallengeApplication extends Application{
private Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
initImageLoder();
}
/**
* 初始化ImageLoder库
*/
private void initImageLoder(){
// 缓存路径
File cacheDir = StorageUtils.getOwnCacheDirectory(context, "imageloader/Cache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(getApplicationContext())
// .memoryCacheExtraOptions(480, 800) // 不用设置,尺寸在布局中定义
.threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.discCacheSize(50 * 1024 * 1024)
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.discCacheFileCount(100)
.discCache(new UnlimitedDiskCache(cacheDir))
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000))
.writeDebugLogs()
.build();
// 调用初始化
ImageLoader.getInstance().init(config);
}
}
3.2 使用
3.2.1 获取实例
ImageLoader imageLoader = ImageLoader.getInstance();
3.2.2 使用方式一
imageLoader.loadImage(imageUrl, options, new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
imageView.setImageBitmap(loadedImage);
super.onLoadingComplete(imageUri, view, loadedImage);
}});
3.2.3 使用方式二(建议)
imageLoader.displayImage(imageUrl, imageView);
四、效果