ImageLoader初始化以及调用

1、首先在当前程序的Application中调用ImageLoader的初始化init()方法

[java]  view plain  copy
  1. private void initImageLoader() {  
  2.         ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).imageDownloader(  
  3.                 new BaseImageDownloader(this60 * 100060 * 1000)) // connectTimeout超时时间  
  4.                 .build();  
  5.         ImageLoader.getInstance().init(config);  
  6.     }  


2、下载图片的参数选项配置

[java]  view plain  copy
  1.  /** 
  2.   * 调用该方法下载图片 
  3.   * 配置imageLoader图片选项 
  4.   * @param iv  显示图片控件 
  5.   * @param url  网络或本地图片地址 
  6.   * @param defaultPic  默认图片 
  7.   * @param isRound  true为圆形,false不处理 
  8.   * @param cacheOnDisk true缓存到SD卡,false不缓存到SD卡 
  9.   */  
  10. public static void displayImages(ImageView iv,String url,int defaultPic,boolean isRound,boolean cacheOnDisk){  
  11. //配置一些图片选项  
  12. DisplayImageOptions options = new DisplayImageOptions.Builder()  
  13. .showImageOnLoading(defaultPic)// 设置图片在下载期间显示的图片  
  14. .showImageForEmptyUri(defaultPic)// 设置图片Uri为空或是错误的时候显示的图片  
  15. .showImageOnFail(defaultPic)// 设置图片加载/解码过程中错误时候显示的图片  
  16. .cacheInMemory(false)// 设置下载的图片是否缓存在内存中  
  17. .cacheOnDisk(cacheOnDisk)// 设置下载的图片是否缓存在SD卡中  
  18. .considerExifParams(true)//是否考虑JPEG图像EXIF参数(旋转,翻转)  
  19. .displayer(isRound ? new CircleBitmapDisplayer() : new SimpleBitmapDisplayer())//FadeInBitmapDisplayer(200)listview加载闪烁问题  
  20. .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)//图片将降低2倍,直到下一减少步骤,使图像更小的目标大小  
  21. .bitmapConfig(Bitmap.Config.RGB_565)//图片色彩565  
  22. .build();  
  23. imageLoader.displayImage(url, iv, options);  

 
   

3、扩展,图片显示方式,圆角;CircleBitmapDisplayer()

[java]  view plain  copy
  1. private static class CircleBitmapDisplayer implements BitmapDisplayer {  
  2.             final int margin ;  
  3.             public CircleBitmapDisplayer() {  
  4.                 this(0);  
  5.             }  
  6.             public CircleBitmapDisplayer(int margin) {  
  7.                 this.margin = margin;  
  8.             }  
  9.             @Override  
  10.             public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {  
  11.                 if (!(imageAware instanceof ImageViewAware)) {  
  12.                     throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");  
  13.                 }  
  14.                 imageAware.setImageBitmap(ToRoundBitmap.toRoundBitmap(bitmap));  
  15.             }  
  16.      }  

4、返回圆形bitmap;toRoundBitmap()

[java]  view plain  copy
  1. public static  Bitmap toRoundBitmap(Bitmap bitmap) {  
  2.          int width = bitmap.getWidth();  
  3.          int height = bitmap.getHeight();  
  4.          float roundPx;  
  5.          float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;  
  6.          if (width <= height) {  
  7.               roundPx = width / 2;  
  8.               top = 0;  
  9.               left = 0;  
  10.               bottom = width;  
  11.               right = width;  
  12.               height = width;  
  13.               dst_left = 0;  
  14.               dst_top = 0;  
  15.               dst_right = width;  
  16.               dst_bottom = width;  
  17.           } else {  
  18.               roundPx = height / 2;  
  19.               float clip = (width - height) / 2;  
  20.               left = clip;  
  21.               right = width - clip;  
  22.               top = 0;  
  23.               bottom = height;  
  24.               width = height;  
  25.               dst_left = 0;  
  26.               dst_top = 0;  
  27.               dst_right = height;  
  28.               dst_bottom = height;  
  29.          }  
  30.   
  31.             Bitmap output = Bitmap.createBitmap(width,height, Config.ARGB_8888);  
  32.             Canvas canvas = new Canvas(output);  
  33.               
  34.             final int color = 0xff424242;  
  35.             final Paint paint = new Paint();  
  36.             final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);  
  37.             final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);  
  38.             final RectF rectF = new RectF(dst);  
  39.   
  40.             paint.setAntiAlias(true);  
  41.   
  42.             canvas.drawARGB(0000);  
  43.             paint.setColor(Color.WHITE);  
  44.               
  45.             paint.setStrokeWidth(4);   
  46.             canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  47.             paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  48.             canvas.drawBitmap(bitmap, src, dst, paint);  
  49.               
  50.             return output ;   
  51.         }  


5、根据图片uri返回bitmap;此缓存位置为内存

[java]  view plain  copy
  1. public static Bitmap getBitmapUtils(String imgUri){  
  2.         return imageLoader.getMemoryCache().get(imgUri);  
  3.     }  


6、根据图片uri返回File;此缓存位置为sd卡

[java]  view plain  copy
  1. public static File getFileUtils(String imgUri){  
  2.         return imageLoader.getDiskCache().get(imgUri);  
  3.     }  

7、获取imageloader缓存所有图片总计大小

[java]  view plain  copy
  1. public static long getCacheFileSize(){  
  2.         File disCacheFile = imageLoader.getDiskCache().getDirectory();  
  3.         long size = 0;  
  4.         for(int i=0; i<disCacheFile.listFiles().length; i++){  
  5.             size += disCacheFile.listFiles()[i].length();  
  6.         }  
  7.         return size;  
  8.     }  

8、清除图片缓存

[java]  view plain  copy
  1. public static void clearImageCache(){  
  2.         imageLoader.clearDiskCache();//清除磁盘缓存  
  3.         imageLoader.clearMemoryCache();//清除内存缓存  
  4.     }  





转载于:https://www.cnblogs.com/jeffen/p/6595746.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值