android下载图片代码,从源代码浅析Android-Universal-Image-Loader的图片下载策略

开头就哆嗦两句。相信大家做Android应用项目的时候,多少会接触到异步加载图片,或者加载大量图片的问题,而加载图片我们常常会遇到许多的问题,比如说图片的错乱,OOM等问题。直奔主题吧。

ImageLoader这个开源库常用的几个特征

多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等。

支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置。

支持图片的内存缓存,文件系统缓存或者SD卡缓存。

支持图片下载过程的监听。

根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存。

较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中。

动过程中暂停加载图片。

停止滑动的时候去加载图片。

供在较慢的网络下对图片进行加载。

特征有很多,就列举上面几个。要想了解一些其他的特性只能通过我们的使用慢慢去发现了,使用时多去了解一下其源码。

自定义配置(当然也可以用默认配置),上一份自定义的吧

File cacheDir = StorageUtils.getCacheDirectory(context); //缓存文件夹路径

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)

.memoryCacheExtraOptions(480, 800) // default = device screen dimensions 内存缓存文件的最大长宽

// .diskCacheExtraOptions(480, 800, null) // 本地缓存的详细信息(缓存的最大长宽),最好不要设置这个

.taskExecutor(...)

.taskExecutorForCachedImages(...)

.threadPoolSize(5) // default是3个线程池 线程池内加载的数量

.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 UnlimitedDiscCache(cacheDir)) // default ,可以自定义缓存路径

.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值

.diskCacheFileCount(100) // 可以缓存的文件数量

// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密

.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())

.imageDownloader(new BaseImageDownloader(context)) // default

.imageDecoder(new BaseImageDecoder()) // default

.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default

.writeDebugLogs() // 打印debug log

ImageLoader.getInstance().init(config.build()); //初始化配置,开始构建

使用的方法

/**

* display img

*

* @param defaultResId 默认图片

* @param cacheOnDisk 是否缓存到本地

* @param lowRgb 是否使用RGB_565

* @param roundRadius 圆角程度,0无圆角,>0 有圆角,

*/

public void displayImg(String url, ImageView img, int defaultResId, boolean lowRgb, boolean

cacheOnDisk, int roundRadius) {

try {

Builder loaderBuilder = new DisplayImageOptions.Builder();

loaderBuilder.showImageForEmptyUri(defaultResId);

loaderBuilder.showImageOnFail(defaultResId);

loaderBuilder.showImageOnLoading(defaultResId);

loaderBuilder.cacheInMemory(true);//默认缓存到内存

loaderBuilder.cacheOnDisk(cacheOnDisk);

//显示低像素图片

if (lowRgb) {

loaderBuilder.bitmapConfig(Bitmap.Config.RGB_565);

}

//显示圆角

if (roundRadius > 0) {

loaderBuilder.displayer(new RoundedBitmapDisplayer(roundRadius));

}

//调用ImageLoader的方法

coreImageLoader.displayImage(url, img, loaderBuilder.build());

} catch (Exception e) {

Log.e("--displayImg", e.toString());

}

}

图片下载策略设计与实现

displayImage(...)的源码 如下:

public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,

ImageSize targetSize, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {

checkConfiguration();

if (imageAware == null) {

throw new IllegalArgumentException(ERROR_WRONG_ARGUMENTS);

}

if (listener == null) {

listener = defaultListener;

}

if (options == null) {

options = configuration.defaultDisplayImageOptions;

}

if (TextUtils.isEmpty(uri)) {

engine.cancelDisplayTaskFor(imageAware);

listener.onLoadingStarted(uri, imageAware.getWrappedView());

if (options.shouldShowImageForEmptyUri()) {

imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources));

} else {

imageAware.setImageDrawable(null);

}

listener.onLoadingComplete(uri, imageAware.getWrappedView(), null);

return;

}

if (targetSize == null) {

targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize());

}

String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize);

engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);

listener.onLoadingStarted(uri, imageAware.getWrappedView());

//判断是否有缓存,如果有则读缓存,反之读取网络

Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);

if (bmp != null && !bmp.isRecycled()) {

L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);

if (options.shouldPostProcess()) {

ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,

options, listener, progressListener, engine.getLockForUri(uri));

ProcessAndDisplayImageTask displayTask = new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo,

defineHandler(options));

if (options.isSyncLoading()) {

displayTask.run();

} else {

engine.submit(displayTask);

}

} else {

options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE);

listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);

}

} else {//读取网络图片

if (options.shouldShowImageOnLoading()) {

imageAware.setImageDrawable(options.getImageOnLoading(configuration.resources));

} else if (options.isResetViewBeforeLoading()) {

imageAware.setImageDrawable(null);

}

//把下载或者读取文件缓存资源所需的参数组成ImageLoadingInfo对象

ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,

options, listener, progressListener, engine.getLockForUri(uri));

//加入加载和展览图片的任务栈

LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo,

defineHandler(options));

if (options.isSyncLoading()) {//判断是不是同步加载

displayTask.run();

} else {//异步下载

engine.submit(displayTask);

}

}

}

engine.getLockForUri(uri) ; ReentrantLock 的实现原理

每一个 uri 都会对应加上一个 lock ,因此就不会出现加载多个图片共用一个锁需等待问题。

对比 Synchronize

ReentrantLock getLockForUri(String uri) {

ReentrantLock lock = uriLocks.get(uri);

if (lock == null) {

lock = new ReentrantLock();

uriLocks.put(uri, lock);

}

return lock;

}

engine # submit()方法中出现的 taskExecutorForCachedImages、taskExecutor、taskDistributor 的作用分别是什么?

taskDistributor由于在每创建一个新的线程的时候都需要读取一下磁盘,属于IO操作。需要图片缓存的应用一般在需要加载图片的时候,同时创建很多(>5)线程,这些线程一般来得猛去的也快,存活时间不必太长。

taskExecutor和taskExecutorForCachedImages涉及网络和磁盘的读取和写入操作,比较耗时。主线程数默认为3,实际上IO密集的操作应该定得高一点,以便合理利用CPU的。线程优先级(10为最高,1为最低)为4是比较合理的,因为这些操作只需要后台完成即可,优先级太高可能让界面失去响应。

/** Submits task to execution pool */

void submit(final LoadAndDisplayImageTask task) {

taskDistributor.execute(new Runnable() {

@Override

public void run() {

//取缓存文件图片

File image = configuration.diskCache.get(task.getLoadingUri());

boolean isImageCachedOnDisk = image != null && image.exists();

initExecutorsIfNeed();//如果需要则初始化

if (isImageCachedOnDisk) {

taskExecutorForCachedImages.execute(task);

} else {

taskExecutor.execute(task);

}

}

});

}

为什么需要将三个任务交给不同的 Executor 去执行呢?

这其实这跟线程池的调优有关,如果我们将所有的任务都放在同一个线程池中运行当然是可以的,但是这样的话所有的任务就都只能采取同一种任务优先级和运行策略。显然要有更好的性能,在线程数比较多并且线程承担的任务不同的情况下,App中最好还是按任务的类别来划分线程池。

线程池的分配是由 engine 去管理的。

由 DefaultConfigurationFactory 去创建一个默认的线程池。

如果没有指定图片的加载顺序的话,那么系统默认的顺序是 FIFO 的方式。

这里可以去配置,底层还是使用 LinkedBlockingDeque 去实现的。

具体的数据结构实现 TODO

主要接入点LoadAndDisplayImageTask类

LoadAndDisplayImageTask 的设计与实现

它是一个任务 Runnable 对象。

关注它的 run 方法即可。

给每一个 uri 都配置一把锁。

加载图片。

图片加载完毕之后释放锁。

源码

@Override

public void run() {

if (waitIfPaused()) return;

if (delayIfNeed()) return;

ReentrantLock loadFromUriLock = imageLoadingInfo.loadFromUriLock;

L.d(LOG_START_DISPLAY_IMAGE_TASK, memoryCacheKey);

if (loadFromUriLock.isLocked()) {

L.d(LOG_WAITING_FOR_IMAGE_LOADED, memoryCacheKey);

}

loadFromUriLock.lock();

Bitmap bmp;

try {

checkTaskNotActual();

//先从内存缓存中获取

bmp = configuration.memoryCache.get(memoryCacheKey);

if (bmp == null || bmp.isRecycled()) {

//开始从文件缓存或者网络中加载图片资源

bmp = tryLoadBitmap();

//如果最终获取失败,那么就返回

if (bmp == null) return; //注意此时你自定义的

//在正式使用bitmap之前和放入缓存之前对bitmap进行处理

// listener callback already was fired

checkTaskNotActual();

checkTaskInterrupted();

if (options.shouldPreProcess()) {

L.d(LOG_PREPROCESS_IMAGE, memoryCacheKey);

bmp = options.getPreProcessor().process(bmp);

if (bmp == null) {

L.e(ERROR_PRE_PROCESSOR_NULL, memoryCacheKey);

}

}

//如果使用内存缓存的话,就把加载到的bitmap放入缓存

if (bmp != null && options.isCacheInMemory()) {

L.d(LOG_CACHE_IMAGE_IN_MEMORY, memoryCacheKey);

configuration.memoryCache.put(memoryCacheKey, bmp);

}

} else {

//标志是从内存缓存中读取的资源

loadedFrom = LoadedFrom.MEMORY_CACHE;

L.d(LOG_GET_IMAGE_FROM_MEMORY_CACHE_AFTER_WAITING, memoryCacheKey);

}

//如果在加载完成后的图片仍然需要进行处理的话

if (bmp != null && options.shouldPostProcess()) {

L.d(LOG_POSTPROCESS_IMAGE, memoryCacheKey);

bmp = options.getPostProcessor().process(bmp);

if (bmp == null) {

L.e(ERROR_POST_PROCESSOR_NULL, memoryCacheKey);

}

}

checkTaskNotActual();

checkTaskInterrupted();

} catch (TaskCancelledException e) {

fireCancelEvent();

return;

} finally {

loadFromUriLock.unlock();

}

//创建对象DisplayBitmapTask 最终进行显示。

DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(bmp, imageLoadingInfo, engine, loadedFrom);

runTask(displayBitmapTask, syncLoading, handler, engine);

}

DisplayImageOptions 陈列展览图片的一些配置信息,从信息里的发生判断,加载图片前做些什么。LoadAndDisplayImageTask#run()是关键,然后开始加载图片,主要方法LoadAndDisplayImageTask#tryLoadBitmap()。接着再判断本地缓存是否存在加载过的图片,没有则进入主要方法LoadAndDisplayImageTask#decodeImage()方法。最后获取Bitmap 。

源码

private Bitmap decodeImage(String imageUri) throws IOException {

ViewScaleType viewScaleType = imageAware.getScaleType();

ImageDecodingInfo decodingInfo = new ImageDecodingInfo(memoryCacheKey, imageUri, uri, targetSize, viewScaleType,

getDownloader(), options);

return decoder.decode(decodingInfo);

}

根据网络状态选择下载器策略

为了应对慢速、正常、访问受限网络,UIL分别 使用了SlowNetworkDownloader、BaseImageLoader、NetworkDeniedDownloader来应对这些策略,在LoadAndDisplayImageTask.getDownloader(…)中通过获取对应的downloader,最后通过LoadAndDisplayImageTask.decodeImage(…)将图片解析出来。其中 SlowNetworkDownloader 和 NetworkDeniedDownloader 对 BaseDownloader 的包装。

源码分析

private ImageDownloader getDownloader() {

ImageDownloader d;

if (engine.isNetworkDenied()) {//网络受限

d = networkDeniedDownloader;

} else if (engine.isSlowNetwork()) {//网速慢

d = slowNetworkDownloader;

} else {//正常

d = downloader;

}

return d;

}

DisplayTask 的设计与实现

它负责展示 bitmap 到 对应的 imageview 上。

handler 是通过 options 中配置的。

sync 的情况下是直接调用 r.run() 方法直接运行。

handler 为空,也就是用户没有设置 handler 或者是 sync 的方式,并且 displayImage 又不是在主线程中调用的,那么就由 engine 分配线程池去执行这个任务。不过这里会出现问题:Can't set a drawable into view. You should call ImageLoader on UI thread for it.

若是用户设置了 handler 则由 handler 去执行这个 task。

static void runTask(Runnable r, boolean sync, Handler handler, ImageLoaderEngine engine) {

if (sync) {

r.run();

} else if (handler == null) {

engine.fireCallback(r);

} else {

handler.post(r);

}

}

由于多线程 、线程池 、DisplayOptions 的设计与实现 、处理图片不会 OOM 问题还没有了解,有时间研究了再写,有问题请多指教,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值