1、首先在當前程序的Application中調用ImageLoader的初始化init()方法
[java] view plain copy
- private void initImageLoader() {
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).imageDownloader(
- new BaseImageDownloader(this, 60 * 1000, 60 * 1000)) // connectTimeout超時時間
- .build();
- ImageLoader.getInstance().init(config);
- }
2、下載圖片的參數選項配置
[java] view plain copy
- /**
- * 調用該方法下載圖片
- * 配置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);
3、擴展,圖片顯示方式,圓角;CircleBitmapDisplayer()
[java] view plain copy
- 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()
[java] view plain copy
- 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;此緩存位置為內存
[java] view plain copy
- public static Bitmap getBitmapUtils(String imgUri){
- return imageLoader.getMemoryCache().get(imgUri);
- }
6、根據圖片uri返回File;此緩存位置為sd卡
[java] view plain copy
- public static File getFileUtils(String imgUri){
- return imageLoader.getDiskCache().get(imgUri);
- }
7、獲取imageloader緩存所有圖片總計大小
[java] view plain copy
- 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、清除圖片緩存
[java] view plain copy
- public static void clearImageCache(){
- imageLoader.clearDiskCache();//清除磁盤緩存
- imageLoader.clearMemoryCache();//清除內存緩存
- }
