官网:http://square.github.io/picasso/
介绍
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
常用用法
在ListView 或者RecycleView的适配器中的使用:
@Override
public void getView(int position, View convertView, ViewGroup parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);
}
String url = getItem(position);
//一行代码
Picasso.with(context).load(url).into(view);
}
图片的转换功能
自带剪切方法:
Picasso.with(context) //设置图片路径 .load(url) //重新定义图片尺寸 .resize(50, 50) //剪切位置设置 .centerCrop() //设置装载图片的ImageView控件 .into(imageView)
自定义图片转换:
//自定义转换类,在transform方法中实现具体转换逻辑 public static class CropSquareTransformation implements Transformation { @Override public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap result = Bitmap.createBitmap(source, x, y, size, size); if (result!=null){ source.recycle(); } return result; } @Override public String key() { return "square()"; } } ====================================================================================== //使用方法 Picasso.with(context).load(url).transform(new CropSquareTransformation()).into(imageView);
设置加载失败和正在加载的图片
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);
//通过创建File对象设置图片
Picasso.with(context).load(new File(...)).into(imageView3);
设置自定义下载器
源码中自带的下载类,支持OkHttp的前提下会使用这个类,但是如果需要自定义磁盘缓存路径,需要执行
“public OkHttpDownloader(final File cacheDir)”构造方法。
final class Utils {
//创建默认下载器
static Downloader createDefaultDownloader(Context context) {
try {
//检查是否存在OKhttp,不存在就使用 UrlConnectionDownloader
Class.forName("com.squareup.okhttp.OkHttpClient");
return OkHttpLoaderCreator.create(context);
} catch (ClassNotFoundException ignored) {
}
return new UrlConnectionDownloader(context);
}
//创建默认缓存路径
static File createDefaultCacheDir(Context context) {
File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE);
if (!cache.exists()) {
//noinspection ResultOfMethodCallIgnored
cache.mkdirs();
}
return cache;
}
}
==========================================================================
public class OkHttpDownloader implements Downloader {
...
public OkHttpDownloader(final File cacheDir) {
this(cacheDir, Utils.calculateDiskCacheSize(cacheDir));
}
public OkHttpDownloader(final Context context, final long maxSize) {
this(Utils.createDefaultCacheDir(context), maxSize);
}
public OkHttpDownloader(final File cacheDir, final long maxSize) {
this(defaultOkHttpClient());
try {
client.setCache(new com.squareup.okhttp.Cache(cacheDir, maxSize));
} catch (IOException ignored) {
}
}
用法:
Picasso.setSingletonInstance(new Picasso
.downloader(new OkHttpDownloader("自定义目录,返回File"))
.build());
Picasso获取缓存默认通过Okhttp获取,而Okhttp使用的缓存是基于DiskLruCache,DiskLruCache的实现原理比较晕,通过读写journal文件来对缓存文件进行操作的,所以在设置了自定义目录下,就可以通过DiskLruCache来进行获取和清理缓存了。
对内存缓存的操作
Picasso.with(context)
.load(url)
//缓存方案,NO_CACHE 直接从网络获取,NO_STORE 不缓存在 Cache中(内存)
.memoryPolicy(NO_CACHE, NO_STORE)
.into(imageView);
暂停、继续加载操作
在ListView 和 RecycleView滑动时为了避免卡顿,可以暂停请求,当控件停止滑动时继续加载:
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState){
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
Picasso.with(MainActivity.this).resumeTag(tag);
break;
default:
Picasso.with(MainActivity.this).pauseTag(tag);
break;
}
}
....
});
}