picasso的简单用法

来源:互联网 发布:社交网络 肖恩帕克 编辑:程序博客网 时间:2024/06/08 19:28
Picasso框架
什么是Picasso:

强大的图片下载和缓存的第三方库;我觉得这就是对它最准确的描述了,

至于其他特性,可以参见官网介绍:Picasso
如何使用Picasso

android update project -p

ant jar

2.Picasso的基本用法:
将Picasso添加进项目后,要使用它非常简单,只需要一行代码就能搞定


Picasso.with(context).load(imageUrl).into(imageView);

短短的一行代码为我们解决了很多问题:

    自动将图像缓存在本地
    通过图片压缩转换以减少内存消耗
    自动处理了ImageView的回收,即自动取消不在视野范围内的

ImageView视图资源的加载;

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);
    }

4.图像格式转换:很多时候需要将图片进行格式转换或者剪裁以节省内存

或者达到我们的布局效果:

剪裁大小:

    Picasso.with(context).load(imageUrl).resize

(50,50).centerCrop().into(imageView);

自定义格式转换:为了实现更多你想要图片转换的效果,你可以自己实现

一个实现了Transformation接口的类,然后将其对象传递给transform()

方法:

1 0
原创粉丝点击