开源项目Universal Image Loader for Android 注意事项

来源:互联网 发布:淘宝模特米雪儿丝袜 编辑:程序博客网 时间:2024/04/26 05:16

最简单的使用示例代码

<span style="white-space:pre"></span><pre name="code" class="java">// 1.获取ImageLoader实例ImageLoader imageLoader = ImageLoader.getInstance();// 2. 使用默认配置ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);// 3. 初始化ImageLoaderimageLoader.init(configuration);// 4. 显示图片时的配置displayImageOptions = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc().bitmapConfig(Config.RGB_565).build();// 5.显示图片imageLoader.displayImage(uri, imageView, displayImageOptions);

</pre><pre code_snippet_id="645449" snippet_file_name="blog_20150416_3_1394129" name="code" class="java">初始化的参考代码
<pre name="code" class="java">ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory().diskCacheFileNameGenerator(new Md5FileNameGenerator()).diskCacheSize(50 * 1024 * 1024) // 50.tasksProcessingOrder(QueueProcessingType.LIFO).diskCache(new UnlimitedDiscCache(cacheFile) )//设置图片的缓存路径,由自己指定.writeDebugLogs() // Remove for release app.build();// Initialize ImageLoader with configuration.ImageLoader.getInstance().init(config);


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

以下为参照原文进行的翻译

1.    Caching默认不可用. 启用需要对DisplayImageOptions进行如下配置:

2.  // Create default options which will be usedfor every

3.  // displayImage(...) call if no options will be passed to this method

4.  DisplayImageOptions defaultOptions= new DisplayImageOptions.Builder()

5.          ...

6.          .cacheInMemory()

7.          .cacheOnDisc()

8.          ...

9.          .build();

10. ImageLoaderConfiguration config= new ImageLoaderConfiguration.Builder(getApplicationContext())

11.         ...

12.         .defaultDisplayImageOptions(defaultOptions)

13.         ...

14.         .build();

15. ImageLoader.getInstance().init(config);// Do it on Application start

16. // Then later, when you want to display image

17. ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used

or this way:

DisplayImageOptions options= new DisplayImageOptions.Builder()

        ...

        .cacheInMemory()

        .cacheOnDisc()

        ...

        .build();

ImageLoader.getInstance().displayImage(imageUrl, imageView, options); // Incoming options will be used

18.  开启缓存后默认会缓存到外置SD卡如下地址(/sdcard/Android/data/[package_name]/cache).如果外置SD卡不存在,会缓存到手机.缓存到Sd卡需要在Manifest文件中进行如下配置

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

20.  UIL是如何为ImageView精确定义需要的Bitmap的尺寸?它会搜索如下参数

o   获取ImageView真实的 width height

o   获取 android:layout_width  android:layout_height 参数

o   获取 android:maxWidth and/or android:maxHeight 参数

o   configuration (memoryCacheExtraOptions(int,int) option)获取 maximum width and/or height 参数

o   获取设备屏幕的 width and/or height  

所以如果你知道ImageView的大约最大尺寸,就可以设置如下参数android:layout_width|android:layout_height or android:maxWidth|android:maxHeight 这样会有助于正确计算当前View所需要的Bitmap尺寸,并节约内存

如果你使用UIL时经常出现 OutOfMemoryError 那你可以尝试如下方法:

o   减少线程池大小 (.threadPoolSize(...)). 1 - 5 isrecommended.

o   在显示选项中使用 .bitmapConfig(Bitmap.Config.RGB_565) . RGB_565模式消耗的内存比ARGB_8888模式少两倍.

o   配置中使用 .memoryCache(newWeakMemoryCache()) 或者完全禁用在内存中缓存(don't call .cacheInMemory()).

o   在显示选项中使用 .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 或者.imageScaleType(ImageScaleType.EXACTLY).

o   避免使用 RoundedBitmapDisplayer.调用的时候它使用ARGB-8888模式创建了一个新的Bitmap对象来显示

对于内存缓存模式 (ImageLoaderConfiguration.memoryCache(...))你可以使用已经实现好的方法.

o   缓存只能使用强引用

§ LruMemoryCache (Least recently used bitmap is deleted when cache size limit isexceeded缓存大小超过指定值时,删除最近最少使用的bitmap) - Used by default for API >= 9

o   缓存使用弱引用和强引用:

§ UsingFreqLimitedMemoryCache (Least frequently used bitmap is deleted when cachesize limit is exceeded删除最少使用bitmap)

§ LRULimitedMemoryCache (Least recently used bitmap is deletedwhen cache size limit is exceeded删除最近最少使用bitmap) - Used by default for API < 9

§ FIFOLimitedMemoryCache (FIFOrule is used for deletion when cache sizelimit is exceeded先进先出规则删除bitmap)

§ LargestLimitedMemoryCache (The largest bitmap is deleted when cache sizelimit is exceeded删除最大的bitmap)

§ LimitedAgeMemoryCache (Decorator. Cached object is deleted when its ageexceeds defined value缓存对象超过定义的时间后删除)

o   缓存只能使用弱引用:

§ WeakMemoryCache (Unlimited cache不限制缓存)

21.  本地缓存模式可以使用以下以实现的方法 (ImageLoaderConfiguration.discCache(...)):

o   UnlimitedDiscCache (The fastest cache, doesn't limit cache size不限制缓存大小) - Used by default

o   TotalSizeLimitedDiscCache (Cache limited by total cache size. If cache size exceedsspecified limit then file with the most oldest last usage date will be deleted设置总缓存大小,超过时删除最久之前的缓存)

o   FileCountLimitedDiscCache (Cache limited by file count. If file count incache directory exceeds specified limit then file with the most oldest lastusage date will be deleted. Use it if your cached files are of about the samesize.设置总缓存文件数量,当到达警戒值时,删除最久之前的缓存。如果文件的大小都一样的时候,可以使用该模式)

o   LimitedAgeDiscCache (Size-unlimited cache with limited files' lifetime.If age of cached file exceeds defined limit then it will be deleted from cache.不限制缓存大小,但是设置缓存时间,到期后删除)

NOTE: UnlimitedDiscCache比其他方式快30%以上.

22.  To displaybitmap (DisplayImageOptions.displayer(...)) you can usealready prepared implementations:

o   RoundedBitmapDisplayer (Displays bitmap with rounded corners)

o   FadeInBitmapDisplayer (Displays image with "fade in" animation)

23.  To avoid list(grid, ...) scrolling lags you can use PauseOnScrollListener:

24. boolean pauseOnScroll= false;// or true

25. boolean pauseOnFling= true;// or false

26. PauseOnScrollListener listener= new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);

27. listView.setOnScrollListener(listener);