ImageLoader一个强大的图片加载框架,下面请跟我一起进入它的简单使用。
简单的步骤
- 多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等
- 支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置
- 支持图片的内存缓存,文件系统缓存或者SD卡缓存
- 支持图片下载过程的监听
- 根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存
- 较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中,滑动过程中暂停加载图片,停止滑动的时候去加载图片
- 提供在较慢的网络下对图片进行加载
/* 自定义APpLication的步骤 1.自定义一个类,继承 2.清单文注册自定义的Application
* applciation:先于androidAPP的组件存在,时对这个应用的信息配置.**/
配置Android Manifest文件
- <manifest>
- <uses-permission android:name="android.permission.INTERNET" />
- <!-- Include next permission if you want to allow UIL to cache images on SD card -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- ...
- <application android:name="MyApplication"> <!配置applciation>
- ...
- </application>
- </manifest>
//配置jar包,或者添加依赖
供参考的依赖:compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
@Override
public void onCreate() {
super.onCreate();
//初始化ImageLoader设置
initImageLoader();
}
private void initImageLoader() {
//设置ImageLoader的配置信息
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(APP.this)
.memoryCacheExtraOptions(480, 800) //缓存图片最大的长和宽
.threadPriority(2) //线程池的数量
.memoryCacheSizePercentage(4) // default
.memoryCacheSize(2*1024*1024) //设置内存缓存区的大小
.diskCacheSize(50 * 1024 * 1024) //设置SD卡缓存区大小
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() //打开日志neirong
.build();
//通过单例的模式,拿到对象,设置我们自定义配置的ImageLoader的配置信息
ImageLoader.getInstance().init(config);
}
public static DisplayImageOptions getOption(){
//创建对象,又叫链式调用法.
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_loading) //设置图片下载期间显示的图片
.showImageForEmptyUri(R.drawable.ic_empty) //设置图片url为null的时候显示的图片
.showImageOnFail(R.drawable.ic_error) // 设置图片uri有错误,解密的过程中发生错误显示的图片
.delayBeforeLoading(1000)
.cacheInMemory(true) //设置下载的图片是否缓存到内存中
.cacheOnDisk(true) // 设置下载的图片是否缓存到SD卡中
.build();
return options;
}
接下来我们就可以来加载图片了,首先我们定义好Activity的布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:layout_gravity="center"
- android:id="@+id/image"
- android:src="@drawable/ic_empty"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </FrameLayout>
//控件
String path = "http://t2.27270.com/uploads/tu/201610/198/uxkqdrirglw.jpg";
//拿到ImageLoader的对象
ImageLoader imageLoader = ImageLoader.getInstance();
A,B,C,D各代表一个方法加载图片,选择一个即可
//A.ImageLoader的基本使用,环境配置好,一行代码加载网络图片 参数1.URL图片网址,String类型 2.控件对象
// imageLoader.displayImage(path,imageView);
//applciation类,调用类中方法
APP app = new APP();
DisplayImageOptions options = app.getOption();
//B.ImageLoader加载网络图片(带参数设置)
// imageLoader.displayImage(path,imageView,options);
//C.ImageLoader加载网络图片(带参数设置),自带监听回调
/* imageLoader.displayImage(path, imageView, options, new ImageLoadingListener() {
//加载图片开始回调
@Override
public void onLoadingStarted(String imageUri, View view) {
}
//加载图片失败回调
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
//加载图片成功的时候回调
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
//加载图片取消的时候回调
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});*/
//D.ImageLoader加载网络图片(带参数设置),自带监听回调,同时可以显示加载进度的回调
imageLoader.displayImage(path, imageView, options, new ImageLoadingListener() {
//加载图片开始回调
@Override
public void onLoadingStarted(String imageUri, View view) {
}
//加载图片失败回调
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
//加载图片成功的时候回调
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
//加载图片取消的时候回调
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
}, //加载图片的进度条的回调
new ImageLoadingProgressListener() {
//参数1.网址 2.ImageView控件对象
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
System.out.println("控件"+view);
}
});
}
1520




被折叠的 条评论
为什么被折叠?



