本文出自xiaanming的博客(http://blog.youkuaiyun.com/xiaanming/article/details/26810303)
相信大家平时做Android应用的时候,多少会接触到异步加载图片,或者加载大量图片的问题,而加载图片我们常常会遇到许多的问题,比如说图片的错乱,OOM等问题,对于新手来说,这些问题解决起来会比较吃力,所以就有很多的开源图片加载框架应运而生,比较著名的就是Universal-Image-Loader,相信很多朋友都听过或者使用过这个强大的图片加载框架,今天这篇文章就是对这个框架的基本介绍以及使用,主要是帮助那些没有使用过这个框架的朋友们。该项目存在于Github上面https://github.com/nostra13/Android-Universal-Image-Loader,我们可以先看看这个开源库存在哪些特征
- 多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等
- 支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置
- 支持图片的内存缓存,文件系统缓存或者SD卡缓存
- 支持图片下载过程的监听
- 根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存
- 较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中,滑动过程中暂停加载图片,停止滑动的时候去加载图片
- 提供在较慢的网络下对图片进行加载
当然上面列举的特性可能不全,要想了解一些其他的特性只能通过我们的使用慢慢去发现了,接下来我们就看看这个开源库的简单使用吧
新建一个Android项目,下载JAR包添加到工程libs目录下
新建一个MyApplication继承Application,并在onCreate()中创建ImageLoader的配置参数,并初始化到ImageLoader中代码如下
- package com.example.uil;
-
- import com.nostra13.universalimageloader.core.ImageLoader;
- import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
-
- import android.app.Application;
-
- public class MyApplication extends Application {
-
- @Override
- public void onCreate() {
- super.onCreate();
-
-
- ImageLoaderConfiguration configuration = ImageLoaderConfiguration
- .createDefault(this);
-
-
- ImageLoader.getInstance().init(configuration);
- }
-
- }
ImageLoaderConfiguration是图片加载器ImageLoader的配置参数,使用了建造者模式,这里是直接使用了createDefault()方法创建一个默认的ImageLoaderConfiguration,当然我们还可以自己设置ImageLoaderConfiguration,设置如下
- File cacheDir = StorageUtils.getCacheDirectory(context);
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
- .memoryCacheExtraOptions(480, 800)
- .diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
- .taskExecutor(...)
- .taskExecutorForCachedImages(...)
- .threadPoolSize(3)
- .threadPriority(Thread.NORM_PRIORITY - 1)
- .tasksProcessingOrder(QueueProcessingType.FIFO)
- .denyCacheImageMultipleSizesInMemory()
- .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
- .memoryCacheSize(2 * 1024 * 1024)
- .memoryCacheSizePercentage(13)
- .diskCache(new UnlimitedDiscCache(cacheDir))
- .diskCacheSize(50 * 1024 * 1024)
- .diskCacheFileCount(100)
- .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
- .imageDownloader(new BaseImageDownloader(context))
- .imageDecoder(new BaseImageDecoder())
- .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
- .writeDebugLogs()
- .build();
上面的这些就是所有的选项配置,我们在项目中不需要每一个都自己设置,一般使用createDefault()创建的ImageLoaderConfiguration就能使用,然后调用ImageLoader的init()方法将ImageLoaderConfiguration参数传递进去,ImageLoader使用单例模式。
配置Android Manifest文件
- <manifest>
- <uses-permission android:name="android.permission.INTERNET" />
-
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- ...
- <application android:name="MyApplication">
- ...
- </application>
- </manifest>
接下来我们就可以来加载图片了,首先我们定义好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>
里面只有一个ImageView,很简单,接下来我们就去加载图片,我们会发现ImageLader提供了几个图片加载的方法,主要是这几个displayImage(), loadImage(),loadImageSync(),loadImageSync()方法是同步的,android4.0有个特性,网络操作不能在主线程,所以loadImageSync()方法我们就不去使用
.
loadimage()加载图片
我们先使用ImageLoader的loadImage()方法来加载网络图片
- final ImageView mImageView = (ImageView) findViewById(R.id.image);
- String imageUrl = "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.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接口方法的空实现,使用的是缺省适配器模式
- final ImageView mImageView = (ImageView) findViewById(R.id.image);
- String imageUrl = "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg"; <li class="alt" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px%2