Picasso——针对Android的一个强大的图像下载和缓存库

http://www.eoeandroid.com/thread-556334-1-1.html

简介
图片为 安卓应用添加了必备内容和视觉风格。Picasso允许应用程序加载图片——往往只需一行代码!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso会自动处理安卓加载图片时出现的许多常见缺陷:
1.在适配器中处理ImageView循环和下载取消。
2.保证最小内存使用率情况下的复杂图片转换。
3.自动内存和磁盘高速缓存。


特性
适配器下载
可以自动检测适配器复用
@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()
  .into(imageView)

可以指定自定义变化以便达到更好效果。
public 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 != source) {
 
      source.recycle();
 
    }
 
    return result;
 
  }
 

  @Override public String key() { return "square()"; }
 
}
把该类的实例传递给变换方法。


占位符
Picasso把下载和错误占位符作为可选功能。
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(new File(...)).into(imageView2);
 

DEBUG指标
开发时可以启用彩带来指示图像源。在Picasso实例中调用setIndicatorsEnabled(true)。

Download
picasso-2.4.0.jar(112.05 KB, 下载次数: 14)


MAVEN
 
<dependency>
 
  <groupId>com.squareup.picasso</groupId>
 
  <artifactId>picasso</artifactId>
 
  <version>2.4.0</version>
 
</dependency>

翻译作者:eoe - @wanning
转载请注明:文章转载自eoeAndroid社区(http://www.eoeandroid.com

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值