上一篇介绍了Glide的使用,今天介绍一下Picasso的简单使用,以便自己以后使用。其实两种图片加载框架基本相同的
参考:https://www.jianshu.com/p/c68a3b9ca07a 介绍的很详细,我这里简单写下能经常用到的以及感觉不错的
添加Picasso依赖
compile 'com.squareup.picasso:picasso:2.5.2'
1. 网络加载图片
.with(Context) 单例传入上下文
.load(Uri),load(File),load(int),load(String)分别对应uri,图片文件,本地资源图片,string类型的uri
.into() 则是赋值的View
Picasso.with(this).load(uri).into(mImg);
2. .placeholder() 网络加载图片时 设置默认图片
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.into(mImg);
3. .error() 网络加载图片时出错 设置错误是默认图片
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(mImg);
4. .noPlaceholder() 设置View不是占位图 需要注意的是.placeholder()和.noPlaceholder()不能同时应用在同一个请求上
Picasso.with(this).load(uri)
.noPlaceholder()
.error(R.mipmap.ic_launcher)
.into(mImg);
5. .noFade() 做网络加载时会有一个渐入过度效果,让ui视觉更好,但.noFade()是解除该效果
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.noFade()
.into(mImg);
6. 设置图片尺寸(Resize)、缩放(Scale)、裁剪(Crop)
.Resize(int w,int h) 重新设置图片尺寸
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.resize(400,300)
.into(mImg);
如果想用dp尺寸,在dimens.xml文件中写dp尺寸 调用.resizeDimen()方法设置该图片大小
<dimen name="image_width">400dp</dimen>
<dimen name="image_height">300dp</dimen>
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.resizeDimen(R.dimen.image_width,R.dimen.image_height)
.into(mImg);
当调用了resize() 方法时,调用 .onlyScaleDown() 方法是只有在图片的尺寸大于我们指定的尺寸时resize()才有作用
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.resize(4000,3000)
.onlyScaleDown()
.into(mImg);
.centerCrop() 充满ImageView的边界,集中裁剪
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.centerCrop()
.into(mImg);
.centerInside() 如果图片尺寸小于ImageView的尺寸,不会充满View边界
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.centerInside()
.into(mImg);
.fit() 测量View大小进行拉伸填充,但如果加上.centerCrop() 就会放大裁剪不会出现拉伸变形的情况,View的宽高不能wrap_content
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.fit()
.into(mImg);
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(mImg);
7. .rotate()旋转图片
.rotate(float degrees) 以(0,0)为原点进行旋转
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.rotation(180)
.into(mImg);
.rotation(fioat degrees,fioat pivotX,fioat pivotY) 以(pivotX,pivotY)为原点旋转
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.rotation(180,100,100)
.into(mImg);
8. .tag(Object tag) 为请求设置tag
程序有照片流列表时,我们快速滑动看照片时,会一直加载照片导致卡顿,这时我们为每个请求设置Tag。滑动时调用.pauseTag() 暂停请求,滑动停止时调用.resumeTag() 恢复请求来提升体验度。
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.tag("tag")
.into(mImg);
mRlv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
final Picasso picasso = Picasso.with(MainActivity.this);
if (newState == SCROLL_STATE_IDLE) {
picasso.resumeTag("tag");
} else {
picasso.pauseTag("tag");
}
}
});
弱网环境下加载很慢,退出时图片加载可能还没有完成,这时可以通过Tag来取消请求。
@Override
protected void onDestroy() {
super.onDestroy();
Picasso.with(this).cancelTag("tag");
}
9. 缓存策略
默认情况下,Picasso内存缓存和磁盘缓存都开启的状态。如果我们项目中需要调整则可以设置缓存策略
.memoryPolicy() 设置内存缓存策略
No_CACHE:表示处理请求时 跳过检查内存缓存
No_STORE:表示请求成功后,不将最终的结果存到内存
.networkPolicy() 设置磁盘缓存策略
NO_CACHE:表示处理请求时跳过处理磁盘缓存
NO_STORE:表示请求成功后,不会将结果缓存到Disk
OFFLINE:强制这次请求从缓存中获取结果,不会发起网络请求,不管缓存中是否存在结果
Picasso.with(this).load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE) // 跳过内存缓存
.networkPolicy(NetworkPolicy.NO_CACHE) // 跳过磁盘缓存
.networkPolicy(NetworkPolicy.OFFLINE) // 强制从缓存获取结果
.into(mImg);
这些基本就能让我们的图片加载简单便捷了!