首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

android 图片加载跟缓存开源项目 Picasso

2013-12-15 
android 图片加载和缓存开源项目 Picassopublic class CropSquareTransformation implements Transformati

android 图片加载和缓存开源项目 Picasso

  1. public class CropSquareTransformation implements Transformation {
  2. @Override public Bitmap transform(Bitmap source) {
  3. int size = Math.min(source.getWidth(), source.getHeight());
  4. int x = (source.getWidth() - size) / 2;
  5. int y = (source.getHeight() - size) / 2;
  6. Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
  7. if (result != source) {
  8. source.recycle();
  9. }
  10. return result;
  11. }@Override public String key() { return "square()"; }
  12. }
复制代码

?


用该类示例调用函数 RequestBuilder.transform(Transformation) 即可。

占位符图片

Picasso支持下载和加载错误占位符图片。

帮助
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
如果重试3次(下载源代码可以根据需要修改)还是无法成功加载图片 则用错误占位符图片显示。

支持本地资源加载

从 Resources, assets, files, content providers 加载图片都支持

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load(new File("/images/oprah_bees.gif")).into(imageView2);
调试支持

调用函数 Picasso.setDebug(true) 可以在加载的图片左上角显示一个 三角形 ,不同的颜色代表加载的来源

红色:代表从网络下载的图片

黄色:代表从磁盘缓存加载的图片

绿色:代表从内存中加载的图片

如果项目中使用了OkHttp库的话,默认会使用OkHttp来下载图片。否则使用HttpUrlConnection来下载图片。

http://square.github.io/picasso/

其他功能查看项目主页:http://github.com/square/picasso

参考项目:https://github.com/nostra13/Android-Universal-Image-Loader

https://github.com/mitmel/Android-Image-Cache

https://github.com/novoda/ImageLoader

https://github.com/square/okhttp

热点排行