andorid 学习笔记 UniversalImageLoader的使用

来源:互联网 发布:apache base64 jar包 编辑:程序博客网 时间:2024/06/14 05:21


from : http://blog.csdn.net/yuanlongquan753/article/details/52709658?ref=myread

UniversalImageLoader

问题:

从网络获取图片、显示、回收,每个环节都可能造成应用的OOM(如果没做相关的处理的话)。像ListView或者是GridView中,一页就含有大量的图片,快速滑动的时候,没有OOM也会有卡顿的现象。

解决办法:

使用异步加载网络图片、缓存以及显示。
ImageLoader的工作原理: 

这里写图片描述

  • 最后下载好图片,同时缓存到内存和本地中。
  • 当退出一个页面的时候,内存中ImageLoader的缓存将会全部清除,节省大量内存。
  • ImageLoader对图片的引用形式为软引用,当内不足的时候,将会被GC。

UniversalImageLoader的三大组件

  1. ImageLoadConfiguration: 
    对图片缓存过程进行总体的配置,包括内存缓存大小、本地缓存大小以及缓存的位置,下载策略(FIFO,LIFO)
  2. DisplayImageOptions: 
    控制图片显示的细节,比如指定下载中、下载失败、URL为空等默认图片,是否将图片缓存到内存或者硬盘中。
  3. ImageLoader: 
    在显示图片的地方,比如RecyclerView的Adapter中,使用displayImage把Url对应的图片显示在ImageView上。

使用流程:

github官方文档:https://github.com/nostra13/Android-Universal-Image-Loader/wiki/Quick-Setup
1.  添加库的三种方法

   a.  将jar 包添加到lib目录下面

     b.Maven dependency:  

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <dependency>  
  2.     <groupId>com.nostra13.universalimageloader</groupId>  
  3.     <artifactId>universal-image-loader</artifactId>  
  4.     <version>1.9.5</version>  
  5. </dependency>  
     c.Gradle dependency:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;font-size:14px;color:#333333;">compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'</span>  
2、在Manifest文件中配置相关

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <manifest>  
  2.         <!-- 网络权限 -->  
  3.         <uses-permission android:name="android.permission.INTERNET" />  
  4.         <!-- 缓存图片到SD卡中所使用的权限 -->  
  5.         <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  6.     ...  
  7. </manifest>  

3、 在继承Application的类中或者是在Activity中初始化配置

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class YuZhenApplication extends Application {  
  2.     @Override  
  3.     public void onCreate() {  
  4.         super.onCreate();  
  5.   
  6.         DisplayImageOptions displayImageOptions =  
  7.                 new DisplayImageOptions.Builder()  
  8.                         // 当图片加载中的时候显示的图片,showStubImage()方法已废弃  
  9.                         .showImageOnLoading(R.mipmap.ic_launcher)  
  10.                         // 空url时显示的图片  
  11.                         .showImageForEmptyUri(R.mipmap.ic_launcher)  
  12.                         // 加载图片失败时显示的图片  
  13.                         .showImageOnFail(R.mipmap.ic_launcher)  
  14.                         // 解析Bitmap时使用的编码,默认为ARGB_8888  
  15.                         .bitmapConfig(Bitmap.Config.ARGB_8888)  
  16.                         // 缓存到内存中  
  17.                         .cacheInMemory(true)  
  18.                         // 缓存到本地  
  19.                         .cacheOnDisk(true)  
  20.                         // 设置在加载的前延迟的时间,默认没有延迟  
  21.                         .delayBeforeLoading(0)  
  22.                         .build();  
  23.   
  24.         ImageLoaderConfiguration imageLoaderConfiguration =  
  25.                 new ImageLoaderConfiguration.Builder(getApplicationContext())  
  26.                         // 设置线程的优先级  
  27.                         .threadPriority(Thread.NORM_PRIORITY - 2)  
  28.                         // 将图像解析成Bitmap时内存缓存保留的最大图片宽度和高度,默认为设备屏幕的宽高  
  29.                         .memoryCacheExtraOptions(480480)  
  30.                         // 默认的ImageLoader将会缓存多种尺寸的图片到内存中,调用这个方法禁止这个行为  
  31.                         .denyCacheImageMultipleSizesInMemory()  
  32.                         // 设置加载图片和设置图片的顺序(先进先出、先进后出)  
  33.                         .tasksProcessingOrder(QueueProcessingType.FIFO)  
  34.                         // 设置内存缓存的大小,默认是1/8APP当前可用的内存大小,可以设置为弱引用存储文件大小  
  35.                         // 注:设置完后,memoryCacheSize(int)方法将会被忽略  
  36.                         .memoryCache(new WeakMemoryCache())  
  37.                         // 设置文件名生成器,使用Md5算法生成(discCacheFileNameGenerator()方法已废弃)  
  38.                         .diskCacheFileNameGenerator(new Md5FileNameGenerator())  
  39.                         // 设置默认显示图片时的配置  
  40.                         .defaultDisplayImageOptions(displayImageOptions)  
  41.                         .build();  
  42.   
  43.         ImageLoader.getInstance().init(imageLoaderConfiguration);  
  44.     }  
  45. }  

4. Android Manifest 中添加application

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <application  
  2.         android:allowBackup="true"  
  3.         android:icon="@mipmap/ic_launcher"  
  4.         android:label="@string/app_name"  
  5.         android:supportsRtl="true"  
  6.         android:name=".SkApplication"  
  7.         android:theme="@style/AppTheme">  
  8.   
  9.         <activity  
  10.             android:name=".MainActivity"  
  11.             android:screenOrientation="portrait">  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.   
  15.                 <category android:name="android.intent.category.LAUNCHER" />  
  16.             </intent-filter>  
  17.         </activity>  
  18.   
  19.     </application>  



5. 显示图片的方法

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ImageLoader.getInstance().displayImage(imageUrl,imageView);  


延伸:

优化UniversalImageLoader : http://www.open-open.com/lib/view/open1433940304473.html




0 0