1、首先在當(dāng)前程序的Application中調(diào)用ImageLoader的初始化init()方法
private void initImageLoader() {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).imageDownloader(
new BaseImageDownloader(this, 60 * 1000, 60 * 1000)) // connectTimeout超時(shí)時(shí)間
.build();
ImageLoader.getInstance().init(config);
}
2、下載圖片的參數(shù)選項(xiàng)配置
/**
* 調(diào)用該方法下載圖片
* 配置imageLoader圖片選項(xiàng)
* @param iv 顯示圖片控件
* @param url 網(wǎng)絡(luò)或本地圖片地址
* @param defaultPic 默認(rèn)圖片
* @param isRound true為圓形,false不處理
* @param cacheOnDisk true緩存到SD卡,false不緩存到SD卡
*/
public static void displayImages(ImageView iv,String url,int defaultPic,boolean isRound,boolean cacheOnDisk){
//配置一些圖片選項(xiàng)
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(defaultPic)// 設(shè)置圖片在下載期間顯示的圖片
.showImageForEmptyUri(defaultPic)// 設(shè)置圖片Uri為空或是錯(cuò)誤的時(shí)候顯示的圖片
.showImageOnFail(defaultPic)// 設(shè)置圖片加載/解碼過(guò)程中錯(cuò)誤時(shí)候顯示的圖片
.cacheInMemory(false)// 設(shè)置下載的圖片是否緩存在內(nèi)存中
.cacheOnDisk(cacheOnDisk)// 設(shè)置下載的圖片是否緩存在SD卡中
.considerExifParams(true)//是否考慮JPEG圖像EXIF參數(shù)(旋轉(zhuǎn),翻轉(zhuǎn))
.displayer(isRound ? new CircleBitmapDisplayer() : new SimpleBitmapDisplayer())//FadeInBitmapDisplayer(200)listview加載閃爍問(wèn)題
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)//圖片將降低2倍,直到下一減少步驟,使圖像更小的目標(biāo)大小
.bitmapConfig(Bitmap.Config.RGB_565)//圖片色彩565
.build();
imageLoader.displayImage(url, iv, options); //加載網(wǎng)絡(luò)圖片url
3、擴(kuò)展,圖片顯示方式,圓角;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、根據(jù)圖片uri返回bitmap;此緩存位置為內(nèi)存
public static Bitmap getBitmapUtils(String imgUri){
return imageLoader.getMemoryCache().get(imgUri);
}
6、根據(jù)圖片uri返回File;此緩存位置為sd卡
public static File getFileUtils(String imgUri){
return imageLoader.getDiskCache().get(imgUri);
}
7、獲取imageloader緩存所有圖片總計(jì)大小
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();//清除內(nèi)存緩存
}