AndroidUniversalImageLoader网络图片加载
1.功能概要
Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。
(1).使用多线程加载图片
(2).灵活配置ImageLoader的基本参数,包括线程数、缓存方式、图片显示选项等;
(3).图片异步加载缓存机制,包括内存缓存及SDCard缓存;
(4).采用监听器监听图片加载过程及相应事件的处理;
(5).配置加载的图片显示选项,比如图片的圆角处理及渐变动画。
2.简单实现
ImageLoader采用单例设计模式,ImageLoader imageLoader = ImageLoader.getInstance();得到该对象,每个ImageLoader采用单例设计模式,ImageLoader必须调用init()方法完成初始化。
- // 1.完成ImageLoaderConfiguration的配置
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
- .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
- .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, 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
- .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
- .writeDebugLogs()
- .build();
- // 2.单例ImageLoader类的初始化
- ImageLoader imageLoader = ImageLoader.getInstance();
- imageLoader.init(config);
- // 3.DisplayImageOptions实例对象的配置
- // 以下的设置再调用displayImage()有效,使用loadImage()无效
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- .showStubImage(R.drawable.ic_stub) // image在加载过程中,显示的图片
- .showImageForEmptyUri(R.drawable.ic_empty) // empty URI时显示的图片
- .showImageOnFail(R.drawable.ic_error) // 不是图片文件 显示图片
- .resetViewBeforeLoading(false) // default
- .delayBeforeLoading(1000)
- .cacheInMemory(false) // default 不缓存至内存
- .cacheOnDisc(false) // default 不缓存至手机SDCard
- .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();
- // 4图片加载
- // 4.1 调用displayImage
- imageLoader.displayImage(
- 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
- */
- imageView, // 对应的imageView控件
- options); // 与之对应的image显示方式选项
- // 4.2 调用loadImage
- // 对于部分DisplayImageOptions对象的设置不起作用
- imageLoader.loadImage(
- uri,
- options,
- new MyImageListener()); //ImageLoadingListener
- class MyImageListener extends SimpleImageLoadingListener{
- @Override
- public void onLoadingStarted(String imageUri, View view) {
- imageView.setImageResource(R.drawable.loading);
- super.onLoadingStarted(imageUri, view);
- }
- @Override
- public void onLoadingFailed(String imageUri, View view,
- FailReason failReason) {
- imageView.setImageResource(R.drawable.no_pic);
- super.onLoadingFailed(imageUri, view, failReason);
- }
- @Override
- public void onLoadingComplete(String imageUri, View view,
- Bitmap loadedImage) {
- imageView.setImageBitmap(loadedImage);
- super.onLoadingComplete(imageUri, view, loadedImage);
- }
- @Override
- public void onLoadingCancelled(String imageUri, View view) {
- imageView.setImageResource(R.drawable.cancel);
- super.onLoadingCancelled(imageUri, view);
- }
- }
3.支持的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)
加载drawables下图片,可以通过ImageView.setImageResource(...) 而不是通过上面的ImageLoader.
4.缓冲至手机
默认不能保存缓存,必须通过下面的方式指定
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- ...
- .cacheInMemory(true)
- .cacheOnDisc(true)
- ...
- .build();
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
- ...
- .defaultDisplayImageOptions(options)
- ...
- .build();
- ImageLoader.getInstance().init(config); // Do it on Application start
- ImageLoader.getInstance().displayImage(imageUrl, imageView); /*
- 默认为defaultDisplayImageOptions设定的options对象,此处不用指定options对象 */
或者通过下面这种方式
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- ...
- .cacheInMemory(true)
- .cacheOnDisc(true)
- ...
- .build();
- ImageLoader.getInstance().displayImage(imageUrl, imageView, options); //此处指定options对象
由于缓存需要在外设中写入数据,故需要添加下面的权限
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
5.OutOfMemoryError
如果OutOfMemoryError错误很常见,可以通过下面的方式设置
(1).减少configuration中线程池的线程数目(.threadPoolSize(...)) 推荐为1 - 5
(2).display options通过.bitmapConfig(Bitmap.Config.RGB_565)设置. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.
(3).使用configuration的memoryCache(new WeakMemoryCache())方法 或者不调用.cacheInMemory()方法
(4).display options通过.imageScaleType(ImageScaleType.IN_SAMPLE_INT) 或者 .imageScaleType(ImageScaleType.EXACTLY)方法
(4).避免使用RoundedBitmapDisplayer,它创建了一个新的ARGB_8888 Bitmap对象
6.内存缓存管理
通过imageLoaderConfiguration.memoryCache([new LruMemoryCache(1)]))对手机内存缓存进行管理
LruMemoryCache
API >= 9默认.it is moved to the head of a queue.
FreqLimitedMemoryCache
当超过缓存大小后,删除最近频繁使用的bitmap
LRULimitedMemoryCache
API < 9 默认.当超过缓存大小后,删除最近使用的bitmap
FIFOLimitedMemoryCache
FIFO rule is used for deletion when cache size limit is exceeded
LargestLimitedMemoryCache
The largest bitmap is deleted when cache size limit is exceeded
WeakMemoryCache
Unlimited cache
7.SDcard缓存管理
通过imageLoaderConfiguration.discCache([new TotalSizeLimitedDiscCache()]))对SD卡缓存进行管理
UnlimitedDiscCache
default The fastest cache, doesn't limit cache size
TotalSizeLimitedDiscCache
Cache limited by total cache size. If cache size exceeds specified limit then file with themost oldest lastusage date will be deleted
FileCountLimitedDiscCache
Cache limited by file count. If file count in cache directory exceeds specified limit then file with the most oldest last usage date will be deleted.
LimitedAgeDiscCache
Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.
UnlimitedDiscCache is 30%-faster than other limited disc cache implementations
转自:http://www.cnblogs.com/wanqieddy/p/3836485.html
AndroidUniversalImageLoader网络图片加载的更多相关文章
- android--------Universal-Image-Loader图片加载框架和结合LruCache缓存图片
本博客包含包含Android-Universal-Image-Loader 网络图片加载框架实现图片加载和结合universal-image-loader与LruCache来自定义缓存图片,可以设置缓 ...
- Android之网络图片加载的5种基本方式
学了这么久,最近有空把自己用到过的网络加载图片的方式总结了出来,与大家共享,希望对你们有帮助. 此博客包含Android 5种基本的加载网络图片方式,包括普通加载HttpURLConnection.H ...
- 55、Android网络图片 加载缓存处理库的使用
先来一个普通的加载图片的方法. import android.annotation.SuppressLint; import android.app.Activity; import and ...
- Android 开发 框架系列 Android-Universal-Image-Loader 图片加载使用demo
Android-Universal-Image-Loader github地址:https://github.com/nostra13/Android-Universal-Image-Loader 加 ...
- android ImageView网络图片加载、动态设置尺寸、圆角..
封装了一个关于ImageView的辅助类,该类可以方便实现网络图片下载的同时,动态设置图片尺寸.圆角.....一系列连贯的操作,无样式表,java代码实现所有功能,使用很方便. package com ...
- ★android开发--ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)
例子中用于解析Json的Gson请自己Google下载 主Activity: package COM.Example.Main; import java.util.HashMap; import ja ...
- android 开发 - 网络图片加载库 Fresco 的使用。
概述 Fresco 是 facebook 的开源类库,它支持更有效的加载网络图片以及资源图片.它自带三级缓存功能,让图片显示更高效. 介绍 Fresco 是一个强大的图片加载组件. Fresco 中设 ...
- IOS开发笔记 - 基于SDWebImage的网络图片加载处理
前言: 在IOS下通过URL读一张网络图片并不像Asp.net那样可以直接把图片路径放到图片路径的位置就ok, 而是需要我们通过一段类似流的方式去加载网络图片,接着才能把图片放入图片路径显示. 这里找 ...
- Android_开源框架_AndroidUniversalImageLoader网络图片加载
1.功能概要 Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示. (1).使用多线程加载图片(2) ...
随机推荐
- 【HDU 1133】 Buy the Ticket (卡特兰数)
Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...
- [wikioi]乘积最大
http://wikioi.com/problem/1017/ 划分型动态规划1.转移方程是:f[i][j]=max(f[k][j-1]*t[k+1][i]),f[i][j]表示前面i个字符加上j个乘 ...
- 使用libCurl实现断点下载
关键部分代码如下: #include "curl.h" #pragma comment(lib, "libcurl.lib") size_t CROS_Down ...
- 值得推荐的C/C++框架和库 very good
[本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看看现有的轮子吧 值得学习的C语 ...
- [cocos2dx]计算scrollview元素的index
scrollview的原生代码没有提供元素对齐功能 通过下面介绍的index计算方法以及scrollview自带的设置位置方法 void setContentOffsetInDuration(CCPo ...
- 17.2.1 Replication Implementation Details 复制实现细节:
17.2 Replication Implementation 复制是基于master server 跟踪所有改变到他的数据库(更新,删除等等)在它的binary log. binary log 作为 ...
- vs2010 更新jQuery智能提示包
vs2010 更新jQuery只能提示包时,可以直接在NuGet中更新 jquery-2.1.0-vsdoc.js jquery-2.1.0.js jquery-2.1.0.min.js jquery ...
- Angular.js vs Ember.js
Angular.js 拥抱 HTML/CSS Misko Hevery(Angular.js的开发者之一)回答了这一问题,他的主要观点如下: 在HTML中加入太多逻辑不是好做法.Angular.js只 ...
- [Unix.C]Files and Directories
stat, fstat, and lstat Functions 本部分讨论的内容主要围绕3个stat函数及其返回值. #include <sys/stat.h> int stat(co ...
- JDBC_demo:java连接mysql过程
1.任何数据库驱动程序都提供对java.sql.Driver接口的驱动类,mysql-connector-java-5.1.39-bin.jar中Driver: package com.mysql.j ...