Picasso解析图
目录
1. 简介
- 介绍:Picasso,是Android中一个图片加载开源库
- 主要作用:实现图片加载
- Picasso不仅实现了图片异步加载的功能,还解决了Android中加载图片时需要解决的一些常见问题
- 接下来,我会对Picasso的每个功能点进行详细的介绍
0. 添加依赖
要使用Picasso,首先我们要添加版本依赖,去官网或者Github 看一下当前的最新版本(截止本文最新版本为2.5.2),然后在build.gradle中添加依赖:
compile 'com.squareup.picasso:picasso:2.5.2'
1. 加载显示图片
将Picasso添加到项目之后,我们就可以用它来加载图片了,使用方法非常简单:
Picasso.with(this)
.load("http://ww3.sinaimg.cn/large/610dc034jw1fasakfvqe1j20u00mhgn2.jpg")
.into(mImageView);
只需要一行代码就完成了加载图片到显示的整个过程,链式调用,非常简洁,其实有三步,一次调用了三个方法:
- with(Context) 获取一个Picasso单例,参数是一个Context上下文
- load(String) 调用load 方法加载图片
- into (ImageView) 将图片显示在对应的View上,可以是ImageView,也可以是实现了Target j接口的自定义View。
上面演示了加载一张网络图片,它还支持其它形式的图片加载,加载文件图片,加载本地资源图片,加载一个Uri 路径给的图片,提供了几个重载的方法:
1, load(Uri uri) 加载一个以Uri路径给的图片
Uri uri = Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId)
Picasso.with(this).load(uri).into(mImageView);
** 2,load(File file) 加载File中的图片**
Picasso.with(this).load(file).into(mImageView);
3, load(int resourceId) 加载本地资源图片
Picasso.with(this).load(R.mipmap.ic_launcher).into(mImageView);
提醒:上面介绍了load的几个重载方法,加载不同资源的图片,另外提醒注意一下load(String path)接受String 参数的这个方法,参数String 可以是一个网络图片url,也可以是file 路径、content资源 和Android Resource。看一下源码的注释。
/**
* Start an image request using the specified path. This is a convenience method for calling
* {@link #load(Uri)}.
* <p>
* This path may be a remote URL, file resource (prefixed with {@code file:}), content resource
* (prefixed with {@code content:}), or android resource (prefixed with {@code
* android.resource:}.
* <p>
* Passing {@code null} as a {@code path} will not trigger any request but will set a
* placeholder, if one is specified.
*
* @see #load(Uri)
* @see #load(File)
* @see #load(int)
* @throws IllegalArgumentException if {@code path} is empty or blank string.
*/
public RequestCreator load(String path) {
if (path == null) {
return new RequestCreator(this, null, 0);
}
if (path.trim().length() == 0) {
throw new IllegalArgumentException("Path must not be empty.");
}
return load(Uri.parse(path));
}
要使用string 参数加载上面的几种资源,除了网络url,其它几种需要加上对应前缀,file文件路径前缀:file: , content 添加前缀:content: ,Android Resource 添加:android.resource:
2. placeholder& error & noPlaceholder & noFade
通过上面的第一步我们就可以通过Picasso 加载图片了,我们的项目中通常最常用的就是加载网络图片,但是由于网络环境的差异,有时侯加载网络图片的过程有点慢,这样界面上就会显示空ImageView什么也看不见,用户体验非常不好。其实以前用过ImageLoader的同学都知道,ImageLoader 是可以设置加载中显示默认图片的,Picasso当然也给我们提供了这个功能,这就是我们要说的placeholder(占位图)。
1,placeholder
placeholder提供一张在网络请求还没有完成时显示的图片,它必须是本地图片,代码如下:
Picasso.with(this).load(URL)
.placeholder(R.drawable.default_bg)
.into(mImageView);
设置placeholder之后,在加载图片的时候,就可以显示设置的默认图了,提升用户体验。
2, error
和placeholder 的用法一样,error 提供一张在加载图片出错的情况下显示的默认图
Picasso.with(this).load(URL)
.placeholder(R.drawable.default_bg)
.error(R.drawable.error_iamge)
.into(mImageView);
3,noPlaceholder
这个方法的意思就是:在调用into的时候明确告诉你没有占位图设置。根据这个方法签名的解释是阻止View被回收的时候Picasso清空target或者设置一个应用的占位图。需要注意的是placeholder和noPlaceholder 不能同时应用在同一个请求上,会抛异常。
Picasso.with(this).load(URL)
.noPlaceholder()
.error(R.drawable.error_iamge)
.into(mImageView);
4,noFade
无论你是否设置了占位图,Picasso 从磁盘或者网络加载图片时,into 显示到ImageView 都会有一个简单的渐入过度效果,让你的UI视觉效果更柔顺丝滑一点,如果你不要这个渐入的效果(没有这么坑爹的需求吧!!!),就调用noFade方法。
Picasso.with(this).load(URL)
.placeholder(R.drawable.default_bg)
.error(R.drawable.error_iamge)
.noFade()
.into(mImageView);
3. 设置图片尺寸(Resize)、缩放(Scale)和裁剪(Crop)
1, Resize(int w,int h)
在项目中,为了带宽、内存使用和下载速度等考虑,服务端给我们的图片的size 应该和我们View 实际的size一样的,但是实际情况并非如此,服务端可能给我们一些奇怪的尺寸的图片,我们可以使用resize(int w,int hei) 来重新设置尺寸。
Picasso.with(this).load(URL)
.placeholder(R.drawable.default_bg)
.error(R.drawable.error_iamge)
.resize(400,200)
.into(mImageView);
resize()方法接受的参数的单位是pixels,还有一个可以设置dp单位