1.功能概要

Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。

(1).使用多线程加载图片
(2).灵活配置ImageLoader的基本参数,包括线程数、缓存方式、图片显示选项等;
(3).图片异步加载缓存机制,包括内存缓存及SDCard缓存;
(4).采用监听器监听图片加载过程及相应事件的处理;
(5).配置加载的图片显示选项,比如图片的圆角处理及渐变动画。

2.简单实现

ImageLoader采用单例设计模式,ImageLoader imageLoader = ImageLoader.getInstance();得到该对象,每个ImageLoader采用单例设计模式,ImageLoader必须调用init()方法完成初始化。

  1. //  1.完成ImageLoaderConfiguration的配置
  2. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
  3. .memoryCacheExtraOptions(480, 800)          // default = device screen dimensions
  4. .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
  5. .taskExecutor(...)
  6. .taskExecutorForCachedImages(...)
  7. .threadPoolSize(3)                          // default
  8. .threadPriority(Thread.NORM_PRIORITY - 1)   // default
  9. .tasksProcessingOrder(QueueProcessingType.FIFO) // default
  10. .denyCacheImageMultipleSizesInMemory()
  11. .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
  12. .memoryCacheSize(2 * 1024 * 1024)
  13. .memoryCacheSizePercentage(13)              // default
  14. .discCache(new UnlimitedDiscCache(cacheDir))// default
  15. .discCacheSize(50 * 1024 * 1024)        // 缓冲大小
  16. .discCacheFileCount(100)                // 缓冲文件数目
  17. .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
  18. .imageDownloader(new BaseImageDownloader(context)) // default
  19. .imageDecoder(new BaseImageDecoder()) // default
  20. .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
  21. .writeDebugLogs()
  22. .build();
  23. //  2.单例ImageLoader类的初始化
  24. ImageLoader imageLoader = ImageLoader.getInstance();
  25. imageLoader.init(config);
  26. //  3.DisplayImageOptions实例对象的配置
  27. //      以下的设置再调用displayImage()有效,使用loadImage()无效
  28. DisplayImageOptions options = new DisplayImageOptions.Builder()
  29. .showStubImage(R.drawable.ic_stub)          // image在加载过程中,显示的图片
  30. .showImageForEmptyUri(R.drawable.ic_empty)  // empty URI时显示的图片
  31. .showImageOnFail(R.drawable.ic_error)       // 不是图片文件 显示图片
  32. .resetViewBeforeLoading(false)  // default
  33. .delayBeforeLoading(1000)
  34. .cacheInMemory(false)           // default 不缓存至内存
  35. .cacheOnDisc(false)             // default 不缓存至手机SDCard
  36. .preProcessor(...)
  37. .postProcessor(...)
  38. .extraForDownloader(...)
  39. .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)// default
  40. .bitmapConfig(Bitmap.Config.ARGB_8888)              // default
  41. .decodingOptions(...)
  42. .displayer(new SimpleBitmapDisplayer()) // default 可以设置动画,比如圆角或者渐变
  43. .handler(new Handler())                             // default
  44. .build();
  45. //  4图片加载
  46. //  4.1 调用displayImage
  47. imageLoader.displayImage(
  48. uri,        /*
  49. String imageUri = "http://site.com/image.png";      // from Web
  50. String imageUri = "file:///mnt/sdcard/image.png";   // from SD card
  51. String imageUri = "content://media/external/audio/albumart/13"; // from content provider
  52. String imageUri = "assets://image.png";             // from assets
  53. */
  54. imageView,      // 对应的imageView控件
  55. options);       // 与之对应的image显示方式选项
  56. //  4.2 调用loadImage
  57. //      对于部分DisplayImageOptions对象的设置不起作用
  58. imageLoader.loadImage(
  59. uri,
  60. options,
  61. new MyImageListener()); //ImageLoadingListener
  62. class MyImageListener extends SimpleImageLoadingListener{
  63. @Override
  64. public void onLoadingStarted(String imageUri, View view) {
  65. imageView.setImageResource(R.drawable.loading);
  66. super.onLoadingStarted(imageUri, view);
  67. }
  68. @Override
  69. public void onLoadingFailed(String imageUri, View view,
  70. FailReason failReason) {
  71. imageView.setImageResource(R.drawable.no_pic);
  72. super.onLoadingFailed(imageUri, view, failReason);
  73. }
  74. @Override
  75. public void onLoadingComplete(String imageUri, View view,
  76. Bitmap loadedImage) {
  77. imageView.setImageBitmap(loadedImage);
  78. super.onLoadingComplete(imageUri, view, loadedImage);
  79. }
  80. @Override
  81. public void onLoadingCancelled(String imageUri, View view) {
  82. imageView.setImageResource(R.drawable.cancel);
  83. super.onLoadingCancelled(imageUri, view);
  84. }
  85. }

3.支持的Uri

  1. String imageUri = "http://site.com/image.png";      // from Web
  2. String imageUri = "file:///mnt/sdcard/image.png";   // from SD card
  3. String imageUri = "content://media/external/audio/albumart/13"; // from content provider
  4. String imageUri = "assets://image.png";             // from assets
  5. String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)

加载drawables下图片,可以通过ImageView.setImageResource(...) 而不是通过上面的ImageLoader.

4.缓冲至手机

默认不能保存缓存,必须通过下面的方式指定

  1. DisplayImageOptions options = new DisplayImageOptions.Builder()
  2. ...
  3. .cacheInMemory(true)
  4. .cacheOnDisc(true)
  5. ...
  6. .build();
  7. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
  8. ...
  9. .defaultDisplayImageOptions(options)
  10. ...
  11. .build();
  12. ImageLoader.getInstance().init(config); // Do it on Application start
  13. ImageLoader.getInstance().displayImage(imageUrl, imageView);    /*
  14. 默认为defaultDisplayImageOptions设定的options对象,此处不用指定options对象 */

或者通过下面这种方式

  1. DisplayImageOptions options = new DisplayImageOptions.Builder()
  2. ...
  3. .cacheInMemory(true)
  4. .cacheOnDisc(true)
  5. ...
  6. .build();
  7. ImageLoader.getInstance().displayImage(imageUrl, imageView, options); //此处指定options对象

由于缓存需要在外设中写入数据,故需要添加下面的权限

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

5.OutOfMemoryError

如果OutOfMemoryError错误很常见,可以通过下面的方式设置
(1).减少configuration中线程池的线程数目(.threadPoolSize(...)) 推荐为1 - 5
(2).display options通过.bitmapConfig(Bitmap.Config.RGB_565)设置. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.
(3).使用configuration的memoryCache(new WeakMemoryCache())方法 或者不调用.cacheInMemory()方法
(4).display options通过.imageScaleType(ImageScaleType.IN_SAMPLE_INT) 或者 .imageScaleType(ImageScaleType.EXACTLY)方法
(4).避免使用RoundedBitmapDisplayer,它创建了一个新的ARGB_8888 Bitmap对象

6.内存缓存管理

通过imageLoaderConfiguration.memoryCache([new LruMemoryCache(1)]))对手机内存缓存进行管理

LruMemoryCache

API >= 9默认.it is moved to the head of a queue.

FreqLimitedMemoryCache

当超过缓存大小后,删除最近频繁使用的bitmap

LRULimitedMemoryCache

API < 9 默认.当超过缓存大小后,删除最近使用的bitmap

FIFOLimitedMemoryCache

FIFO rule is used for deletion when cache size limit is exceeded

LargestLimitedMemoryCache

The largest bitmap is deleted when cache size limit is exceeded

WeakMemoryCache

Unlimited cache

7.SDcard缓存管理

通过imageLoaderConfiguration.discCache([new TotalSizeLimitedDiscCache()]))对SD卡缓存进行管理

UnlimitedDiscCache

default The fastest cache, doesn't limit cache size

TotalSizeLimitedDiscCache

Cache limited by total cache size. If cache size exceeds specified limit then file with themost oldest lastusage date will be deleted

FileCountLimitedDiscCache

Cache limited by file count. If file count in cache directory exceeds specified limit then file with the most oldest last usage date will be deleted.

LimitedAgeDiscCache

Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.

UnlimitedDiscCache is 30%-faster than other limited disc cache implementations

转自:http://www.cnblogs.com/wanqieddy/p/3836485.html

AndroidUniversalImageLoader网络图片加载的更多相关文章

  1. android--------Universal-Image-Loader图片加载框架和结合LruCache缓存图片

    本博客包含包含Android-Universal-Image-Loader 网络图片加载框架实现图片加载和结合universal-image-loader与LruCache来自定义缓存图片,可以设置缓 ...

  2. Android之网络图片加载的5种基本方式

    学了这么久,最近有空把自己用到过的网络加载图片的方式总结了出来,与大家共享,希望对你们有帮助. 此博客包含Android 5种基本的加载网络图片方式,包括普通加载HttpURLConnection.H ...

  3. 55、Android网络图片 加载缓存处理库的使用

         先来一个普通的加载图片的方法. import android.annotation.SuppressLint; import android.app.Activity; import and ...

  4. Android 开发 框架系列 Android-Universal-Image-Loader 图片加载使用demo

    Android-Universal-Image-Loader github地址:https://github.com/nostra13/Android-Universal-Image-Loader 加 ...

  5. android ImageView网络图片加载、动态设置尺寸、圆角..

    封装了一个关于ImageView的辅助类,该类可以方便实现网络图片下载的同时,动态设置图片尺寸.圆角.....一系列连贯的操作,无样式表,java代码实现所有功能,使用很方便. package com ...

  6. ★android开发--ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)

    例子中用于解析Json的Gson请自己Google下载 主Activity: package COM.Example.Main; import java.util.HashMap; import ja ...

  7. android 开发 - 网络图片加载库 Fresco 的使用。

    概述 Fresco 是 facebook 的开源类库,它支持更有效的加载网络图片以及资源图片.它自带三级缓存功能,让图片显示更高效. 介绍 Fresco 是一个强大的图片加载组件. Fresco 中设 ...

  8. IOS开发笔记 - 基于SDWebImage的网络图片加载处理

    前言: 在IOS下通过URL读一张网络图片并不像Asp.net那样可以直接把图片路径放到图片路径的位置就ok, 而是需要我们通过一段类似流的方式去加载网络图片,接着才能把图片放入图片路径显示. 这里找 ...

  9. Android_开源框架_AndroidUniversalImageLoader网络图片加载

    1.功能概要 Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示. (1).使用多线程加载图片(2) ...

随机推荐

  1. BZOJ 1003: [ZJOI2006]物流运输trans DP+最短路

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  2. COGS 577 蝗灾 [CDQ分治入门题]

    题目链接 昨天mhr神犇,讲分治时的CDQ分治的入门题. 题意: 你又一个w*w正方形的田地. 初始时没有蝗虫. 给你两个操作: 1. 1 x y z: (x,y)这个位置多了z只蝗虫. 2. 2 x ...

  3. 【技术贴】解决 myeclipse打不开报错an error has occurred, see .

    方法1.右键选中快捷方式属性选项,在快捷方式页,目标一项最后加上-clean选项,如C:\MyEclipse6\eclipse.exe -clean. 然后重新启动一下MyEclipse. 方法2. ...

  4. ruby条件控制结构

    一.比较语句 大部分和其他的语言一样,这里注意<=>. 条件语句 如下几种形式 if if ..else.. end if..elsif..else..end unless(if not) ...

  5. Python urllib2写爬虫时候每次request open以后一定要关闭

    最近用python urllib2写一个爬虫工具,碰到运行一会程序后就会出现scoket connection peer reset错误.经过多次试验发现原来是在每次request open以后没有及 ...

  6. 一起啃PRML - 1.1 Example: Polynomial Curve Fitting 多项式曲线拟合

    一起啃PRML - 1.1 Example: Polynomial Curve Fitting @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ 前言: ...

  7. java 枚举(括号赋值)

    详解在这里 要注意的是: 1. 通过括号赋值,而且必须带有一个参构造器和一个属性跟方法,否则编译出错2. 赋值必须都赋值或都不赋值,不能一部分赋值一部分不赋值:如果不赋值则不能写构造器,赋值编译也出错 ...

  8. selenium webdriver(1)---浏览器操作

    启动浏览器 如何启动浏览器已在上篇文章中说明,这里还是以chrome为例,firefox.IE启动方式相同. //启动浏览器 import org.openqa.selenium.WebDriver; ...

  9. Bzoj 2662: [BeiJing wc2012]冻结 dijkstra,堆,分层图,最短路

    2662: [BeiJing wc2012]冻结 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 647  Solved: 348[Submit][Sta ...

  10. Bzoj 1532: [POI2005]Kos-Dicing 二分,网络流

    1532: [POI2005]Kos-Dicing Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1373  Solved: 444[Submit][St ...