强大的图片加载框架Picasso和Glide

一、Picasso

Picasso是Square公司出品的一个强大的图片下载和缓存图片库。官方网址是:http://square.github.io/picasso/

只需要一句代码就可以将图片下载并设置到ImageView上。

特点:

   1.在adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解决了这个问题

   2.使用复杂的图片压缩转换来尽可能的减少内存消耗

   3.自带内存和硬盘二级缓存功能

简单加载实例:

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        mImageView = (ImageView) findViewById(R.id.image);
        //加载网络图片
     //   Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(mImageView);
        //加载本地图片
     //   Picasso.with(this).load(R.drawable.mg2).into(mImageView);
        //转换图片进行裁剪
     //   Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").resize(200,200).centerCrop().into(mImageView);
        /**
         * Place holders-空白或者错误占位图片
         * error:错误占位符
         *  如果加载发生错误会重复三次请求,三次都失败才会显示error Place holder
         */
        Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(mImageView);
    }

}
Adapter中下载:使用ListView,GridView的时候,自动检测Adapter的重用(re-use),取消下载,使用缓存。

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


二、Glide

在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech。这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会上发布的官方app。

Picasso和Glide都在jcenter上。在项目中添加依赖非常简单。

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
Glide加载图片实例:

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        mImageView = (ImageView) findViewById(R.id.image);
        //加载网络图片
      //  Glide.with(this).load("http://i.imgur.com/DvpvklR.png").into(mImageView);
        //加载本地图片
      //  Glide.with(this).load(R.drawable.mg2).into(mImageView);
     //   Glide.with(this).load("http://i.imgur.com/DvpvklR.png").placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(mImageView);
        Glide.with(this).load("http://i.imgur.com/DvpvklR.png").placeholder(R.mipmap.ic_launcher).crossFade().into(mImageView);
    }

Glide和Picasso非常相似,Glide加载图片的方法和Picasso如出一辙。虽然两者看起来一样,但是Glide更易用,因为Glide的with方法不光接受Context,还接受Activity 和 Fragment,Context会自动的从他们获取。同时将Activity/Fragment作为with()参数的好处是:图片加载会和Activity/Fragment的生命周期保持一致,比如Paused状态在暂停加载,在Resumed的时候又自动重新加载。所以我建议传参的时候传递Activity 和 Fragment给Glide,而不是Context。另外,Glide加载的图片质量要差于Picasso,这是因为Glide默认的Bitmap格式是RGB_565 ,比ARGB_8888格式的内存开销要小一半。


更新说明:

不久前Glide发布了新版本4.0,本来也没注意,结果使用的时候发现:

Glide.with(this).load(R.mipmap.image).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher)nima

尼玛,什么鬼!预加载图片和加载失败显示图片这两个方法竟然不见了,顿时感觉整个人就不好了,这还怎么玩,官方还声称4.0是目前最为顶的一个版本。于是及时去查看了源码,发现确实改变很大,不过里面多了一个这样的方法:

/**
 * Applies the given options to the request, options set or unset in the given options will
 * replace those previously set in options in this class.
 *
 * @see RequestOptions#apply(RequestOptions)
 * @return This request builder.
 */
public RequestBuilder<TranscodeType> apply(@NonNull RequestOptions requestOptions) {
  Preconditions.checkNotNull(requestOptions);
  this.requestOptions = getMutableOptions().apply(requestOptions);
  return this;
}

英文部分:将给定的选项应用到请求中,在给定选项中设置或取消选项将取代之前在这个类中设置的选项。(纯属百度,个人英语水平有限,见谅啊!)

于是抱着试试看的心态,查看了一下RequestOptions这个类,在这个类中,“不经意”间发现了
public RequestOptions placeholder(int resourceId)

public RequestOptions priority
public RequestOptions diskCacheStrategy
public RequestOptions error
看到这应该就明白了,原来的一些方法,还是可以用的,只不过多了一些设置而已,于是便尝试了一下:

RequestOptions options = new RequestOptions()
        .placeholder(R.mipmap.ic_launcher)//预加载占位符
         .error(R.mipmap.ic_launcher)//加载失败占位符
         .centerCrop()
        .priority(Priority.HIGH)//优先级
        .diskCacheStrategy(DiskCacheStrategy.ALL);//缓存
Glide.with(this).load(R.mipmap.image).apply(options).into(mImageView);
效果自然是和原来一样,正常显示了。真是大吃一惊,4.0的改变,着实有点大,个人感觉还是以前的更好用的一些,非常方便(也有可能是以前的习惯了 ),不知道这样改是不是有什么其他深意,暂时还是比较痛苦的,没办法!

在此更新一下,希望看到的道友,不要被先前写的内容误导!(不过,如果你的Glide是4.0以下的话,用以前的方法完全是没问题的!)

Glide4.1.1:

加入依赖的话,出报错:Error:Failed to resolve: com.android.support:support-annotations:26.0.2(根据个人所用SDK版本不同,后面版本一会不同g)

解决办法:

在Project的build中:

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}
运行一下,搞定!

下面是用Picasso和Glide加载同一张图片的效果差别:

Picasso:      Glide:

本地图片:

    

Picasso裁剪图片:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值