您的位置:首页 > 移动开发 > Android开发

android-universal-image-loader的使用

2013-08-22 17:06 441 查看
温故而知新,方可学而用

开源第三方框架 android-universal-image-loader简介

多线程异步图像加载,缓存和显示

属性设置

//设置图片在下载期间显示的图片

showStubImage(R.drawable.ic_launcher)

//设置图片Uri为空或是错误的时候显示的图片

showImageForEmptyUri(R.drawable.ic_empty)

//设置图片加载/解码过程中错误时候显示的图片

showImageOnFail(R.drawable.ic_error)

//设置图片在下载前是否重置,复位

resetViewBeforeLoading()

//设置下载的图片是否缓存在内存中

cacheInMemory()

//设置下载的图片是否缓存在SD卡中

cacheOnDisc()

//设置图片的解码类型

bitmapConfig(Bitmap.Config.RGB_565)

//设置图片的解码配置

decodingOptions(android.graphics.BitmapFactory.Options decodingOptions)

//设置图片下载前的延迟

delayBeforeLoading(int delayInMillis)

//设置额外的内容给ImageDownloader

extraForDownloader(Object extra)

//设置图片加入缓存前,对bitmap进行设置

preProcessor(BitmapProcessor preProcessor)

//设置显示前的图片,显示后这个图片一直保留在缓存中

postProcessor(BitmapProcessor postProcessor)

//设置图片以如何的编码方式显示

imageScaleType(ImageScaleType imageScaleType)

使用:

1. 创建继承Application的类 在Oncread中初始化

public static void initImageLoader(Context context) {

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)

.threadPriority(Thread.NORM_PRIORITY - 2)

.denyCacheImageMultipleSizesInMemory()

.discCacheFileNameGenerator(new Md5FileNameGenerator())

.tasksProcessingOrder(QueueProcessingType.LIFO)

.enableLogging() // Not necessary in common

.build();

// Initialize ImageLoader with configuration.

ImageLoader.getInstance().init(config);

}

2 在图片加载的activity中 创建
ImageLoader 、DisplayImageOptions对象

imageLoader = ImageLoader.getInstance();

options = new DisplayImageOptions.Builder()

// .showStubImage(R.drawable.food)//设置图片在下载期间显示的图片

// .showImageForEmptyUri(R.drawable.food)//设置图片Uri为空或是错误的时候显示的图片

// .showImageOnFail(R.drawable.food)//设置图片加载/解码过程中错误时候显示的图片

.cacheInMemory()//设置下载的图片是否缓存在内存中

.cacheOnDisc()//设置下载的图片是否缓存在SD卡中

.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型

.build();

gridViewleft = (GridView)findViewById(R.id.gridview_left);

context = this;

3 显示图片

imageLoader.displayImage(position, imageview, options);

简单的三步就可以实现多线程下图片的下载并缓存显示,无须考虑OOM问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐