一、概述
1、简介
Picasso是Square公司出品的为Android提供一个强大的图像下载和缓存库。
2、Picasso特点
1)Picasso解决,在adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位的问题。
2)使用复杂的图片压缩转换来尽可能的减少内存消耗。
3)自带内存和硬盘二级缓存功能。
3、使用场景
- 加载图片不能错位;
- 资源消耗要低;
- 加载速度要快;
- 异步加载图片。
4、官方参考
GitHub地址:https://github.com/square/picasso
二、使用Picasso处理图片
1、依赖库
compile 'com.squareup.picasso:picasso:2.5.2'
如果需要对图片变化处理,则需要依赖库
compile 'jp.wasabeef:picasso-transformations:2.1.0'
2、Picasso基本用法
// 普通加载图片 Picasso.with(this).load(url).into(ivPicassoResult1);
3、使用方式整理
// 裁剪的方式加载图片
Picasso.with(MainActivity.this)
.load("http://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg")
.resize(100, 100)//裁剪图片大小
.into(ivPicassoResult2);
// 选择180度旋转
Picasso.with(MainActivity.this) .load("http://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg")
.rotate(180)//旋转设置
.into(ivPicassoResult3);
// load SD卡资源
Picasso.with(MainActivity.this)
.load("file://+Environment.getExternalStorageDirectory().getPath()+/meinv1.jpg")
.placeholder(R.mipmap.ic_launcher)//资源加载过程中的显示的图片
.error(R.mipmap.ic_launcher)//设置load失败时显示的Drawable。
.into(ivPicassoResult4);
// load assets资源:
Picasso.with(MainActivity.this)
.load("file:///android_asset/demo.jpg")
.placeholder(R.mipmap.ic_launcher)//资源加载过程中的显示的图片
.error(R.mipmap.ic_launcher)//设置load失败时显示的Drawable。
.into(ivPicassoResult5);
// load drawable资源:
Picasso.with(MainActivity.this)
.load(R.drawable.demo)
.placeholder(R.mipmap.ic_launcher)//资源加载过程中的显示的图片
.error(R.mipmap.ic_launcher)//设置load失败时显示的Drawable。
.into(ivPicassoResult6);
4、使用Transformations对图片进行变换处理
Picasso.with(context)
.load(R.drawable.check)
.resize(width, height)
.centerCrop()
.transform((new MaskTransformation(context,R.drawable.mask_starfish)))
.into(holder.image);
picasso-transformations库地址:
https://github.com/wasabeef/picasso-transformations
三、Demo
1、Demo效果
2、Demo地址:
https://github.com/TDCQZD/PicassoTest
四、源码解析
推荐博客:
http://www.codeceo.com/article/android-picasso.html
自定义缓存设置:
http://blog.csdn.net/u012702547/article/details/52273918
结束语:
首先感谢尚硅谷无偿提供的学习资料。写博客是为了帮助开发者学习使用技术,同时巩固自己所学技术。如果此篇博客有助于您的学习,那是我的荣幸!如果此篇博客有任何瑕疵,请多多指教!在此感谢您的学习和指教!