android-universal-image-loader

一、ImageLoaderConfiguration参数说明


1.获得此类的方式:

复制代码
 1     /**
 2      * ImageLoaderConfiguration 创建的两种方式。
 3      */
 4     // 创建默认的ImageLoaderConfiguration
 5     ImageLoaderConfiguration configuration_0 = ImageLoaderConfiguration
 6             .createDefault(this);
 7 
 8     // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
 9     ImageLoaderConfiguration configuration_1 = new ImageLoaderConfiguration.Builder(
10             this).threadPriority(Thread.NORM_PRIORITY - 2)
11             .denyCacheImageMultipleSizesInMemory()
12             .discCacheFileNameGenerator(new Md5FileNameGenerator())
13             .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()
14             .build();
复制代码

2.常量

复制代码
1 /**
2  * 常量的设置
3  */
4 //属性的设置
5 EFAULT_TASK_PROCESSING_TYPE 
6 //线程池的大小
7 DEFAULT_THREAD_POOL_SIZE 
8 //线程的优先级
9 DEFAULT_THREAD_PRIORITY 
复制代码

3.常用方法

1 /**
2  *当同一个Uri获取不同大小的图片,缓存到内存时,只缓存一个。默认会缓存多个不同的大小的相同图片
3  */
4 denyCacheImageMultipleSizesInMemory() 
复制代码
 1     /**
 2      * 设置本地图片缓存
 3      * @param discCache
 4      */
 5     discCache(DiscCacheAware discCache) 
 6               discCache:
 7                         FileCountLimitedDiscCache(File cacheDir, int maxFileCount):设置缓存路径和缓存文件的数量,超过数量后,old将被删除
 8                         FileCountLimitedDiscCache(File cacheDir,FileNameGenerator fileNameGenerator,int maxFileCount):第二个参数是通过图片的url生成的唯一文件名。
 9                         LimitedAgeDiscCache(File cacheDir, FileNameGenerator fileNameGenerator, long maxAge) :第二个参数同上
10                         LimitedAgeDiscCache(File cacheDir, long maxAge):maxAge为定义的时间,超过时间后,图片将被删除
11                         TotalSizeLimitedDiscCache(File cacheDir, FileNameGenerator fileNameGenerator, int maxCacheSize) :第二个参数同上   
12                         TotalSizeLimitedDiscCache(File cacheDir, int maxCacheSize) :定义缓存的大小,如超过了,就会删除old图片。
13                         UnlimitedDiscCache(File cacheDir) :缓存没有限制
14                         UnlimitedDiscCache(File cacheDir, FileNameGenerator fileNameGenerator):第二个参数同上
复制代码
复制代码
 1     /**
 2      * 设置图片保存到本地的参数
 3      * @param maxImageWidthForDiscCache 保存的最大宽度
 4      * @param maxImageHeightForDiscCache 保存的最大高度
 5      * @param compressFormat    保存的压缩格式
 6      * @param compressQuality 提示压缩的程度,有0-100.想png这种图片无损耗,就不必设置了
 7      */
 8     discCacheExtraOptions(int maxImageWidthForDiscCache,
 9             int maxImageHeightForDiscCache,
10             android.graphics.Bitmap.CompressFormat compressFormat,
11             int compressQuality)
复制代码
1     /**
2      * 设置缓存文件的数量
3      * @param maxFileCount 数量
4      */
5     discCacheFileCount(int maxFileCount)
1     /**
2      * 设置缓存的大小
3      * @param maxCacheSize 大小
4      */
5       discCacheSize(int maxCacheSize) 
复制代码
1     /**
2      * 设置缓存文件的名字
3      * @param fileNameGenerator
4      */
5     discCacheFileNameGenerator(FileNameGenerator fileNameGenerator)
6     fileNameGenerator:
7     HashCodeFileNameGenerator() :通过HashCode将url生成文件的唯一名字
8     Md5FileNameGenerator():通过Md5将url生产文件的唯一名字
复制代码
1 /**
2  * 启动Log信息记录,用于查看异常信息
3  */
4   enableLogging() 
复制代码
1     /**
2      * 设置缓存信息
3      * @param maxImageWidthForMemoryCache 缓存图片的最大宽度,默认为手机的屏幕宽度
4      * @param maxImageHeightForMemoryCache 缓存图片的最大高度,默认为手机的屏幕宽度
5      */
6     memoryCacheExtraOptions(int maxImageWidthForMemoryCache, int maxImageHeightForMemoryCache) 
复制代码
复制代码
1     /**
2      * 添加个线程池,进行下载
3      * @param executor 线程池
4      * 如果进行了这个设置,那么threadPoolSize(int),threadPriority(int),tasksProcessingOrder(QueueProcessingType)
5      * 将不会起作用
6      */
7      taskExecutor(Executor executor)
复制代码
1     /**
2      * 设置用于显示图片的线程池大小
3      * @param threadPoolSize
4      */
5     threadPoolSize(int threadPoolSize)
1     /**
2      * 设置线程的优先级
3      * @param threadPriority
4      */
5     threadPriority(int threadPriority)
1     /**
2      * 设置图片下载和显示的工作队列排序
3      * @param tasksProcessingType
4      */
5     tasksProcessingOrder(QueueProcessingType tasksProcessingType)
1     /**
2      * 下载缓存图片
3      * @param executorForCachedImages
4      */
5     taskExecutorForCachedImages(Executor executorForCachedImages)


二、DisplayImageOptions参数
 1     //设置图片在下载期间显示的图片
 2     showStubImage(R.drawable.ic_launcher)
 3     
 4     //设置图片Uri为空或是错误的时候显示的图片
 5     showImageForEmptyUri(R.drawable.ic_empty)
 6     
 7     //设置图片加载/解码过程中错误时候显示的图片
 8     showImageOnFail(R.drawable.ic_error)
 9     
10     //设置图片在下载前是否重置,复位
11     resetViewBeforeLoading()
12     
13     //设置下载的图片是否缓存在内存中
14     cacheInMemory()
15     
16     //设置下载的图片是否缓存在SD卡中
17     cacheOnDisc()
18     
19     //设置图片的解码类型
20     bitmapConfig(Bitmap.Config.RGB_565)
21     
22     //设置图片的解码配置
23     decodingOptions(android.graphics.BitmapFactory.Options decodingOptions)
24     
25     //设置图片下载前的延迟
26     delayBeforeLoading(int delayInMillis) 
27     
28     //设置额外的内容给ImageDownloader
29     extraForDownloader(Object extra)
30     
31     //设置图片加入缓存前,对bitmap进行设置
32     preProcessor(BitmapProcessor preProcessor)
33     
34     //设置显示前的图片,显示后这个图片一直保留在缓存中
35     postProcessor(BitmapProcessor postProcessor) 
36     
37     //设置图片以如何的编码方式显示
38     imageScaleType(ImageScaleType imageScaleType)
 
 

一,快速使用(确保ImageLoader只初始化一次,这样图片缓存会更加优秀。)

场景:为ImageView设置一张指定Uri的图片。

1,导包,配置联网,读写SD卡权限。

2,初始化:(如果在自定义Application中执行初始化动作,manifest文件中Application节点的name属*,记得改变。)

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();

ImageLoader.getInstance.init(config);

3,参数配置

DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder() 
            .showStubImage(R.drawable.ic_launcher) 
            .showImageOnFail(R.drawable.ic_launcher) 
            .imageScaleType(ImageScaleType.EXACTLY) 
            .showImageForEmptyUri(R.drawable.ic_launcher).cacheInMemory(true) 
            .cacheOnDisc(true).displayer(new FadeInBitmapDisplayer(300)) 
            .imageScaleType(ImageScaleType.EXACTLY).build();

4,设置图片。

ImageLoader.displayImage(URI,mImageView,displayImageOptions);

二,功能(翻译自GitHub)

来源:https://github.com/dodola/Android-Universal-Image-Loader

功能:1,多线程图片加载;

        2,尽可能协调了最广泛的图片加载配置(子线程,下载,解*,内存&硬盘缓存,显示图片,和其他);

         3,尽可能的监听下载进程;

         4,尽可能的为各种参数定制显示图片调用;

         5,支持桌面小空间

支持Android 2.0以上版本。

三,初始化参数介绍:

Configuration

所有的选项在配置建造器中都是可选择的。使用那些你真正想要定制的。

// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
        .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75)
        .taskExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
        .taskExecutorForCachedImages(AsyncTask.THREAD_POOL_EXECUTOR)
        .threadPoolSize(3) // default
        .threadPriority(Thread.NORM_PRIORITY - 1) // default
        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
        .denyCacheImageMultipleSizesInMemory()
        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
        .memoryCacheSize(2 * 1024 * 1024)
        .discCache(new UnlimitedDiscCache(cacheDir)) // default
        .discCacheSize(50 * 1024 * 1024)
        .discCacheFileCount(100)
        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
        .imageDownloader(new BaseImageDownloader(context)) // default
        .imageDecoder(new BaseImageDecoder()) // default
        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
        .enableLogging()
        .build();
 
 
Display Option
显示选项,可以被任何一个显示线程请求。
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.ic_stub) // resource or drawable
        .showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
        .showImageOnFail(R.drawable.ic_error) // resource or drawable
        .resetViewBeforeLoading(false)  // default
        .delayBeforeLoading(1000)
//如果想要从缓存中加载图片 ,下面两句话的参数设置为true。
        .cacheInMemory(false) // default。       
        .cacheOnDisc(false) // default
        .preProcessor(...)
        .postProcessor(...)
        .extraForDownloader(...)
        .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
        .bitmapConfig(Bitmap.Config.ARGB_8888) // default
        .decodingOptions(...)
        .displayer(new SimpleBitmapDisplayer()) // default
        .handler(new Handler()) // default
        .build();
四,用例
显示图片所使用的uri:
String imageUri = "http://site.com/image.png"; // from Web
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
注意:使用drawable://除非你真的需要他。时刻要注意使用本地图片加载方法:setImageResource带代替ImageLoader。
五,有用的信息
1,ImageLoader.getInstance().init(config); // 在应用开启的时候初始化。
2,<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>sd卡缓存是需要写入权限
3,ImageLoader根据ImageView的width,height确定图片的宽高。
4,如果经常出现OOM
   ①减少配置之中线程池的大小,(.threadPoolSize).推荐1-5;
   ②使用.bitmapConfig(Bitmap.config.RGB_565)代替ARGB_8888;
   ③使用.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者try.imageScaleType(ImageScaleType.EXACTLY);
   ④避免使用RoundedBitmapDisplayer.他会创建新的ARGB_8888格式的Bitmap对象;
   ⑤使用.memoryCache(new WeakMemoryCache()),不要使用.cacheInMemory();
5,内存缓存,sd卡缓存,显示图片,可以使用已经初始化过的实现;
6,为了避免使用list,grid,scroll,你可以使用
boolean pauseOnScroll = false; // or true
boolean pauseOnFling = true; // or false
PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
listView.setOnScrollListener(listener);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值