SimpleCommand框架ImageLoader API详解(三)
ImageLoaderAPI详细介绍,具体内容如下
在ImageLoader中有以下几个不同的构造器:
/**
*注意:次构造器不支持下载进度提示功能
*@paramcontext
*@paramwithCache是否支持缓存
*false--不带缓存
*true--支持缓存功能,默认缓存路径在外置存储缓冲目录中的picasso-big-cache文件夹中
*/
publicImageLoader(Contextcontext,booleanwithCache){
this(context,null,withCache);
}
/**
*支持下载进度提示,以及设置缓存路径
*@paramcontext
*@paramlistener下载进度监听器
*@paramcachePath缓存路径字符串
*/
publicImageLoader(Contextcontext,ProgressListenerlistener,StringcachePath){
//TODOextendtosupportmultiplelibrariesasGlide
//TODOmustbeinitializedandkeptasanmemberinstancetoavoidlosingcache
Picasso.Builderbuilder=setupLoaderClientWithCachePath(context,listener,cachePath);
setupListener(builder);
picasso=builder.build();
picasso.setIndicatorsEnabled(BuildConfig.DEBUG);
picasso.setLoggingEnabled(BuildConfig.DEBUG);
}
/**
*支持下载进度提示,以及设置缓存路径为默认路径picasso-big-cache
*@paramcontext
*@paramlistener下载进度监听器
*@paramwithCache是否支持缓存
*/
publicImageLoader(Contextcontext,ProgressListenerlistener,booleanwithCache){
//TODOextendtosupportmultiplelibrariesasGlide
//TODOmustbeinitializedandkeptasanmemberinstancetoavoidlosingcache
Picasso.Builderbuilder=setupLoaderClient(context,listener,withCache);
setupListener(builder);
picasso=builder.build();
picasso.setIndicatorsEnabled(BuildConfig.DEBUG);
picasso.setLoggingEnabled(BuildConfig.DEBUG);
}
publicvoidshutdown(){
if(picasso==null)return;
Logger.d(Logger.TAG,"Imageloaderhasbeenshutdown");
picasso.shutdown();
callback=null;
}
将ImageLoader停止,一般在Activity停止,或者任务结束时调用此方法将其关闭
publicImageLoadercancelRequest(ImageViewimageView){
picasso.cancelRequest(imageView);
returnthis;
}
取消之前显示到ImageView上的请求
publicImageLoaderload(finalintresourceId){
cleanResources();
this.imageResourceId=resourceId;
returnthis;
}
publicImageLoaderload(finalStringimageUri){
cleanResources();
this.imageUri=imageUri;
returnthis;
}
分别加载本地drawable文件夹下的图片,以及网络图片
publicImageLoaderwithPlaceholder(finalintplaceholder){
this.placeholder=placeholder;
returnthis;
}
设置ImageLoader下载图片时的预览图
publicImageLoaderwithErrorImage(finalinterrorImage){
this.errorImage=errorImage;
returnthis;
}
下载图片失败时显示的图片
publicImageLoaderwithTag(finalStringtag){
this.tag=tag;
returnthis;
}
publicvoidpause(finalStringtag){
if(picasso==null)return;
picasso.pauseTag(tag);
}
publicvoidresume(finalStringtag){
if(picasso==null)return;
picasso.resumeTag(tag);
}
以上三个方法依次是
1下载图片时添加标签tag
2暂停tag标签的下载任务
3resumetag标签的下载任务
publicImageLoaderwithCallback(finalCallbackcallback){
this.callback=callback;
returnthis;
}
给ImageLoader设置下载完成的回调,包含onSuccess和onFailed方法
publicImageLoaderfit(){
this.fit=true;
returnthis;
}
publicImageLoadercenterCrop(){
this.centerCrop=true;
returnthis;
}
publicImageLoadercenterInside(){
this.centerInside=true;
returnthis;
}
publicImageLoaderresize(finalintwidthResId,finalintheightResId){
this.widthResId=widthResId;
this.heigthResId=heightResId;
returnthis;
}
分别设置Picasso下载图片时的相应属性,可以参考ImageView的scaleType属性
publicvoidinto(finalImageViewimageView){
run(imageView);
}
into方法调用内部run方法,并启动下载任务。此方法需要在以上所有的API之后调用。
框架github地址:SimpleCommand框架
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。