Android Glide Google 推荐加载图片框架(加载图片详解篇)

每个时间,都会有不同的心情,学会享受现在的心情,无论快乐亦或悲伤的心情,都是生活的点滴印记。


   看到这个,你一定会说,我现在在用ImageLoader,Picasso,Fresco或其它框架中的图片加载,都挺好用的,为什么要选用Glide呢?
   答:因为是Google推荐的。(不要打我,我说的是实话)

看这篇译文图片加载框架介绍
和Picasso有90%相似度,但却比Picasso省内存。如果你注重app的优化,注重内存的管理,可以转换使用Glide来加载图片。

转载请注明出处:http://blog.csdn.net/qq_33078541?viewmode=list
下面来总结一下Glide加载图片过程中的方法。(都很有用哦)

1.从本地资源中加载:

int resourceId = R.mipmap.ic_launcher;
Glide
    .with(context)
    .load(resourceId)
    .into(imageViewResource);

2.从文件中加载:

//这个文件可能不存在于你的设备中。然而你可以用任何文件路径,去指定一个图片路径。

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");
Glide
    .with(context)
    .load(file)
    .into(imageViewFile);

3.从Uri中加载:

//这可能是任何 Uri。为了演示的目的我们只是用一个 launcher icon 去创建了一个 Uri

Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);
Glide
    .with(context)
    .load(uri)
    .into(imageViewUri);

4.listView GridView 加载不会出现图片错位的情况。并自带三级缓存。

转载请注明出处:http://blog.csdn.net/qq_33078541?viewmode=list

5.占位图的展示:placeholder()

Glide
    .with(context)
    .load(R.mipmap.ic_launcher)
    .placeholder(R.mipmap.ic_launcher) // 设置在加载图片过程图显示的占位图
    .into(imageViewPlaceholder);

6.错误占位符: .error()

Glide
    .with(context)
    .load("http://futurestud.io/non_existing_image.png")
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // 图片加载错误时候的加载
    .into(imageViewError);

7.加载图片时候淡入淡出动画 :crossFade();(这个效果在3.6.1版是默认激活的)

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
    .crossFade()//增加图片显示时候的淡入淡出动画
    .into(imageViewFade);

8.直接加载不要淡入淡出效果:dontAnimate();

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
    .dontAnimate()//取消淡入淡出效果
    .into(imageViewFade);

9.resize(x,y) 调整图片大小 override(horizontalSize,verticalSize)

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
    .into(imageViewResize);
(设置图片大小,可能导致图片失真变形)

转载请注明出处:http://blog.csdn.net/qq_33078541?viewmode=list

10.CenterCrop 和FitCenter,看到这俩属性,你一定想到了ImageView 也有同样的属性,就是为了按照需要缩放图片。

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200) // resizes the image to these dimensions (in pixel)
    .centerCrop() //从中间开始扩散显示
    .into(imageViewResizeCenterCrop);
Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200)
    .fitCenter() //裁剪技术,缩放图像让图像都测量出来等于或小于 ImageView 的边界范围,该图像将会完全显示,但可能不会填满整个 ImageView。
    .into(imageViewResizeFitCenter);

11.显示Gif 图片 强制Glide 变成一个Gif加载 asGif()方法

Glide  
    .with( context )
    .load( gifUrl )
    .asGif()//强制显示Gif
    .error( R.drawable.full_cake )
    .into( imageViewGif );

12.显示本地视频

String filePath = "/storage/emulated/0/Pictures/example_video.mp4";
Glide  
    .with( context )
    .load( Uri.fromFile( new File( filePath ) ) )
    .into( imageViewGifAsBitmap );

13.图片加载缓存(内存缓存) .skipMemoryCache(true).

该方法将告诉glide跳过内存缓存,就是不将图片缓存到内存缓存中。

Glide  
    .with( context )
    .load( eatFoodyImages[0] )
    .skipMemoryCache( true )
    .into( imageViewInternet );

转载请注明出处:http://blog.csdn.net/qq_33078541?viewmode=list

14.图片加载缓存(磁盘缓存)

.diskCacheStrategy(),参数需要一个枚举类型的,而不是一个布尔类型。
.diskCacheStrategy(DiskCacheStrategy.NONE);

Glide  
    .with( context )
    .load( eatFoodyImages[0] )
    .diskCacheStrategy( DiskCacheStrategy.NONE )
    .into( imageViewInternet );

15.自定义磁盘缓存(磁盘缓存)

Glide加载图片和Picasso加载图片有百分之90%的相似度,但是Glide占用内存不足Picasso的一半(据说图片会稍微不清晰,但是肉眼看不出来(我是没看出来))
在缓存方面,Picasso缓存的是整张高清图片,而Glide缓存原始图的同时,缓存加载出来的图片(比如你的图片是1000*1000,加载到ImageView 中的图片是500*500),这个时候,Glide会将1000*1000和500*500两个参数都进行缓存。

这个时候的就要看.diskCacheStrategy() 方法中几个枚举参数了(看12)
DiskCacheStrategy.NONE 什么都不缓存,就像刚讨论的那样
DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像。在我们上面的例子中,将会只有一个 1000x1000 像素的图片
DiskCacheStrategy.RESULT 仅仅缓存最终的图像,即,降低分辨率后的(或者是转换后的)
DiskCacheStrategy.ALL 缓存所有版本的图像(默认行为)
一目了然,磁盘缓存就搞定了。可以缓存原图,可以缓存处理过的图。吊炸天
(备注:3.6.1以及最新的3.7.0 .diskCacheStrategy()默认都是DiskCacheStrategy.RESULT)

16.自定义图片加载优先级 .priority()方法。

介绍:当你要加载图片的时候,希望某些图片先加载,某些图片之后加载,这个时候调用.priority()方法,其参数为枚举类型。
所包含的参数递增的方式(优先级越来越高)排列:

Priority.LOW
Priority.NORMAL
Priority.HIGH
Priority.IMMEDIATE

设置了这个参数,系统会尽可能的按照你的意愿去加载图片(就是可能因为某些规则,不按你设置的规则走)

17.设置缩略图 .thumbnail() 传入float参数作为其大小的倍数。

Glide  
    .with( context )
    .load( UsageExampleGifAndVideos.gifUrl )
    .thumbnail( 0.1f ) //加载缩略图  为原图的十分之一
    .into( imageView2 );

备注:使用缩略图的话,就要考虑ImageView 确保ScaleType 的属性问题。要不然加载出来的图片会很小。

上面的可能是要加载一个网络图片,即使是缩略图,也要先从网络上拿到图片,才能变成缩略图进行展示。所以我们也可以加载本地的一张图片的缩略图。

private void loadImageThumbnailRequest() {  
    // setup Glide request without the into() method
    DrawableRequestBuilder<String> thumbnailRequest = Glide
        .with( context )
        .load( R.drawable.pic);
    // pass the request as a a parameter to the thumbnail request
    Glide
        .with( context )
        .load( UsageExampleGifAndVideos.gifUrl )
        .thumbnail( thumbnailRequest )
        .into( imageView3 );
}

加载本地的一张图片作为缩略图进行加载。


Glide的17个方法,教你快速学会使用Glide加载图片资源(其实很简单,每个基本都是调一个方法。)
转载请注明出处:http://blog.csdn.net/qq_33078541?viewmode=list

随后将收集Glide框架 除加载图片其他功能实现,如果你喜欢我的博客,关注一下,共同学习咯~么么哒

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值