Android Imageloader的配置的实现代码
AndroidImageloader的配置的实现代码
ImageLoader优点
(1)支持下载进度监听
(2)可以在View滚动中暂停图片加载
通过PauseOnScrollListener接口可以在View滚动中暂停图片加载。
(3)默认实现多种内存缓存算法这几个图片缓存都可以配置缓存算法,不过ImageLoader默认实现了较多缓存算法,如Size
最大先删除、使用最少先删除、最近最少使用、先进先删除、时间最长先删除等。
(4)支持本地缓存文件名规则定义
实现代码:
/** *初始化ImageLoader */ publicstaticvoidinitImageLoader(Contextcontext){ FilecacheDir=StorageUtils.getOwnCacheDirectory(context, "bee_k77/Cache");//获取到缓存的目录地址 Log.e("cacheDir",cacheDir.getPath()); //创建配置ImageLoader(所有的选项都是可选的,只使用那些你真的想定制),这个可以设定在APPLACATION里面,设置为全局的配置参数 ImageLoaderConfigurationconfig=newImageLoaderConfiguration.Builder( context) //maxwidth,maxheight,即保存的每个缓存文件的最大长宽 .memoryCacheExtraOptions(480,800) //CanslowImageLoader,useitcarefully(Betterdon'tuseit)设置缓存的详细信息,最好不要设置这个 /.discCacheExtraOptions(480,800,CompressFormat.JPEG,75,null) //线程池内加载的数量 .threadPoolSize(3) //线程优先级 .threadPriority(Thread.NORM_PRIORITY-2) /* *WhenyoudisplayanimageinasmallImageView *andlateryoutrytodisplaythisimage(fromidenticalURI)inalargerImageView *sodecodedimageofbiggersizewillbecachedinmemoryasapreviousdecodedimageofsmallersize. *Sothedefaultbehavioristoallowtocachemultiplesizesofoneimageinmemory. *Youcandenyitbycallingthismethod: *sowhensomeimagewillbecachedinmemorythenpreviouscachedsizeofthisimage(ifitexists) *willberemovedfrommemorycachebefore. */ /.denyCacheImageMultipleSizesInMemory() //Youcanpassyourownmemorycacheimplementation你可以通过自己的内存缓存实现 //.memoryCache(newUsingFreqLimitedMemoryCache(2*1024*1024)) //.memoryCacheSize(2*1024*1024) //硬盘缓存50MB .diskCacheSize(50*1024*1024) //将保存的时候的URI名称用MD5 .diskCacheFileNameGenerator(newMd5FileNameGenerator()) //加密 .diskCacheFileNameGenerator(newHashCodeFileNameGenerator())//将保存的时候的URI名称用HASHCODE加密 .tasksProcessingOrder(QueueProcessingType.LIFO) .diskCacheFileCount(100)//缓存的File数量 .diskCache(newUnlimitedDiscCache(cacheDir))//自定义缓存路径 //.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) //.imageDownloader(newBaseImageDownloader(context,5*1000, //30*1000))//connectTimeout(5s),readTimeout(30s)超时时间 .writeDebugLogs()//Removeforreleaseapp .build(); //InitializeImageLoaderwithconfiguration. ImageLoader.getInstance().init(config);//全局初始化此配置 }
Option类
packagecom.topnews.config; importandroid.graphics.Bitmap; importcom.nostra13.universalimageloader.core.DisplayImageOptions; importcom.nostra13.universalimageloader.core.assist.ImageScaleType; importcom.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; importcom.topnews.R; publicclassOptions{ /** *新闻列表中用到的图片加载配置 */ publicstaticDisplayImageOptionsgetListOptions(){ DisplayImageOptionsoptions=newDisplayImageOptions.Builder() //设置图片在下载期间显示的图片 .showImageOnLoading(R.drawable.ic_stub) //设置图片Uri为空或是错误的时候显示的图片 .showImageForEmptyUri(R.drawable.ic_stub) //设置图片加载/解码过程中错误时候显示的图片 .showImageOnFail(R.drawable.ic_error) //设置下载的图片是否缓存在内存中 .cacheInMemory(false) //设置下载的图片是否缓存在SD卡中 .cacheOnDisc(true) //保留Exif信息 .considerExifParams(true) //设置图片以如何的编码方式显示 .imageScaleType(ImageScaleType.EXACTLY_STRETCHED) //设置图片的解码类型 .bitmapConfig(Bitmap.Config.RGB_565) //.decodingOptions(android.graphics.BitmapFactory.Options //decodingOptions)//设置图片的解码配置 .considerExifParams(true) //设置图片下载前的延迟 .delayBeforeLoading(100)//int //delayInMillis为你设置的延迟时间 //设置图片加入缓存前,对bitmap进行设置 //.preProcessor(BitmapProcessorpreProcessor) .resetViewBeforeLoading(true)//设置图片在下载前是否重置,复位 //.displayer(newRoundedBitmapDisplayer(20))//是否设置为圆角,弧度为多少 .displayer(newFadeInBitmapDisplayer(100))//淡入 .build(); returnoptions; } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!