现在网络上的图片加载库已经多如牛毛了,所以选择一个适合自己的图片加载库是非常有助于项目开发的。
现在几个被用的比较多的库有:UIL,Picasso, Glide, ImageLoader, Volley 和 Fresco。
如下简介:
Universal Image Loader:一个强大的图片加载库,包含各种各样的配置,最老牌,使用也最广泛。只可惜作者已经停止该项目的维护了,所以不太推荐使用。
Picasso: Square出品,和OkHttp搭配使用效果简直杠杠滴。主要特点就是使用简单,扩展性强,支持各种来源的图片,包括网络、Resources、assets、files、content providers等。内部集成了OkHttp的网络框架,所以如果你的项目中使用了Square公司的其他框架,那么推荐使用Picasso,兼容性会好一些。
Volley :Google官方出品,详细移步这里
Fresco:Facebook出的,就我看来,除非你的项目有非常多图片展示的模块,慎用,但用好了,还是非常不错的。
Glide:Google推荐的图片加载库,专注于流畅的滚动。代码风格与Picasso非常相似,增加了更多的功能,非常重要的就是支持gif,当然它的包会大一些。如果你的项目对图片的使用场景非常多,并且需要支持gif,则推荐使用。
今天我们就说 Picasso ImageLoader Glide 三个库。
一 、 picasso
特点
1 提供内存和磁盘缓存,默认开启,可以设置不进行缓存
2 图片加载过程中默认显示的图片
3 图片加载失败或出错后显示的图片
4 图片加载成功或失败的回调
5 自定义图片大小、自动测量ImageView大小、裁剪、旋转图片等
6 对图片进行转换
7 标签管理,暂停和恢复图片加载
8 请求优先级管理
9 可以从不同来源加载图片,网络、Resources、assets、files、content providers
10 更加丰富的扩展功能
添加:
compile 'com.squareup.picasso:picasso:2.5.2'
picasso基本的使用及API相当简单,说只需要一行代码就能满足你也不为过
Picasso.with(this).load(imageUrl).into(imageView);
常用API也不多 如下:
Picasso.with(this)
.load(url)
.centerCrop()
.error()
.resize()
.into(iv);
一般项目中的图片异步加载需求,picasso完全可以满足需求。
二、Imageloader
算了,相对于其他几个库来说,imageloader已经可以淘汰了,我就不再多说,想了解的同学 移步这里
三、 Glide
Glide 使用与picasso大同小异,使用这些库基本上是一法通万法通,api也很好理解。但欲钻研其原理底层之类的还是需要仔细研究。
转自这里 glide更多点击这里
网络加载图片
Glide.with(context).load(internetUrl).into(targetImageView);
Glide.with(context).load(internetUrl).into(targetImageView);
从文件加载
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Test.jpg");
Glide.with(context).load(file).into(imageViewFile);
从资源id
int resourceId = R.mipmap.ic_launcher;
Glide.with(context).load(resourceId).into(imageViewResource);
从uri
Glide.with(context).load(uri).into(imageViewUri);
Glide.with(context).load(uri).into(imageViewUri);
加载gif
String gifUrl = "xxxxx";
Glide.with( context ).load( gifUrl ).into( imageViewGif );
一些常规方法(待加载时显示,加载出错时显示)
Glide.with( context ).load( gifUrl ).placeholder( R.drawable.cupcake ).error( R.drawable.full_cake ).into( imageViewGif );
Glide.with( context ).load( gifUrl ).placeholder( R.drawable.cupcake ).error( R.drawable.full_cake ).into( imageViewGif );
强制转化为gif
Glide.with( context ).load( gifUrl ).asGif().error( R.drawable.full_cake ).into( imageViewGif );
Glide.with( context ).load( gifUrl ).asGif().error( R.drawable.full_cake ).into( imageViewGif );
用bitMap播放Gif
Glide.with( context ).load( gifUrl ).asBitmap().into( imageViewGifAsBitmap );
Glide.with( context ).load( gifUrl ).asBitmap().into( imageViewGifAsBitmap );
播放本地mp4,只能是本地
String filePath = "/storage/emulated/0/Pictures/example_video.mp4";
Glide.with( context ).load( Uri.fromFile( **new **File( filePath ) ) ).into( imageViewGifAsBitmap );
实现圆形图片
Glide.with(this).load(ur2).asBitmap().centerCrop().into(new BitmapImageViewTarget(iv){
@Override
protected void setResource(Bitmap resource) {
super.setResource(resource);
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(getApplicationContext().getResources(), resource);
circularBitmapDrawable.setCircular(true);
iv.setImageDrawable(circularBitmapDrawable);
}
});