1、首先在当前程序的Application中调用ImageLoader的初始化init()方法
private void initImageLoader() {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).imageDownloader(
new BaseImageDownloader(this, 60 * 1000, 60 * 1000)) // connectTimeout超时时间
.build();
ImageLoader.getInstance().init(config);
}
2、下载图片的参数选项配置
/**
* 调用该方法下载图片
* 配置imageLoader图片选项
* @param iv 显示图片控件
* @param url 网络或本地图片地址
* @param defaultPic 默认图片
* @param isRound true为圆形,false不处理
* @param cacheOnDisk true缓存到SD卡,false不缓存到SD卡
*/
public static void displayImages(ImageView iv,String url,int defaultPic,boolean isRound,boolean cacheOnDisk){
//配置一些图片选项
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(defaultPic)// 设置图片在下载期间显示的图片
.showImageForEmptyUri(defaultPic)// 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(defaultPic)// 设置图片加载/解码过程中错误时候显示的图片
.cacheInMemory(false)// 设置下载的图片是否缓存在内存中
.cacheOnDisk(cacheOnDisk)// 设置下载的图片是否缓存在SD卡中
.considerExifParams(true)//是否考虑JPEG图像EXIF参数(旋转,翻转)
.displayer(isRound ? new CircleBitmapDisplayer() : new SimpleBitmapDisplayer())//FadeInBitmapDisplayer(200)listview加载闪烁问题
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)//图片将降低2倍,直到下一减少步骤,使图像更小的目标大小
.bitmapConfig(Bitmap.Config.RGB_565)//图片色彩565
.build();
imageLoader.displayImage(url, iv, options); //加载网络图片url
3、扩展,图片显示方式,圆角;CircleBitmapDisplayer()
private static class CircleBitmapDisplayer implements BitmapDisplayer {
final int margin ;
public CircleBitmapDisplayer() {
this(0);
}
public CircleBitmapDisplayer(int margin) {
this.margin = margin;
}
@Override
public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
if (!(imageAware instanceof ImageViewAware)) {
throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
}
imageAware.setImageBitmap(ToRoundBitmap.toRoundBitmap(bitmap));
}
}
4、返回圆形bitmap;toRoundBitmap()
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
left = 0;
bottom = width;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(4);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output ;
}
5、根据图片uri返回bitmap;此缓存位置为内存
public static Bitmap getBitmapUtils(String imgUri){
return imageLoader.getMemoryCache().get(imgUri);
}
6、根据图片uri返回File;此缓存位置为sd卡
public static File getFileUtils(String imgUri){
return imageLoader.getDiskCache().get(imgUri);
}
7、获取imageloader缓存所有图片总计大小
public static long getCacheFileSize(){
File disCacheFile = imageLoader.getDiskCache().getDirectory();
long size = 0;
for(int i=0; i<disCacheFile.listFiles().length; i++){
size += disCacheFile.listFiles()[i].length();
}
return size;
}
8、清除图片缓存
public static void clearImageCache(){
imageLoader.clearDiskCache();//清除磁盘缓存
imageLoader.clearMemoryCache();//清除内存缓存
}