随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
转自:http://blog.csdn.net/rain_butterfly/article/details/37812371
private void loadBitmap(){
BitmapUtils bitmapUtils = new BitmapUtils(this);
// 加载网络图片,以下都可以配置BitmapDisplayConfig,BitmapLoadCallBack
// bitmapUtils.display(imgView, "http://bbs.lidroid.com/static/image/common/logo.png");
// 加载本地图片(路径以/开头, 绝对路径)
bitmapUtils.display(imgView, Environment.getExternalStorageDirectory()+"/ic_launcher.png");
//
// // 加载assets中的图片(路径以assets开头)
// bitmapUtils.display(imgView, "assets/img/wallpaper.jpg");
// 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
// listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
// listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true, customListener));
}
自定义加载图片类,设置了默认图片、加载失败时的图片和压缩类型以及动画效果:
package com.home.testxutils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.widget.ImageView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.bitmap.callback.DefaultBitmapLoadCallBack;
public class XUtilsImageLoader {//框架里面设置了缓存和异步操作,不用单独设置线程池和缓存机制(也可以自定义缓存路径)
private BitmapUtils bitmapUtils;
private Context mContext;
public XUtilsImageLoader(Context context) {
this.mContext = context;
bitmapUtils = new BitmapUtils(mContext);
bitmapUtils.configDefaultLoadingImage(R.drawable.defaultimg);//默认背景图片
bitmapUtils.configDefaultLoadFailedImage(R.drawable.defaultimg);//加载失败图片
bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型
}
/**
*
* @author sunglasses
* @category 图片回调函数
*/
public class CustomBitmapLoadCallBack extends
DefaultBitmapLoadCallBack<ImageView> {
@Override
public void onLoading(ImageView container, String uri,
BitmapDisplayConfig config, long total, long current) {
}
@Override
public void onLoadCompleted(ImageView container, String uri,
Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
// super.onLoadCompleted(container, uri, bitmap, config, from);
fadeInDisplay(container, bitmap);
}
@Override
public void onLoadFailed(ImageView container, String uri,
Drawable drawable) {
}
}
private static final ColorDrawable TRANSPARENT_DRAWABLE = new ColorDrawable(
android.R.color.transparent);
/**
* @author sunglasses
* @category 图片加载效果
* @param imageView
* @param bitmap
*/
private void fadeInDisplay(ImageView imageView, Bitmap bitmap) {//目前流行的渐变效果
final TransitionDrawable transitionDrawable = new TransitionDrawable(
new Drawable[] { TRANSPARENT_DRAWABLE,
new BitmapDrawable(imageView.getResources(), bitmap) });
imageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(500);
}
public void display(ImageView container,String url){//外部接口函数
bitmapUtils.display(container, url,new CustomBitmapLoadCallBack());
}
}