Android-Universal-Image-Loader是一个非常强大的开源的图片异步加载库,提供一个可重复使用的仪器为异
步图像加载,缓存和显示,而且使用也非常简单。使用它我们可以少考虑加载图片遇到的多线程,缓存,内存溢出等
很多方面的问题。国内外很多有名的应用程序都有使用,在该类库的默认缓存文件夹中甚至发现了google,
instagram, qq, baidu都有在用。 Android-Universal-Image-Loader的代码托管在GitHub上:
https://github.com/nostra13/Android-Universal-Image-Loader
本文已同步到个人对立域名博客:http://www.fengchaokm.com/
加载显示任务流
多线程的图像加载
尽可能多的配置选项(线程池,加载器,解析器,内存/磁盘缓存,显示参数等等)
图片可以缓存在内存中,或者设备文件目录下,或者SD卡中
可以添加图片加载监听器
可以自定义显示每一张图片时都带不同参数
支持Widget
Android 1.5以上支持
1.9.4+版本添加了一个新的API:DisplayImageOptions.targetSize(ImageSize),提供可设置图片的大小。
通过网络加载图片涉及到网络请求权限
<code class="hljs xml has-numbering"><span style="font-size:18px;"><span style="font-family:SimSun;"><span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.INTERNET"</span> /></span> </span></span></code>
缓存设置需要设置权限
<code class="hljs xml has-numbering"><span style="font-size:18px;"><span style="font-family:SimSun;"><span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.WRITE_EXTERNAL_STORAGE"</span> /></span> </span></span></code>
<code class="hljs cs has-numbering"><span style="font-size:18px;"><span style="font-family:SimSun;"><span class="hljs-string">"http://site.com/image.png"</span> <span class="hljs-comment">// from Web 从网络获取</span> <span class="hljs-string">"file:///mnt/sdcard/image.png"</span> <span class="hljs-comment">// from SD card 从SD卡获取</span> <span class="hljs-string">"file:///mnt/sdcard/video.mp4"</span> <span class="hljs-comment">// from SD card (video thumbnail)</span> <span class="hljs-string">"content://media/external/images/media/13"</span><span class="hljs-comment">//from content provider 从内容提供器获取</span> <span class="hljs-string">"content://media/external/video/media/13"</span> <span class="hljs-comment">// from content provider (video thumbnail)</span> <span class="hljs-string">"assets://image.png"</span> <span class="hljs-comment">// from assets 从assets目录获取</span> <span class="hljs-string">"drawable://"</span> + R.drawable.img <span class="hljs-comment">// from drawables (non-9patch images)</span></span></span></code>
3、MyApplication class
<code class="hljs avrasm has-numbering"><span style="font-family:SimSun;font-size:18px;">public class MyApplication extends Application { @Override public void onCreate() { super<span class="hljs-preprocessor">.onCreate</span>()<span class="hljs-comment">;</span> File cacheDir = StorageUtils<span class="hljs-preprocessor">.getCacheDirectory</span>(getApplicationContext())<span class="hljs-comment">;</span> Environment<span class="hljs-preprocessor">.getExternalStorageDirectory</span>()<span class="hljs-preprocessor">.getPath</span>())<span class="hljs-comment">;</span> //ImageLoaderConfiguration 所有的选项都是可选的,只选择你真正想制定的去配置 ImageLoaderConfiguration config = new ImageLoaderConfiguration<span class="hljs-preprocessor">.Builder</span>( getApplicationContext()) // 如果图片尺寸大于了这个参数,那么就会这按照这个参数对图片大小进行限制并缓存 <span class="hljs-preprocessor">.memoryCacheExtraOptions</span>(<span class="hljs-number">480</span>, <span class="hljs-number">800</span>) <span class="hljs-preprocessor">.taskExecutor</span>(AsyncTask<span class="hljs-preprocessor">.sExecutor</span>) <span class="hljs-preprocessor">.taskExecutorForCachedImages</span>(AsyncTask<span class="hljs-preprocessor">.sExecutor</span>) <span class="hljs-preprocessor">.threadPoolSize</span>(<span class="hljs-number">5</span>) <span class="hljs-preprocessor">.threadPriority</span>(Thread<span class="hljs-preprocessor">.NORM</span>_PRIORITY-<span class="hljs-number">2</span>) <span class="hljs-preprocessor">.threadPriority</span>(Thread<span class="hljs-preprocessor">.NORM</span>_PRIORITY - <span class="hljs-number">1</span>) <span class="hljs-preprocessor">.tasksProcessingOrder</span>(QueueProcessingType<span class="hljs-preprocessor">.FIFO</span>) <span class="hljs-preprocessor">.denyCacheImageMultipleSizesInMemory</span>() <span class="hljs-preprocessor">.memoryCache</span>(new LruMemoryCache(<span class="hljs-number">2</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>)) <span class="hljs-preprocessor">.memoryCacheSize</span>(<span class="hljs-number">2</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>) <span class="hljs-preprocessor">.diskCache</span>(new UnlimitedDiscCache(cacheDir)) <span class="hljs-preprocessor">.diskCacheSize</span>(<span class="hljs-number">50</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>) <span class="hljs-preprocessor">.diskCacheFileCount</span>(<span class="hljs-number">100</span>) <span class="hljs-preprocessor">.writeDebugLogs</span>() <span class="hljs-preprocessor">.build</span>()<span class="hljs-comment">;</span> ImageLoader<span class="hljs-preprocessor">.getInstance</span>()<span class="hljs-preprocessor">.init</span>(config)<span class="hljs-comment">;</span> } </span></code>
Display Options
显示参数可以分别被每一个显示任务调用(ImageLoader.displayImage(…))
<code class="hljs r has-numbering"><span style="font-family:SimSun;font-size:18px;">DisplayImageOptions options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.ic_stub) // 在显示真正的图片前,会加载这个资源 .showImageForEmptyUri(R.drawable.ic_empty) //空的Url时 .showImageOnFail(R.drawable.ic_error) .resetViewBeforeLoading() // .delayBeforeLoading(<span class="hljs-number">1000</span>) // 延长1000ms 加载图片 (想不出来用在什么场景下) .cacheInMemory() .cacheOnDisc() .preProcessor(<span class="hljs-keyword">...</span>) .postProcessor(<span class="hljs-keyword">...</span>) .extraForDownloader(<span class="hljs-keyword">...</span>) //可以向加载器携带一些参数 .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default .bitmapConfig(Bitmap.Config.ARGB_8888) // default .decodingOptions(<span class="hljs-keyword">...</span>) .displayer(new SimpleBitmapDisplayer()) // default .handler(new Handler()) // default .build();</span></code>
简单实例
<code class="hljs java has-numbering"><span style="font-size:18px;"><span style="font-family:SimSun;"><span class="hljs-comment">//无回调方法</span> imageLoader.displayImage(imageUri, imageView); <span class="hljs-comment">// 有回调方法</span> imageLoader.loadImage(imageUri, <span class="hljs-keyword">new</span> SimpleImageLoadingListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onLoadingComplete</span>(String imageUri, View view, Bitmap loadedImage) { <span class="hljs-comment">// Do whatever you want with Bitmap</span> } }); <span class="hljs-comment">// 返回Bitmap对象</span> Bitmap bmp = imageLoader.loadImageSync(imageUri);</span></span></code>
完整版
<code class="hljs r has-numbering"><span style="font-family:SimSun;font-size:18px;">// Load image, decode it to Bitmap and display Bitmap <span class="hljs-keyword">in</span> ImageView (or any other view // which implements ImageAware interface) imageLoader.displayImage(imageUri, imageView, options, new ImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { <span class="hljs-keyword">...</span> } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { <span class="hljs-keyword">...</span> } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { <span class="hljs-keyword">...</span> } @Override public void onLoadingCancelled(String imageUri, View view) { <span class="hljs-keyword">...</span> } }, new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { <span class="hljs-keyword">...</span> } }); // Load image, decode it to Bitmap and <span class="hljs-keyword">return</span> Bitmap to callback ImageSize targetSize = new ImageSize(<span class="hljs-number">80</span>, <span class="hljs-number">50</span>); // result Bitmap will be fit to this size imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() { @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { // Do whatever you want with Bitmap } }); // Load image, decode it to Bitmap and <span class="hljs-keyword">return</span> Bitmap synchronously ImageSize targetSize = new ImageSize(<span class="hljs-number">80</span>, <span class="hljs-number">50</span>); // result Bitmap will be fit to this size Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, options);</span></code>