下面是BitmapUtils简单的使用:
BitmapUtils bitmapUtils = new BitmapUtils(this);
// 加载网络图片
bitmapUtils.display(testImageView, "http://bbs.lidroid.com/static/image/common/logo.png");
// 加载本地图片(路径以/开头, 绝对路径)
bitmapUtils.display(testImageView, "/sdcard/test.jpg");
// 加载assets中的图片(路径以assets开头)
bitmapUtils.display(testImageView, "assets/img/wallpaper.jpg");
// 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true, customListener));
public abstract class BitmapLoadCallBack {
/**
* Call back when start loading.
*
* @param container
* @param uri
* @param config
*/
public void onPreLoad(T container, String uri, BitmapDisplayConfig config) {
}
/**
* Call back when start loading.
*
* @param container
* @param uri
* @param config
*/
public void onLoadStarted(T container, String uri, BitmapDisplayConfig config) {
}
/**
* Call back when loading.
*
* @param container
* @param uri
* @param config
* @param total
* @param current
*/
public void onLoading(T container, String uri, BitmapDisplayConfig config, long total, long current) {
}
/**
* Call back when bitmap has loaded.
*
* @param container
* @param uri
* @param bitmap
* @param config
*/
public abstract void onLoadCompleted(T container, String uri, Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from);
/**
* Call back when bitmap failed to load.
*
* @param container
* @param uri
* @param drawable
*/
public abstract void onLoadFailed(T container, String uri, Drawable drawable);
private BitmapSetter bitmapSetter;
public void setBitmapSetter(BitmapSetter bitmapSetter) {
this.bitmapSetter = bitmapSetter;
}
public void setBitmap(T container, Bitmap bitmap) {
if (bitmapSetter != null) {
bitmapSetter.setBitmap(container, bitmap);
} else if (container instanceof ImageView) {
((ImageView) container).setImageBitmap(bitmap);
} else {
container.setBackgroundDrawable(new BitmapDrawable(container.getResources(), bitmap));
}
}
public void setDrawable(T container, Drawable drawable) {
if (bitmapSetter != null) {
bitmapSetter.setDrawable(container, drawable);
} else if (container instanceof ImageView) {
((ImageView) container).setImageDrawable(drawable);
} else {
container.setBackgroundDrawable(drawable);
}
}
public Drawable getDrawable(T container) {
if (bitmapSetter != null) {
return bitmapSetter.getDrawable(container);
} else if (container instanceof ImageView) {
return ((ImageView) container).getDrawable();
} else {
return container.getBackground();
}
}
}
以下是BitmapUtils的关键函数:
public void display(T container, String uri, BitmapDisplayConfig displayConfig, BitmapLoadCallBack callBack) {
if (container == null) {
return;
}
if (callBack == null) {
callBack = new DefaultBitmapLoadCallBack();
}
if (displayConfig == null || displayConfig == defaultDisplayConfig) {
displayConfig = defaultDisplayConfig.cloneNew();
}
// Optimize Max Size
BitmapSize size = displayConfig.getBitmapMaxSize();
displayConfig.setBitmapMaxSize(BitmapCommonUtils.optimizeMaxSizeByView(container, size.getWidth(), size.getHeight()));
container.clearAnimation();
if (TextUtils.isEmpty(uri)) {
callBack.onLoadFailed(container, uri, displayConfig.getLoadFailedDrawable());
return;
}
// start loading
callBack.onPreLoad(container, uri, displayConfig);
// find bitmap from mem cache.
Bitmap bitmap = globalConfig.getBitmapCache().getBitmapFromMemCache(uri, displayConfig);
if (bitmap != null) {
callBack.onLoadStarted(container, uri, displayConfig);
callBack.onLoadCompleted(
container,
uri,
bitmap,
displayConfig,
BitmapLoadFrom.MEMORY_CACHE);
} else if (!bitmapLoadTaskExist(container, uri, callBack)) {
final BitmapLoadTask loadTask = new BitmapLoadTask(container, uri, displayConfig, callBack);
// get executor
PriorityExecutor executor = globalConfig.getBitmapLoadExecutor();
File diskCacheFile = this.getBitmapFileFromDiskCache(uri);
boolean diskCacheExist = diskCacheFile != null && diskCacheFile.exists();
if (diskCacheExist && executor.isBusy()) {
executor = globalConfig.getDiskCacheExecutor();
}
// set loading image
Drawable loadingDrawable = displayConfig.getLoadingDrawable();
callBack.setDrawable(container, new AsyncDrawable(loadingDrawable, loadTask));
loadTask.setPriority(displayConfig.getPriority());
loadTask.executeOnExecutor(executor);
}
}