universal image loader学习笔记

特性:

l 多线程图片加载(同步/异步)

l 可自由定制图片加载方式(thread executors, downloader, decoder, memory and disk cache, display image options, etc.)

l 可自由定制图片显示方式(stub images, caching switch, decoding options, Bitmap processing and displaying, etc)

l 图片缓存的处理(内存缓存 / 硬盘缓存(本机 / SD卡))

l 监听图片加载进度

 

工作流程

 

可处理的URI

"http://site.com/image.png" // 网络地址

"file:///mnt/sdcard/image.png" // SD卡

"file:///mnt/sdcard/video.mp4" // SD卡 (视频缩略图)

"content://media/external/images/media/13" // content provider

"content://media/external/video/media/13" // content provider (视频缩略图)

"assets://image.png" // assets

"drawable://" + R.drawable.img // drawables (不包含.9图)

快速使用

1. 添加依赖

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'

2. 添加权限

如需要请求网络图片,添加

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

如需要SD卡缓存,添加

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

3.在调用UIL之前必须初始化配置

新建一个类继承Application

在这个类里配置

mageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this); 

ImageLoader.getInstance().init(configuration);  

Configuration 参数详解

File cacheDir = StorageUtils.getCacheDirectory(context); // 自定义缓存文件夹

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)

        .memoryCacheExtraOptions(480800// 指定缓存到内存时图片的大小,默认是屏幕尺寸的长宽

        .diskCacheExtraOptions(480800null) // 指定缓存到硬盘时图片的大小,并不建议使用

        .taskExecutor(new Executor()// 自定义一个线程来加载和显示图片

        .taskExecutorForCachedImages(new Executor())// 自定义一个线程来缓存图片

        .threadPoolSize(3// default, 指定线程池大小

        .threadPriority(Thread.NORM_PRIORITY - 2// default ,指定线程优先级 

        .tasksProcessingOrder(QueueProcessingType.FIFO// default , 指定加载显示图片的任务队列的类型

        .denyCacheImageMultipleSizesInMemory() // 禁止在内存中缓存同一张图片的多个尺寸类型

        .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) // 指定内存缓存的大小,默认值为1/8 应用的最大可用内存

        .memoryCacheSize(2 * 1024 * 1024

        .memoryCacheSizePercentage(13// default

        .diskCache(new UnlimitedDiskCache(cacheDir)) // default , 指定硬盘缓存的地址

        .diskCacheSize(50 * 1024 * 1024// 指定硬盘缓存的大小

        .diskCacheFileCount(100// 指定硬盘缓存的文件个数

        .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default , 指定硬盘缓存时文件名的生成器

        .imageDownloader(new BaseImageDownloader(context)) // default , 指定图片下载器

        .imageDecoder(new BaseImageDecoder()) // default , 指定图片解码器

        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default , 指定图片显示的配置

        .writeDebugLogs() // 是否显示Log

        .build();


配置完以后,记得在清单文件中的Application中通过   android:name=".类名"    声明这个类


Display Options 参数详解

DisplayImageOptions options = new DisplayImageOptions.Builder()

        .showImageOnLoading(// 图片正在加载时显示的图片资源ID

        .showImageForEmptyUri(// URI为空时显示的图片资源ID

        .showImageOnFail(// 图片加载失败时显示的图片资源ID

        .resetViewBeforeLoading(false)  // default 图片在下载前是否重置,复位

        .delayBeforeLoading(1000// 图片开始加载前的延时.默认是0

        .cacheInMemory(false// default , 是否缓存在内存中, 默认不缓存

        .cacheOnDisk(false// default , 是否缓存在硬盘 , 默认不缓存

        .preProcessor(new BitmapProcessor// 设置图片缓存在内存前的图片处理器

        .postProcessor(new BitmapProcessor// 设置图片在缓存到内存以后 , 显示在界面之前的图片处理器

        .extraForDownloader(...// 为图片下载设置辅助参数

        .considerExifParams(false// default , 设置是否考虑JPEG图片的EXIF参数信息,默认不考虑

        .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2// default , 指定图片缩放的方式,ListView/GridView/Gallery推荐使用此默认值

        .bitmapConfig(Bitmap.Config.ARGB_8888// default , 指定图片的质量,默认是 ARGB_8888

        .decodingOptions(...// 指定图片的解码方式

        .displayer(new SimpleBitmapDisplayer()) // default , 设置图片显示的方式,用于自定义   new FadeInBitmapDisplayer();慢慢显示出图片,填显示出来过程用的时间。

//new RoundedBitmapDisplayer();设置圆角显示,当填360时变成圆形

        .handler(new Handler()) // default ,设置图片显示的方式和ImageLoadingListener的监听, 用于自定义

        .build();

常用方法

显示图片

ImageLoader.getInstance().loadImage(String uri, ImageLoadingListener listener)  

SimpleImageLoadingListenerImageLoadingListener的空实现

displayImage(String uri, ImageView imageView)

displayImage(String uri, ImageView imageView, DisplayImageOptions options)

   displayImage(String uri, ImageView imageView, DisplayImageOptions options,

ImageLoadingListener listener, ImageLoadingProgressListener progressListener) 

 

加载其他图片

 

// 图片来自本机文件

        String imageUrl = ImageDownloader.Scheme.FILE.wrap("/mnt/sdcard/image.png");

        // 图片来自Content Provider

        String contentURL = ImageDownloader.Scheme.CONTENT.wrap("/media/external/audio/albumart/13");

        //图片来源于assets

        String assetsUrl = ImageDownloader.Scheme.ASSETS.wrap("image.png");

        //图片来源于

        String drawableUrl = ImageDownloader.Scheme.DRAWABLE.wrap("R.drawable.image");

 

注意事项:

1. 默认情况UIL是没有缓存功能的 , 所以需要在构造DisplayImageOptions时打开缓存功能. cacheInMemory() cacheOnDisk()

2. 如果要缓存图片到本地硬盘 , 不要忘记添加SD卡读写权限

3. 如果发生OOM异常,参考如下处理

缩小线程池的大小, ImageLoaderConfiguration. threadPoolSize() , 推荐值为1-5

指定图片质量时,使用RGB_565, DisplayImageOptions. bitmapConfig(Bitmap.Config.RGB_565)

推荐使用DisplayImageOptions.imageScaleType(ImageScaleType.EXACTLY)

使用diskCacheExtraOptions时,推荐参数为ImageLoaderConfiguration.diskCacheExtraOptions(480, 320, null)

4. UIL已实现的内存缓存配置类.用于ImageLoaderConfiguration memoryCache()

仅强引用 LruMemoryCache

强引用 + 弱引用

UsingFreqLimitedMemoryCache

LRULimitedMemoryCache

FIFOLimitedMemoryCache

LargestLimitedMemoryCache

LimitedAgeMemoryCache

仅弱引用WeakMemoryCache

5. UIL已实现的硬盘缓存配置类,用于ImageLoaderConfiguration diskCache()

UnlimitedDiscCache (默认值)

LruDiskCache 

LimitedAgeDiscCache 

6. UIL已实现的图片显示类,    用于DisplayImageOptions displayer()

RoundedBitmapDisplayer (圆角图片) 使用该属性时,ImageView必须指定宽高

FadeInBitmapDisplayer (渐显图片)

7. 为了避免在可滚动视图中(ListView / GridView)发生OOM异常,请使用PauseOnScrollListener , 

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、付费专栏及课程。

余额充值