1.功能概要
Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。
(1).使用多线程加载图片
(2).灵活配置ImageLoader的基本参数,包括线程数、缓存方式、图片显示选项等;
(3).图片异步加载缓存机制,包括内存缓存及SDCard缓存;
(4).采用监听器监听图片加载过程及相应事件的处理;
(5).配置加载的图片显示选项,比如图片的圆角处理及渐变动画。
// 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);
}
}
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.
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对象
由于缓存需要在外设中写入数据,故需要添加下面的权限
通过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 |
通过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.
使用开源imageLoader优化listview加载大量本地图片的demo-->
package com.example.listviewdemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.smalt.smaltmap.utils.GlobalConstant;
/**
*
* @Title:
* @Description:ListView显示大量图片
* @Copyright: Copyright (c) 2014
* @Company:
* @author: Administrator
* @version: 1.0.0.0
* @Date: 2014-2-14
*/
public class MainActivity01 extends AbsListViewBaseActivity {
DisplayImageOptions options; // 配置图片加载及显示选项
ListView lvShow;
/**
* 存储图片地址
*/
ArrayList listImgPath;
String[] imageUriArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvShow = (ListView) findViewById(R.id.lv_show);
// 扫描内存中图片并存入list
listImgPath = getImgPathList();
if (listImgPath.size() < 1) {
GlobalConstant.i("sd卡中无图片");
} else {
// list转成数组
imageUriArray = (String[]) listImgPath
.toArray(new String[listImgPath.size()]);
// 配置图片加载及显示选项(还有一些其他的配置,查阅doc文档吧)
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_launcher) // 在ImageView加载过程中显示图片
.showImageForEmptyUri(R.drawable.ic_launcher) // image连接地址为空时
.showImageOnFail(R.drawable.ic_launcher) // image加载失败
.cacheInMemory(true) // 加载图片时会在内存中加载缓存
.cacheOnDisc(true) // 加载图片时会在磁盘中加载缓存
.displayer(new RoundedBitmapDisplayer(20)) // 设置用户加载图片task(这里是圆角图片显示)
.build();
// 加载适配器
lvShow.setAdapter(new AdapterList());
}
}
@Override
public void onBackPressed() {
AnimateFirstDisplayListener.displayedImages.clear();
super.onBackPressed();
}
/**
*
* 获取图片地址列表
*
* @return list
*/
private ArrayList getImgPathList() {
ArrayList list = new ArrayList();
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { "_id", "_data" }, null, null, null);
while (cursor.moveToNext()) {
list.add(cursor.getString(1));// 将图片路径添加到list中
}
cursor.close();
return list;
}
/** 图片加载监听事件 **/
private static class AnimateFirstDisplayListener extends
SimpleImageLoadingListener {
static final List displayedImages = Collections
.synchronizedList(new LinkedList());
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500); // 设置image隐藏动画500ms
displayedImages.add(imageUri); // 将图片uri添加到集合中
}
}
}
}
/**
*
* @Title:
* @Description:listview适配器
* @Copyright: Copyright (c) 2014
* @Company:
* @author: Administrator
* @version: 1.0.0.0
* @Date: 2014-2-14
*/
class AdapterList extends BaseAdapter {
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageUriArray.length;
}
/**
* 构造方法
*/
public AdapterList() {
// TODO Auto-generated constructor stub
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
// 初始化绑定控件
convertView = getLayoutInflater().inflate(R.layout.layout_item, null);
holder.imgShow = (ImageView) findViewById(R.id.img_show);
holder.tvShow = (TextView) findViewById(R.id.tv_show);
// add to convertView
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 设置img,text具体显示的内容
holder.tvShow.setText("fuck" + (position));
imageLoader.displayImage(imageUriArray[position], holder.imgShow,
options, animateFirstListener);
return convertView;
}
}
static class ViewHolder {
ImageView imgShow;
TextView tvShow;
}
}