首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图片加载利器——Picasso

图片加载利器——Picasso

作者头像
Demo_Yang
发布2018-10-15 16:21:17
5610
发布2018-10-15 16:21:17
举报
文章被收录于专栏:yang0rangeyang0range

piacsso是Square公司开源的一个Android的图形缓存库 官网地址:http://square.github.io/picasso/ Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code! Picasso.with(context).load(“http://i.imgur.com/DvpvklR.png“).into(imageView); Many common pitfalls of image loading on Android are handled automatically by Picasso: Handling ImageView recycling and download cancelation in an adapter. Complex image transformations with minimal memory use. Automatic memory and disk caching. 简单来说 其强大的部分在于,可以实现图片下载和缓存功能,并且完全通过一行代码就能实现图片的异步加载:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Picasso不仅实现了图片异步加载的功能,还解决了android中加载图片时需要解决的一些常见问题:

1.在adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解决了这个问题。
2.使用复杂的图片压缩转换来尽可能的减少内存消耗
3.自带内存和硬盘二级缓存功能

特性以及示例代码:

ADAPTER 中的下载:Adapter的重用会被自动检测到,Picasso会取消上次的加载

@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()"; }
}

将CropSquareTransformation 的对象传递给transform 方法即可。 Place holders-空白或者错误占位图片:picasso提供了两种占位图片,未加载完成或者加载发生错误的时需要一张图片作为提示。

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
.into(imageView);

如果加载发生错误会重复三次请求,三次都失败才会显示erro Place holder 资源文件的加载:除了加载网络图片picasso还支持加载Resources, assets, files, content providers中的资源文件。

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load(new File(...)).into(imageView2);
下面我们详细介绍几个常用方法:
Picasso.with(上下文).load(这里可以本地,网络等等) 
error图片加载失败显示图片 
placeholder图片未完成加载的时候显示图片 
fit调整大小以达到精确的大小 
resize(int targetWidth, int targetHeight) 将图像大小调整为像素的大小 
resizeDimen(int targetWidthResId, int targetHeightResId) 将图像大小调整到指定大小 
rotate(float degrees) 按指定度旋转图像 
rotate(float degrees, float pivotX, float pivotY) 围绕着一个点旋转制定的度数 
centerCrop() centerInside()这俩太常用了 不说了

onlyScaleDown 只要图像大小大于目标大小,就调整图像的大小(需要和resize关联)

config(Bitmap.Config config)尝试使用指定的配置来解码图像

priority(Priority priority) 设置此请求的优先级
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.03.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.在adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解决了这个问题。
  • 2.使用复杂的图片压缩转换来尽可能的减少内存消耗
  • 3.自带内存和硬盘二级缓存功能
  • 下面我们详细介绍几个常用方法:
相关产品与服务
图片处理
图片处理(Image Processing,IP)是由腾讯云数据万象提供的丰富的图片处理服务,广泛应用于腾讯内部各产品。支持对腾讯云对象存储 COS 或第三方源的图片进行处理,提供基础处理能力(图片裁剪、转格式、缩放、打水印等)、图片瘦身能力(Guetzli 压缩、AVIF 转码压缩)、盲水印版权保护能力,同时支持先进的图像 AI 功能(图像增强、图像标签、图像评分、图像修复、商品抠图等),满足多种业务场景下的图片处理需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档