直接上代码缓存
public class ImageLoad { private Context context; private ImageLoaderConfiguration config; private ImageLoader loader = ImageLoader.getInstance(); private DisplayImageOptions options; public ImageLoad(Context context) { this.context = context; initImageLoader(this.context, false); } public ImageLoad(Context context, boolean isAvater) { this.context = context; initImageLoader(this.context, isAvater); } /** * 加载原图 * * @param iv * @param url */ public void loadImage(final ImageView iv, final String url) { try { // iv.setTag(url); loader.displayImage(url, iv, options); // loader.loadImage(url, options, new SimpleImageLoadingListener() { // // @Override // public void onLoadingComplete(String imageUri, View view, // Bitmap loadedImage) { // super.onLoadingComplete(imageUri, view, loadedImage); // if (url.equals(iv.getTag())) { // if (iv != null) { // iv.setImageBitmap(loadedImage); // } // } // } // }); } catch (Exception e) { LogUtil.e(getClass(), "loadImage()", e); } } /** * 加载指定大小图片 * * @param iv * @param url * @param width * @param height */ public void loadImage(final ImageView iv, final String url, int width, int height) { try { // iv.setTag(url); // ImageSize imageSize = new ImageSize(width, height); // loader.displayImage(url, iv, options); // loader.loadImage(url, imageSize, options, // new SimpleImageLoadingListener() { // // @Override // public void onLoadingComplete(String imageUri, // View view, Bitmap loadedImage) { // super.onLoadingComplete(imageUri, view, loadedImage); // if (url.equals(iv.getTag())) { // iv.setImageBitmap(loadedImage); // } // } // }); loader.displayImage(url, iv, options); } catch (Exception e) { LogUtil.e( getClass(), "loadImage(final ImageView iv, final String url, int width,int height)", e); } } public void clearCache() { try { if (loader != null) { // loader.clearDiskCache(); loader.clearMemoryCache(); } } catch (Exception e) { LogUtil.e(getClass(), "clearCache()", e); } } /** * 初始化ImageLoader数据 */ private void initImageLoader(Context context, boolean isAvater) { try { options = new DisplayImageOptions.Builder() .bitmapConfig(Bitmap.Config.RGB_565) .imageScaleType(ImageScaleType.EXACTLY) .showImageOnLoading(isAvater ? R.drawable.default_avater : R.drawable.home_page_pic) .showImageForEmptyUri(isAvater ? R.drawable.default_avater : R.drawable.home_page_pic) .showImageOnFail(isAvater ? R.drawable.default_avater : R.drawable.home_page_pic) .cacheInMemory(true).cacheOnDisk(true).build(); File cacheDir = StorageUtils.getCacheDirectory(context); // 设置缓存路径和缓存文件的数量,超过数量后,old将被删除 config = new ImageLoaderConfiguration.Builder(context) .threadPoolSize(5) // 设定线程池的大小 .threadPriority(Thread.NORM_PRIORITY - 1) .denyCacheImageMultipleSizesInMemory() .diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(100) .diskCache(new UnlimitedDiskCache(cacheDir)) .defaultDisplayImageOptions(options) .memoryCache(new WeakMemoryCache()) .memoryCacheSize(2 * 1024 * 1024) .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO).build(); ImageLoader.getInstance().init(config); } catch (Exception e) { LogUtil.e(getClass(), "initImageLoader(Context context)", e); } } }