zoukankan      html  css  js  c++  java
  • 【转】Picasso – Android系统的图片下载和缓存类库

    来源:http://blog.chengyunfeng.com/?p=492

    另一篇参考:http://blog.csdn.net/xu_fu/article/details/17043231

    PicassoSquare开源的一个用于Android系统下载和缓存图片的项目。该项目和其他一些下载图片项目的主要区别之一是:使用4.0+系统上的HTTP缓存来代替磁盘缓存。

    Picasso 的使用是非常简单的,例如:

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

    Picasso有如下特性:

    • 处理Adapter中的 ImageView 回收和取消已经回收ImageView的下载进程
    • 使用最少的内存完成复杂的图片转换,比如把下载的图片转换为圆角等
    • 自动添加磁盘和内存缓存
    具体介绍

    在Adapter中下载
    自动检测Adapter中的ImageView重用和取消不必要的下载

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

    图片转换

    转换图片以适合所显示的ImageView,来减少内存消耗

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

    用该类示例调用函数 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.with(context).load(R.drawable.landing_screen).into(imageView1);

    的方式不能支持9.png,还是要使用老办法:

    iv.setImageResource(R.drawable.speech_bubble);

    调试支持

    调用函数 Picasso.setDebug(true) (注意:应该是 Picasso.with(MyMsg.this).setDebugging(true);)可以在加载的图片左上角显示一个 三角形 ,不同的颜色代表加载的来源

    红色:代表从网络下载的图片
    
    黄色:代表从磁盘缓存加载的图片
    
    绿色:代表从内存中加载的图片

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

    其他功能查看项目主页: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

     
  • 相关阅读:
    “main cannot be resolved or is not a field”解决方案
    .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>
    MVC学习笔记---ModelBinder
    MVC学习笔记---MVC框架执行顺序
    服务器知识----IIS架设问题
    C/C++学习笔记---primer基础知识
    C/C++学习笔记----指针的理解
    C#学习笔记-----C#枚举中的位运算权限分配
    CSS学习笔记----CSS3自定义字体图标
    Clr Via C#读书笔记----基元线程同步构造
  • 原文地址:https://www.cnblogs.com/sudawei/p/3553928.html
Copyright © 2011-2022 走看看