天天看点

Android中ImageLoader的用法

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

使用步骤

1.导入universal-image-loader-1.9.5.jar 到项目中

2.创建 MyApplication继承 Application,在 onCreate() 中初始化ImageLoader

(1)初始化 ImageLoaderConfiguration

(2)ImageLoader 全局配置

3.将创建的 MyApplication 在AndroidMainfest.xml 中注册

4.在AndroidMainfest.xml添加联网权限和 sdk权限

5.初始化 DisplayImageOptions

6.获取 ImageLoader 实例

7.显示加载的图片

参数1:图片 url

参数2:显示图片的控件

参数3:显示图片的设置

参数4:监听器

imageLoader.displayImage(imageUri, imageView, options, new ImageLoadingListener() {
}
           

ImageLoader内存溢出解决方案

1.减少线程池中的线程个数,在 ImageLoaderConfiguration 中的(.threadPoolSize)中配置,推荐配置1-5.

2.在DisplayImageOptions 选项中配置 bitmapConfig 为Bitmap.Config.RGB_565,因为默认是 ARGB_8888,只用RGB_565会比使用ARGB_8888少消耗2倍的内存

3.ImageLoaderConfiguration 中配置图片的内存缓存为 memoryCache(new WeakMemoryCache())或者不能使用内存缓存

4.在DisplayImageOptions选项中设置 imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者 imageScaleType(ImageScaleType.EXACTLY)