ImageLoader配置

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * 初始化ImageLoader 
  3.  */  
  4. public static void initImageLoader(Context context) {  
  5.     File cacheDir = StorageUtils.getOwnCacheDirectory(context,  
  6.             "bee_k77/Cache");// 获取到缓存的目录地址  
  7.     Log.e("cacheDir", cacheDir.getPath());  
  8.     // 创建配置ImageLoader(所有的选项都是可选的,只使用那些你真的想定制),这个可以设定在APPLACATION里面,设置为全局的配置参数  
  9.     ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(  
  10.             context)  
  11.             // max width, max height,即保存的每个缓存文件的最大长宽  
  12.             .memoryCacheExtraOptions(480800)  
  13.             // Can slow ImageLoader, use it carefully (Better don't use it)设置缓存的详细信息,最好不要设置这个  
  14. /                .discCacheExtraOptions(480800, CompressFormat.JPEG, 75null)   
  15.             // 线程池内加载的数量  
  16.             .threadPoolSize(3)  
  17.             // 线程优先级  
  18.             .threadPriority(Thread.NORM_PRIORITY - 2)  
  19.             /* 
  20.              * When you display an image in a small ImageView 
  21.              *  and later you try to display this image (from identical URI) in a larger ImageView  
  22.              *  so decoded image of bigger size will be cached in memory as a previous decoded image of smaller size. 
  23.              *  So the default behavior is to allow to cache multiple sizes of one image in memory.  
  24.              *  You can deny it by calling this method:  
  25.              *  so when some image will be cached in memory then previous cached size of this image (if it exists) 
  26.              *   will be removed from memory cache before. 
  27.              */  
  28. /               .denyCacheImageMultipleSizesInMemory()  
  29.               
  30.             // You can pass your own memory cache implementation你可以通过自己的内存缓存实现  
  31.             // .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))   
  32.             // .memoryCacheSize(2 * 1024 * 1024)  
  33.             //硬盘缓存50MB  
  34.             .diskCacheSize(50 * 1024 * 1024)  
  35.              //将保存的时候的URI名称用MD5  
  36.             .diskCacheFileNameGenerator(new Md5FileNameGenerator())  
  37.             // 加密  
  38.              .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//将保存的时候的URI名称用HASHCODE加密  
  39.             .tasksProcessingOrder(QueueProcessingType.LIFO)  
  40.              .diskCacheFileCount(100//缓存的File数量  
  41.             .diskCache(new UnlimitedDiscCache(cacheDir))// 自定义缓存路径  
  42.             // .defaultDisplayImageOptions(DisplayImageOptions.createSimple())  
  43.             // .imageDownloader(new BaseImageDownloader(context, 5 * 1000,  
  44.             // 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间  
  45.             .writeDebugLogs() // Remove for release app  
  46.             .build();  
  47.     // Initialize ImageLoader with configuration.  
  48.     ImageLoader.getInstance().init(config);// 全局初始化此配置  
  49. }  

Option类

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.topnews.config;  
  2.   
  3. import android.graphics.Bitmap;  
  4.   
  5. import com.nostra13.universalimageloader.core.DisplayImageOptions;  
  6. import com.nostra13.universalimageloader.core.assist.ImageScaleType;  
  7. import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;  
  8. import com.topnews.R;  
  9.   
  10. public class Options {  
  11.     /** 
  12.      * 新闻列表中用到的图片加载配置 
  13.      */  
  14.     public static DisplayImageOptions getListOptions() {  
  15.         DisplayImageOptions options = new DisplayImageOptions.Builder()  
  16.         // 设置图片在下载期间显示的图片  
  17.                 .showImageOnLoading(R.drawable.ic_stub)  
  18.                 // 设置图片Uri为空或是错误的时候显示的图片  
  19.                 .showImageForEmptyUri(R.drawable.ic_stub)  
  20.                 // 设置图片加载/解码过程中错误时候显示的图片  
  21.                 .showImageOnFail(R.drawable.ic_error)  
  22.                 // 设置下载的图片是否缓存在内存中  
  23.                 .cacheInMemory(false)  
  24.                 // 设置下载的图片是否缓存在SD卡中  
  25.                 .cacheOnDisc(true)  
  26.                 // 保留Exif信息  
  27.                 .considerExifParams(true)  
  28.                 // 设置图片以如何的编码方式显示  
  29.                 .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)  
  30.                 // 设置图片的解码类型  
  31.                 .bitmapConfig(Bitmap.Config.RGB_565)  
  32.                 // .decodingOptions(android.graphics.BitmapFactory.Options  
  33.                 // decodingOptions)//设置图片的解码配置  
  34.                 .considerExifParams(true)  
  35.                 // 设置图片下载前的延迟  
  36.                 .delayBeforeLoading(100)// int  
  37.                 // delayInMillis为你设置的延迟时间  
  38.                 // 设置图片加入缓存前,对bitmap进行设置  
  39.                 // .preProcessor(BitmapProcessor preProcessor)  
  40.                 .resetViewBeforeLoading(true)// 设置图片在下载前是否重置,复位  
  41.                 // .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角,弧度为多少  
  42.                 .displayer(new FadeInBitmapDisplayer(100))// 淡入  
  43.                 .build();  
  44.         return options;  
  45.     }  
  46. }  


代码中队配置讲解的和详细,我就不多说了。有空要研究下源码。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值