ImageLoader 的简单使用配置,最好是将配置信息放到application里面,这样我们就不需要每次使用都需要配置了
1、首先我们得有一个包
2、简单的配置信息
1//显示图片的配置2DisplayImageOptions options =newDisplayImageOptions.Builder()3.showImageOnLoading(R.drawable.ofm_photo_icon)//下载时显示的图片4.showImageForEmptyUri(R.drawable.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片5.showImageOnFail(R.drawable.main_head_erroy_loader)//下载 错误 显示的图片6.cacheInMemory(true)//加入 到缓存7.cacheOnDisk(true)//写入 内存卡8.bitmapConfig(Bitmap.Config.RGB_565)//图片清晰度9.imageScaleType(ImageScaleType.EXACTLY)//设置图片以如何的编码方式显示10.build();11cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(),getPackageName()+"/imageCatch");//设置缓存的路径12ImageLoaderConfiguration config =newImageLoaderConfiguration13.Builder(getApplicationContext())14.memoryCacheExtraOptions(480, 800)//max width, max height,即保存的每个缓存文件的最大长宽15.threadPoolSize(3)//线程池内加载的数量16.threadPRiority(Thread.NORM_PRIORITY - 2)17.denyCacheImageMultipleSizesInMemory()18//设置内存缓存的大小为手机运行内存的 1/819.memoryCacheSize((int) Runtime.getRuntime().maxMemory() / 8)20.diskCacheSize(10 * 1024 * 1024)//缓存大小 10M21.diskCacheFileNameGenerator(newHashCodeFileNameGenerator())//将保存的时候的URI名称用 加密22.tasksProcessingOrder(QueueProcessingType.LIFO)23.diskCacheFileCount(100)//缓存的文件数量24.diskCache(newUnlimitedDiskCache(cacheDir))//自定义缓存路径25.defaultDisplayImageOptions(DisplayImageOptions.createSimple())26.imageDownloader(newBaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000))//connectTimeout (30 s), readTimeout (30 s)超时时间27.defaultDisplayImageOptions(options)28//.writeDebugLogs()//Remove for release app29.build();//开始构建30//初始化ImageLoader31ImageLoader.getInstance().init(config);
3、使用ImageLoader
第一个参数为图片的Url,第二个参数为显示图片的ImageView。
//网络请求加载图片ImageLoader.getInstance().displayImage(newsList.get(position).getImg(), imageView);
若显示的图片配置信息和application配置的不同,可以自己再次另行设置
1privatevoidloadImage(ImageView imageView,intposition){2//显示图片的配置3DisplayImageOptions options =newDisplayImageOptions.Builder()4.showImageOnLoading(R.drawable.ofm_photo_icon)//下载时显示的图片5.showImageForEmptyUri(R.drawable.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片6.showImageOnFail(R.drawable.card_photofail)//下载 错误 显示的图片7.cacheInMemory(true)//加入 到缓存8.cacheOnDisk(true)//写入 内存卡9.bitmapConfig(Bitmap.Config.RGB_565)//图片清晰度10.imageScaleType(ImageScaleType.EXACTLY)//设置图片以如何的编码方式显示11.build();12//网络请求加载图片13ImageLoader.getInstance().displayImage(newsList.get(position).getImg(), imageView, options);14}