Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解 一、介绍

本文主要是介绍Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解 一、介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、介绍

 Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。所以,如果你的程序里需要这个功能的话,那么不妨试试它。因为已经封装好了一些类和方法。我们 可以直接拿来用了。而不用重复去写了。其实,写一个这方面的程序还是比较麻烦的,要考虑多线程缓存,内存溢出等很多方面。

二、具体使用

一个好的类库的重要特征就是可配置性强。我们先简单使用Android-Universal-Image-Loader,一般情况下使用默认配置就可以了。

下面的实例利用Android-Universal-Image-Loader将网络图片加载到图片墙中。

复制代码
 1 public class BaseActivity extends Activity {
 2     ImageLoader imageLoader;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5           // Create global configuration and initialize ImageLoader with this configuration
 6         ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
 7             .build();
 8         ImageLoader.getInstance().init(config);
 9         super.onCreate(savedInstanceState);
10     }
11 }  
复制代码
复制代码
  1 public class MainActivity extends BaseActivity {
  2 
  3     @Override
  4     protected void onCreate(Bundle savedInstanceState) {
  5         super.onCreate(savedInstanceState);
  6         setContentView(R.layout.activity_main);
  7   
  8         ImageLoader imageLoader = ImageLoader.getInstance();
  9 
 10         GridView gridView = (GridView) this.findViewById(R.id.grdvImageWall);
 11         gridView.setAdapter(new PhotoWallAdapter(Constants.IMAGES));
 12     }
 13 
 14     static class ViewHolder {
 15         ImageView imageView;
 16         ProgressBar progressBar;
 17     }
 18 
 19     public class PhotoWallAdapter extends BaseAdapter {
 20         String[] imageUrls;
 21         ImageLoader imageLoad;
 22         DisplayImageOptions options;
 23         LinearLayout gridViewItem;
 24 
 25         public PhotoWallAdapter(String[] imageUrls) {
 26             assert imageUrls != null;
 27             this.imageUrls = imageUrls;
 28 
 29             options = new DisplayImageOptions.Builder()
 30                     .showImageOnLoading(R.drawable.ic_stub) // resource or
 31                                                             // drawable
 32                     .showImageForEmptyUri(R.drawable.ic_empty) // resource or
 33                                                                 // drawable
 34                     .showImageOnFail(R.drawable.ic_error) // resource or
 35                                                             // drawable
 36                     .resetViewBeforeLoading(false) // default
 37                     .delayBeforeLoading(1000).cacheInMemory(false) // default
 38                     .cacheOnDisk(false) // default
 39                     .considerExifParams(false) // default
 40                     .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
 41                     .bitmapConfig(Bitmap.Config.ARGB_8888) // default
 42                     .displayer(new SimpleBitmapDisplayer()) // default
 43                     .handler(new Handler()) // default
 44                     .build();
 45             this.imageLoad = ImageLoader.getInstance();
 46 
 47         }
 48 
 49         @Override
 50         public int getCount() {
 51             return this.imageUrls.length;
 52         }
 53 
 54         @Override
 55         public Object getItem(int position) {
 56             if (position <= 0 || position >= this.imageUrls.length) {
 57                 throw new IllegalArgumentException(
 58                         "position<=0||position>=this.imageUrls.length");
 59             }
 60             return this.imageUrls[position];
 61         }
 62 
 63         @Override
 64         public long getItemId(int position) {
 65             return position;
 66         }
 67 
 68         @Override
 69         public View getView(int position, View convertView, ViewGroup parent) {
 70             // 判断这个image是否已经在缓存当中,如果没有就下载
 71             final ViewHolder holder;
 72             if (convertView == null) {
 73                 holder = new ViewHolder();
 74                 gridViewItem = (LinearLayout) getLayoutInflater().inflate(
 75                         R.layout.image_wall_item, null);
 76                 holder.imageView = (ImageView) gridViewItem
 77                         .findViewById(R.id.item_image);
 78                 holder.progressBar = (ProgressBar) gridViewItem
 79                         .findViewById(R.id.item_process);
 80                 gridViewItem.setTag(holder);
 81                 convertView = gridViewItem;
 82             } else {
 83                 holder = (ViewHolder) gridViewItem.getTag();
 84             }
 85             this.imageLoad.displayImage(this.imageUrls[position],
 86                     holder.imageView, options,
 87                     new SimpleImageLoadingListener() {
 88 
 89                         @Override
 90                         public void onLoadingStarted(String imageUri, View view) {
 91                             holder.progressBar.setProgress(0);
 92                             holder.progressBar.setVisibility(View.VISIBLE);
 93                         }
 94 
 95                         @Override
 96                         public void onLoadingFailed(String imageUri, View view,
 97                                 FailReason failReason) {
 98                             holder.progressBar.setVisibility(View.GONE);
 99                         }
100 
101                         @Override
102                         public void onLoadingComplete(String imageUri,
103                                 View view, Bitmap loadedImage) {
104                             holder.progressBar.setVisibility(View.GONE);
105                         }
106 
107                     }, new ImageLoadingProgressListener() {
108 
109                         @Override
110                         public void onProgressUpdate(String imageUri,
111                                 View view, int current, int total) {
112                             holder.progressBar.setProgress(Math.round(100.0f
113                                     * current / total));
114                         }
115                     }); // 通过URL判断图片是否已经下载
116             return convertView;
117         }
118 
119     }
120 }
复制代码

里面主要的对象都用        突出显示了。

三者的关系

ImageLoaderConfiguration是针对图片缓存的全局配置,主要有线程类、缓存大小、磁盘大小、图片下载与解析、日志方面的配置。

ImageLoader是具体下载图片,缓存图片,显示图片的具体执行类,它有两个具体的方法displayImage(...)、loadImage(...),但是其实最终他们的实现都是displayImage(...)。

DisplayImageOptions用于指导每一个Imageloader根据网络图片的状态(空白、下载错误、正在下载)显示对应的图片,是否将缓存加载到磁盘上,下载完后对图片进行怎么样的处理。

从三者的协作关系上看,他们有点像厨房规定、厨师、客户个人口味之间的关系。ImageLoaderConfiguration就像是厨房里面的规定,每一个厨师要怎么着装,要怎么保持厨房的干净,这是针对每一个厨师都适用的规定,而且不允许个性化改变。ImageLoader就像是具体做菜的厨师,负责具体菜谱的制作。DisplayImageOptions就像每个客户的偏好,根据客户是重口味还是清淡,每一个imageLoader根据DisplayImageOptions的要求具体执行。

 

ImageLoaderConfiguration

在上面的示例代码中,我们使用ImageLoaderConfiguration的默认配置,下面给出ImageLoaderConfiguration比较详尽的配置,从下面的配置中,可以看出ImageLoaderConfiguration的配置主要是全局性的配置,主要有线程类、缓存大小、磁盘大小、图片下载与解析、日志方面的配置。

复制代码
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(480, 800) // default = device screen dimensions.diskCacheExtraOptions(480, 800, null).taskExecutor(...).taskExecutorForCachedImages(...).threadPoolSize(3) // default.threadPriority(Thread.NORM_PRIORITY - 1) // default.tasksProcessingOrder(QueueProcessingType.FIFO) // default
        .denyCacheImageMultipleSizesInMemory().memoryCache(new LruMemoryCache(2 * 1024 * 1024)).memoryCacheSize(2 * 1024 * 1024).memoryCacheSizePercentage(13) // default.diskCache(new UnlimitedDiscCache(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().build();
复制代码

ImageLoaderConfiguration的主要职责就是记录相关的配置,它的内部其实就是一些字段的集合(如下面的源代码)。它有一个builder的内部类,这个类中的字段跟ImageLoaderConfiguration中的字段完全一致,它有一些默认值,通过修改builder可以配置ImageLoaderConfiguration。

View Code

 

 Display Options

每一个ImageLoader.displayImage(...)都可以使用Display Options

复制代码
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).cacheInMemory(false) // default.cacheOnDisk(false) // default
        .preProcessor(...).postProcessor(...).extraForDownloader(...).considerExifParams(false) // default.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default.bitmapConfig(Bitmap.Config.ARGB_8888) // default
        .decodingOptions(...).displayer(new SimpleBitmapDisplayer()) // default.handler(new Handler()) // default.build();
复制代码

 Display Options的主要职责就是记录相关的配置,它的内部其实就是一些字段的集合(如下面的源代码)。它有一个builder的内部类,这个类中的字段跟DisplayOption中的字段完全一致,它有一些默认值,通过修改builder可以配置DisplayOptions。

View Code

 

 

参考链接

http://blog.csdn.net/wangjinyu501/article/details/8091623

https://github.com/nostra13/Android-Universal-Image-Loader

http://www.intexsoft.com/blog/item/74-universal-image-loader-part-3.html

这篇关于Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解 一、介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136793

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP