1、首先在当前程序的Application中调用ImageLoader的初始化init()方法
- private void initImageLoader() {
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).imageDownloader(
- new BaseImageDownloader(this, 60 * 1000, 60 * 1000))
- .build();
- ImageLoader.getInstance().init(config);
- }
2、下载图片的参数选项配置
-
-
-
-
-
-
-
-
-
- public static void displayImages(ImageView iv,String url,int defaultPic,boolean isRound,boolean cacheOnDisk){
-
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- .showImageOnLoading(defaultPic)
- .showImageForEmptyUri(defaultPic)
- .showImageOnFail(defaultPic)
- .cacheInMemory(false)
- .cacheOnDisk(cacheOnDisk)
- .considerExifParams(true)
- .displayer(isRound ? new CircleBitmapDisplayer() : new SimpleBitmapDisplayer())
- .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
- .bitmapConfig(Bitmap.Config.RGB_565)
- .build();
- imageLoader.displayImage(url, iv, options);
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();
- }