软件框架之imageloader的使用

news/2024/7/4 9:17:44

Imageloader的使用

一、特点:

1)多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等
2)支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置
3)支持图片的内存缓存,文件系统缓存或者SD卡缓存
4)支持图片下载过程的监听
5)根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存
6)较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中,滑动过程中暂停加载图片,停止滑动的时候去加载图片
7)提供在较慢的网络下对图片进行加载

二、下载地址

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在AndroidManifest.xml中注册
注册Application.png
4)在AndroidManifest.xml中添加联网权限和写sdk权限
   <uses-permission android:name="android.permission.INTERNET"></uses-permission>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
5)初始化DisplayImageOptions
6)获取ImageLoader实例
ImageLoader imageLoader = ImageLoader.getInstance();
7)显示加载的图片
参数1:图片url; 参数2:显示图片的控件; 参数3:显示图片的设置; 参数4:监听器
 imageLoader.displayImage(Constants.IMAGES[position], holder.image, options, mFirstLoadImageListener);

四、例子

0)准备工作(详见使用步骤)
1)导入universal-image-loader-1.9.5.jar到项目中 
2)创建MyApplication继承Application,在oncreate()中初始化ImageLoader
(1)初始化ImageLoaderConfiguration
(2)ImageLoader全局配置
3)将创建的MyApplication在AndroidManifest.xml中注册
4)在AndroidManifest.xml中添加联网权限和写sdk权限
1)在ListView中加载图片
(1)初始化布局
(2)初始化listview
(3)初始化适配器
a)构造器
b)创建存放图片URL的类
c)四个核心方法
2)在GridView中加载图片
(1)初始化布局
(2)初始化view
(3)初始化适配器
a)构造器
b)四个核心方法
3)在ViewPager中加载图片
(1)初始化布局
(2)初始化view
(3)初始化适配器
a)构造器
b)四个核心方法

五、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)


http://www.niftyadmin.cn/n/3649338.html

相关文章

实验三(OSPF)7 8

解题思路&#xff1a; 先配置好路由的环回及规划好IP地址&#xff0c;确保正确&#xff1b; &#xff08;由于r8模拟为运营商&#xff0c;因此r1,r2,r3各写一条缺省指向r8 并测试&#xff09; hub-spoke网络结构&#xff0c;需要在r1-r2-r3建立隧道0配置MGRE-多点通用路由协…

HarmonyOS 2.0 手机版使用初体验 ——手机开发者 (Beta版)

12月16日上午10点&#xff0c;华为在北京举办华为开发者日暨HarmonyOS2.0手机开发者Beta版发布活动。华为此次宣布面向手机开发者开放完整的HarmonyOS 2.0系统能力、丰富的API&#xff08;应用开发接口&#xff09;&#xff0c;以及强大的开发工具DevEco Studio等技术装备&…

angular4前后端分离_如何在Angular 4+中使用Apollo客户端GraphQL

angular4前后端分离Apollo Client is the flexible, community-driven GraphQL client for Angular, JavaScript, and native platforms. It is designed from the ground up to make it easy to build UI components that fetch data with GraphQL. This article is a quick s…

软件框架之ButterKnife的使用

1、简介ButterKnife是注解中相对简单易懂的很不错的开源框架1.强大的View绑定和Click事件处理功能&#xff0c;简化代码&#xff0c;提升开发效率2.方便的处理Adapter里的ViewHolder绑定问题3.运行时不会影响APP效率&#xff0c;使用配置方便4.代码清晰&#xff0c;可读性强2、…

python pyenv_如何使用Pyenv和Direnv管理Python

python pyenv介绍 (Introduction) Whether you’re just getting started or you’re a seasoned Python developer, you may have found managing your Python environments to be tedious and painful. Managing Python versions, libraries, and various dependencies is li…

不能一帆风顺,那就乘风破浪

我们这一生很短&#xff0c;我们终将会失去它。所以不妨大胆一点&#xff0c;爱一个人&#xff0c;攀一座山&#xff0c;追一次梦。不妨大胆一点&#xff0c;有很多事都没有答案。 – 《大鱼海棠》 ▣ 博主主站地址&#xff1a;微笑涛声 【www.cztcms.cn】 ▣ 博主其他平台&am…

软件框架之EventBus的使用

1、简介EventBus是一个Android端优化的publish/subscribe消息总线&#xff0c;简化了应用程序内各组件间、组件与后台线程间的通信。比如请求网络&#xff0c;等网络返回时通过Handler或Broadcast通知UI&#xff0c;两个Fragment之间需要通过Listener通信&#xff0c;这些需求都…