Picasso开源图片加载利器使用初探
2015-02-12 11:35
316 查看
英文介绍链接地址 : http://square.github.io/picasso/
Picasso 英文意思国外一个很有名的画家毕加索的名字,国外项目取名还是很有意思的!
从github新下载的picasso项目有依赖其他第三方开源项目okhttp和okio,这两个项目也是相当经典的,据说okhttp里网络请求的代码处理逻辑已经加入到android4点几的源码中了。
picasso也提供了封装好了的jar包可以使用,这样就不需要导入okhttp和okio项目了,但是看jar包里的OkHttpDownloader这个类还是引用了okhttp里的对象,可是在jar包里并没找到,不知道为什么~谁能解释下啊?
Picasso使用的方法汇总:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso.with(context).load(url).into(view);
Picasso.with(context).load(url) .resize(50, 50).centerCrop().into(imageView)
//这里的placeholder将resource传入通过getResource.getDrawable取资源,所以可以是张图片也可以是color id
Picasso.with(context).load(url).placeholder(R.drawable.user_placeholder).error(R.drawable.user_placeholder_error).into(imageView);
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
//这里显示notification的图片
Picasso.with(activity).load(Data.URLS[new Random().nextInt(Data.URLS.length)]).resizeDimen(R.dimen.notification_icon_width_height,
R.dimen.notification_icon_width_height).into(remoteViews, R.id.photo, NOTIFICATION_ID, notification);
//这里是通过设置tag标签,就是当前传过来的context,这样就可以根据这个context tag来pause和resume显示了
Picasso.with(context).load(url).placeholder(R.drawable.placeholder).error(R.drawable.error).fit().tag(context).into(view);
//监听onScrollStateChanged的时候调用执行
picasso.resumeTag(context);
picasso.pauseTag(context);
Picasso.with(context).load(contactUri).placeholder(R.drawable.contact_picture_placeholder).tag(context).into(holder.icon);
//这个onpause方法里的这段代码还是很有意思的
@Override protected void onPause() {
super.onPause();
if (isFinishing()) {
// Always cancel the request here, this is safe to call even if the image has been loaded.
// This ensures that the anonymous callback we have does not prevent the activity from
// being garbage collected. It also prevents our callback from getting invoked even after the
// activity has finished.
Picasso.with(this).cancelRequest(imageView);
}
}
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
.centerInside()
.tag(context)
.into(holder.image);
//Picasso.with使用的是单例模式
Picasso.with(this).cancelTag(this);
然后呢,Picasso还提供了debug的标示,调用picasso的setIndicatorsEnabled方法,true是debug模式,跟踪代码其实就是在最后生成的PicassoDrawable类的ondraw里绘制了个左上角小三角,根据
public enum LoadedFrom {
MEMORY(Color.GREEN),
DISK(Color.BLUE),
NETWORK(Color.RED);
枚举里的不同值标示不同加载来源,这对分析图片加载有好处。
另外链接个分析picasso源码不错的博客地址http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0731/1639.html
不要感谢我哦!
Picasso真心是个很强大的图片加载框架,之前用过universal-Image-Loader图片加载框架也挺不错的
开源的力量很强大啊☻
Picasso 英文意思国外一个很有名的画家毕加索的名字,国外项目取名还是很有意思的!
从github新下载的picasso项目有依赖其他第三方开源项目okhttp和okio,这两个项目也是相当经典的,据说okhttp里网络请求的代码处理逻辑已经加入到android4点几的源码中了。
picasso也提供了封装好了的jar包可以使用,这样就不需要导入okhttp和okio项目了,但是看jar包里的OkHttpDownloader这个类还是引用了okhttp里的对象,可是在jar包里并没找到,不知道为什么~谁能解释下啊?
Picasso使用的方法汇总:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso.with(context).load(url).into(view);
Picasso.with(context).load(url) .resize(50, 50).centerCrop().into(imageView)
//这里的placeholder将resource传入通过getResource.getDrawable取资源,所以可以是张图片也可以是color id
Picasso.with(context).load(url).placeholder(R.drawable.user_placeholder).error(R.drawable.user_placeholder_error).into(imageView);
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
//这里显示notification的图片
Picasso.with(activity).load(Data.URLS[new Random().nextInt(Data.URLS.length)]).resizeDimen(R.dimen.notification_icon_width_height,
R.dimen.notification_icon_width_height).into(remoteViews, R.id.photo, NOTIFICATION_ID, notification);
//这里是通过设置tag标签,就是当前传过来的context,这样就可以根据这个context tag来pause和resume显示了
Picasso.with(context).load(url).placeholder(R.drawable.placeholder).error(R.drawable.error).fit().tag(context).into(view);
//监听onScrollStateChanged的时候调用执行
picasso.resumeTag(context);
picasso.pauseTag(context);
Picasso.with(context).load(contactUri).placeholder(R.drawable.contact_picture_placeholder).tag(context).into(holder.icon);
//这个onpause方法里的这段代码还是很有意思的
@Override protected void onPause() {
super.onPause();
if (isFinishing()) {
// Always cancel the request here, this is safe to call even if the image has been loaded.
// This ensures that the anonymous callback we have does not prevent the activity from
// being garbage collected. It also prevents our callback from getting invoked even after the
// activity has finished.
Picasso.with(this).cancelRequest(imageView);
}
}
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
.centerInside()
.tag(context)
.into(holder.image);
//Picasso.with使用的是单例模式
Picasso.with(this).cancelTag(this);
然后呢,Picasso还提供了debug的标示,调用picasso的setIndicatorsEnabled方法,true是debug模式,跟踪代码其实就是在最后生成的PicassoDrawable类的ondraw里绘制了个左上角小三角,根据
public enum LoadedFrom {
MEMORY(Color.GREEN),
DISK(Color.BLUE),
NETWORK(Color.RED);
枚举里的不同值标示不同加载来源,这对分析图片加载有好处。
另外链接个分析picasso源码不错的博客地址http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0731/1639.html
不要感谢我哦!
Picasso真心是个很强大的图片加载框架,之前用过universal-Image-Loader图片加载框架也挺不错的
开源的力量很强大啊☻
相关文章推荐
- Picasso开源图片加载利器使用初探
- Picasso开源图片加载利器
- android 开源库之图片加载-Universal-Imageoader和Picasso的使用和比较
- 使用开源imageLoader优化listview加载大量本地图片的demo
- (4.2.10.1)【android开源工具】Android Volley完全解析(二),使用Volley加载网络图片
- 使用Picasso加载图片的内存优化实践
- 使用开源imageLoader优化listview加载大量本地图片的demo
- 图片加载利器Picasso 解析
- 使用开源imageLoader优化listview加载大量本地图片的demo
- 用Facebook开源框架Frescok加载网络图片的简单使用
- 图片加载与缓存利器(自动缓存)--第三方开源-- Glide
- 使用自定义的item、Adapter和AsyncTask、第三方开源框架PullToRefresh联合使用实现自定义的下拉列表(从网络加载图片显示在item中的ImageView)
- picasso--不得不看的异步图片加载与缓存开源库
- FaceBook开源图片加载框架Fresco的学习使用(一), Fresco下载与简单使用
- 使用自定义的item、Adapter和AsyncTask、第三方开源框架PullToRefresh联合使用实现自定义的下拉列表(从网络加载图片显示在item中的ImageView)
- IOS学习之路二十三(EGOImageLoading异步加载图片开源框架使用)
- Android使用开源框架加载图片
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片的优缺点比较
- android网络图片加载处理库Picasso和universalimageloader使用简单案例
- 使用Picasso加载图片的内存优化实践