框架Universal-Image-Loader解析
对于一个应用,图片加载几乎是必不可少的,网上也有许多教程,而加载图片我们常常会遇到许多的问题,比如说图片的错乱,OOM等问题,对于刚入门的同学来说,应对起来比较吃力,所以就有很多的开源图片加载框架应运而生,比较著名的就是Universal-Image-Loader,相信很多朋友都听过或者使用过这个强大的图片加载框架.
Universal-Image-Loader优点
- 多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等
- 支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置
- 支持图片的内存缓存,文件系统缓存或者SD卡缓存
- 支持图片下载过程的监听
- 根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存
- 较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中,滑动过程中暂停加载图片,停止滑动的时候去加载图片
- 提供在较慢的网络下对图片进行加载
一 到github下载jar包添加到工程libs目录下
原作者Github下载 /downloads/universal-image-loader-1.9.5.jar
个人备份下载 universal-image-loader-1.9.5.jar
二 重写MyApplication类,初始化参数
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
/**
* Created by wxyass.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//创建默认的ImageLoader配置参数,这里是直接使用了createDefault()方法创建一个默认的
//ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
//创建默认的ImageLoader配置参数
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
//.writeDebugLogs() //打印log信息
.build();
//调用ImageLoader的init()方法将ImageLoaderConfiguration参数传递进去
ImageLoader.getInstance().init(configuration);
}
}
ImageLoaderConfiguration是图片加载器ImageLoader的配置参数,使用了建造者模式,这里是直接使用了createDefault()方法创建一个默认的ImageLoaderConfiguration.
三 配置权限,修改application的name属性
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wxyass.tpuniversalimageloader">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:name="com.wxyass.tpuniversalimageloader.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
四 加载网络图片, 通过ImageLoadingListener加载
// 加载网络图片, 通过ImageLoadingListener (不常用)
private void showPicByImageLoadingListener() {
String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000042.jpg";
ImageLoader.getInstance().loadImage(imageUrl, 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) {
mImageView.setImageBitmap(loadedImage);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
传入图片的url和ImageLoaderListener, 在回调方法onLoadingComplete()中将loadedImage设置到ImageView上面就行了,如果你觉得传入ImageLoaderListener太复杂了,我们可以使用SimpleImageLoadingListener类,该类提供了ImageLoaderListener接口方法的空实现,使用的是缺省适配器模式
五 加载网络图片, 通过SimpleImageLoadingListener加载
// 加载网络图片, 通过SimpleImageLoadingListener
private void showPicBySimpleImageLoadingListener() {
String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000045.jpg";
ImageLoader.getInstance().loadImage(imageUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
mImageView.setImageBitmap(loadedImage);
}
});
}
如果我们要指定图片的大小该怎么办呢,这也好办,初始化一个ImageSize对象,指定图片的宽和高,代码如下
六 加载网络图片, 指定图片的宽和高
// 加载网络图片, 指定图片的宽和高
private void showPicSetSiza() {
String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000051.jpg";
ImageSize mImageSize = new ImageSize(100, 100);
ImageLoader.getInstance().loadImage(imageUrl, mImageSize, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
mImageView.setImageBitmap(loadedImage);
}
});
}
上面只是很简单的使用ImageLoader来加载网络图片,在实际的开发中,我们并不会这么使用,那我们平常会怎么使用呢?我们会用到DisplayImageOptions,他可以配置一些图片显示的选项,比如图片在加载中ImageView显示的图片,是否需要使用内存缓存,是否需要使用文件缓存等等
七 加载网络图片, 配置一些图片显示的选项
// 配置一些图片显示的选项,比如: 图片在加载中ImageView显示的图片,是否需要使用内存缓存,是否需要使用文件缓存等等
private void showPicSetOption() {
String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000056.jpg";
ImageSize mImageSize = new ImageSize(100, 100);
//显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true) // 将图片缓存到内存中
.cacheOnDisk(true) // 将图片缓存到硬盘中
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().loadImage(imageUrl, mImageSize, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
mImageView.setImageBitmap(loadedImage);
}
});
}
我们使用了DisplayImageOptions来配置显示图片的一些选项,这里我添加了将图片缓存到内存中已经缓存图片到文件系统中,这样我们就不用担心每次都从网络中去加载图片了,是不是很方便呢,但是DisplayImageOptions选项中有些选项对于loadImage()方法是无效的,比如showImageOnLoading, showImageForEmptyUri等
八 (最常用)加载网络图片, 通过displayImage加载, 配置一些图片显示的选项
// (最常用)加载网络图片, 通过displayImage加载, 配置一些图片显示的选项
private void showPicBydisplayImage() {
String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000061.jpg";
//显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)// 默认显示的图片
.showImageOnFail(R.drawable.ic_launcher_round)// 加载失败显示的图片
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(imageUrl, mImageView, options);
}
从上面的代码中,我们可以看出,使用displayImage()比使用loadImage()方便很多,也不需要添加ImageLoadingListener接口,我们也不需要手动设置ImageView显示Bitmap对象,直接将ImageView作为参数传递到displayImage()中就行了,图片显示的配置选项中,我们添加了一个图片加载中ImageVIew上面显示的图片,以及图片加载出现错误显示的图片,效果如下,刚开始显示ic_stub图片,如果图片加载成功显示图片,加载产生错误显示ic_error
这个方法使用起来比较方便,而且使用displayImage()方法 他会根据控件的大小和imageScaleType来自动裁剪图片
我们在加载网络图片的时候,经常有需要显示图片下载进度的需求,Universal-Image-Loader当然也提供这样的功能,只需要在displayImage()方法中传入ImageLoadingProgressListener接口就行了,代码如下
九 加载网络图片时候,显示图片下载进度
// 加载网络图片时候,显示图片下载进度
private void showPicProgress() {
String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000067.jpg";
//显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)// 默认显示的图片
.showImageOnFail(R.drawable.ic_launcher_round)// 加载失败显示的图片
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(imageUrl, mImageView, options, new SimpleImageLoadingListener(),
new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
// 展示进度条的代码
}
});
}
由于displayImage()方法中带ImageLoadingProgressListener参数的方法都有带ImageLoadingListener参数,所以我这里直接new 一个SimpleImageLoadingListener,然后我们就可以在回调方法onProgressUpdate()得到图片的加载进度。
使用Universal-Image-Loader框架不仅可以加载网络图片,还可以加载sd卡中的图片,Content provider等,使用也很简单,只是将图片的url稍加的改变下就行了,下面是加载文件系统的图片
十 加载sd卡的图片
// 加载sd卡的图片
private void showPicOnSD() {
//显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)// 默认显示的图片
.showImageOnFail(R.drawable.ic_launcher_round)// 加载失败显示的图片
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
// 本地sd卡路径 /storage/emulated/0
String SdCardpath = Environment.getExternalStorageDirectory() + "";
// 本地图片路径
String imagePath = SdCardpath + "/Download/Pic_000071.jpg";
Log.i("wxyass", imagePath);
String imageUrl = ImageDownloader.Scheme.FILE.wrap(imagePath);
// String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000071.jpg";
ImageLoader.getInstance().displayImage(imageUrl, mImageView, options);
}
十一 加载工程图片,图片来源于drawable (非常不推荐,会有坑)
// 加载工程图片,图片来源于drawable (非常不推荐,会有坑)
private void showPicOnDrawable() {
//显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)// 默认显示的图片
.showImageOnFail(R.drawable.ic_launcher_round)// 加载失败显示的图片
.cacheInMemory(false)
.cacheOnDisk(false)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
//String drawableUrl = ImageDownloader.Scheme.DRAWABLE.wrap("R.drawable.asd");
String drawableUrl = "drawable://" + R.drawable.asd;
// String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000076.jpg";
ImageLoader.getInstance().displayImage(drawableUrl, mImageView, options);
}
十二 加载工程图片,图片来源于assets
// 加载工程图片,图片来源于assets
private void showPicOnAssets() {
//显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)// 默认显示的图片
.showImageOnFail(R.drawable.ic_launcher_round)// 加载失败显示的图片
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
//图片来源于assets
String assetsUrl = ImageDownloader.Scheme.ASSETS.wrap("pic000077.png");
//String assetsUrl = "assets://pic000077.png"; // from assets
// String imageUrl = "http://oss.wxyass.com/private/images/001/Pic_000077.jpg";
ImageLoader.getInstance().displayImage(assetsUrl, mImageView, options);
}
十三 加载工程图片,图片来源于Content provider
十四 参数配置介绍
从上面的几个例子可以看出,需要配置参数的地方主要有两处,一是MyApplication中的ImageLoaderConfiguration参数配置,二是加载图片时的DisplayImageOptions参数配置,关于这两处参数配置网上有许多讲解,用的建造者模式,大家具体去翻一翻看看,有选择的进行配置.
14.1 MyApplication中的ImageLoaderConfiguration参数配置–>可用在MyApplication类
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
.taskExecutor(...)
.taskExecutorForCachedImages(...)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiscCache(cacheDir)) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();
14.2 加载图片时的DisplayImageOptions参数配置–>可用在每个用到加载图片的类中
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub) // resource or drawable
.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
.showImageOnFail(R.drawable.ic_error) // resource or drawable
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(1000)
.cacheInMemory(false) // default
.cacheOnDisk(false) // default
.preProcessor(...)
.postProcessor(...)
.extraForDownloader(...)
.considerExifParams(false) // default
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
.bitmapConfig(Bitmap.Config.ARGB_8888) // default
.decodingOptions(...)
.displayer(new SimpleBitmapDisplayer()) // default
.handler(new Handler()) // default
.build();
最后:项目源码
源码已上传到云端 本文源码下载
参考:
https://github.com/nostra13/Android-Universal-Image-Loader
http://blog.csdn.net/xiaanming/article/details/26810303/
http://blog.csdn.net/vipzjyno1/article/details/23206387
http://www.cnblogs.com/ruiati/p/3930732.html
http://blog.csdn.net/wei18359100306/article/details/41920677
http://blog.csdn.net/tyk0910/article/details/50375070
codekk关于UIL解析