Glide图片加载

亦凉 2022-09-30 07:31 330阅读 0赞

转载链接,我只是一个搬运工。

http://blog.csdn.net/jiangxuqaz/article/details/50421754

图片加载框架Glide使用教程:

在项目中引入Glide

Gradle:

compile’com.github.bumptech.glide:glide:3.7.0’

glide全局配置

有时我们需要配置Glide的缓存路径,缓存大小等配置,我们可以通过实现 GlideModule接口:

  1. private static final int DISK_CACHE_SIZE = 100 * 1024 * 1024;
  2. public static final int MAX_MEMORY_CACHE_SIZE = 10 * 1024 * 1024;
  3. @Override
  4. public void applyOptions(final Context context, GlideBuilder builder) {
  5. //设置磁盘缓存的路径 path
  6. final File cacheDir = new File(path);
  7. builder.setDiskCache(new DiskCache.Factory() {
  8. @Override
  9. public DiskCache build() {
  10. return DiskLruCacheWrapper.get(cacheDir, DISK_CACHE_SIZE);
  11. }
  12. });
  13. //设置内存缓存大小,一般默认使用glide内部的默认值
  14. builder.setMemoryCache(new LruResourceCache(MAX_MEMORY_CACHE_SIZE));
  15. }
  16. @Override
  17. public void registerComponents(Context context, Glide glide) {
  18. }
  19. 1
  20. 2
  21. 3
  22. 4
  23. 5
  24. 6
  25. 7
  26. 8
  27. 9
  28. 10
  29. 11
  30. 12
  31. 13
  32. 14
  33. 15
  34. 16
  35. 17
  36. 18
  37. 19
  38. 20
  39. 21
  40. 1
  41. 2
  42. 3
  43. 4
  44. 5
  45. 6
  46. 7
  47. 8
  48. 9
  49. 10
  50. 11
  51. 12
  52. 13
  53. 14
  54. 15
  55. 16
  56. 17
  57. 18
  58. 19
  59. 20
  60. 21

然后在AndroidManifest.xml中添加配置:

  1. <meta-data
  2. android:name="你的完整包名.SimpleGlideModule"
  3. android:value="GlideModule" />
  4. 1
  5. 2
  6. 3
  7. 1
  8. 2
  9. 3

加载图片

加载网络图片

Glide也使用流式的接口。Glide 至少需要三个参数构造一个完整的图片加载请求:

  • with(Context context) - 上下文环境
  • load(String imageUrl) - 需要加载图片的URL.
  • into(ImageView targetImageView) - 图片显示的ImageView.

下面是最简单加载网络图片的用法:

  1. ImageView targetImageView = (ImageView) findViewById(R.id.imageView);
  2. String internetUrl = "http://i.imgur.com/DvpvklR.png";
  3. Glide
  4. .with(context)
  5. .load(internetUrl)
  6. .into(targetImageView);
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6

从资源文件中加载

  1. int resourceId = R.mipmap.ic_launcher;
  2. Glide
  3. .with(context)
  4. .load(resourceId)
  5. .into(imageViewResource);
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 6
  12. 1
  13. 2
  14. 3
  15. 4
  16. 5
  17. 6

从文件中加载图片

  1. File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");
  2. Glide
  3. .with(context)
  4. .load(file)
  5. .into(imageViewFile);
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 6
  12. 1
  13. 2
  14. 3
  15. 4
  16. 5
  17. 6

从URI中加载图片

  1. Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);
  2. Glide
  3. .with(context)
  4. .load(uri)
  5. .into(imageViewUri);
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 6
  12. 1
  13. 2
  14. 3
  15. 4
  16. 5
  17. 6

占位图,错误图,和淡入淡出效果

  1. Glide
  2. .with(context)
  3. .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4. .placeholder(R.mipmap.ic_launcher) //设置占位图
  5. .error(R.mipmap.future_studio_launcher) //设置错误图片
  6. .crossFade() //设置淡入淡出效果,默认300ms,可以传参
  7. //.dontAnimate() //不显示动画效果
  8. .into(imageViewFade);
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
  14. 6
  15. 7
  16. 8
  17. 1
  18. 2
  19. 3
  20. 4
  21. 5
  22. 6
  23. 7
  24. 8

设置图片大小和缩放形式

Glide 会根据ImageView的大小,自动限制图片缓存和内存中的大小,当然也可以通过调用override(horizontalSize, verticalSize)限制图片的大小:

  1. Glide
  2. .with(context)
  3. .load(UsageExampleListViewAdapter.eatFoodyImages[0]) .override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio .into(imageViewResize);
  4. 1
  5. 2
  6. 3
  7. 4
  8. 5
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5

当不知道ImageView的大小的时候,这个选项是非常有用的,我们可以设置需要加载的图片尺寸。

Glide支持两种图片缩放形式,CenterCrop 和 FitCenter
CenterCrop:等比例缩放图片,直到图片的狂高都大于等于ImageView的宽度,然后截取中间的显示。

  1. Glide
  2. .with(context)
  3. .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4. .override(600, 200) // resizes the image to these dimensions (in pixel)
  5. .centerCrop() // this cropping technique scales the image so that it fills the requested bounds and then crops the extra.
  6. .into(imageViewResizeCenterCrop);
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6

FitCenter:等比例缩放图片,宽或者是高等于ImageView的宽或者是高。

  1. Glide
  2. .with(context)
  3. .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4. .override(600, 200)
  5. .fitCenter()
  6. .into(imageViewResizeFitCenter);
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6

加载GIF和视频文件

Fresco支持加载GIF,并且使用的方式和加载图片一样:

  1. String gifUrl = "http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif";
  2. Glide
  3. .with( context )
  4. .load( gifUrl )
  5. .asGif()
  6. .error( R.drawable.full_cake )
  7. .into( imageViewGif );
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5
  13. 6
  14. 7
  15. 1
  16. 2
  17. 3
  18. 4
  19. 5
  20. 6
  21. 7

Glide可以加载视频的缩略图:

  1. String filePath = "/storage/emulated/0/Pictures/example_video.mp4";
  2. Glide
  3. .with( context )
  4. .load( Uri.fromFile( new File( filePath ) ) )
  5. .into( imageViewGifAsBitmap );
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 6
  12. 1
  13. 2
  14. 3
  15. 4
  16. 5
  17. 6

Glide缓存策略

Glide默认开启磁盘缓存和内存缓存,当然也可以对单张图片进行设置特定的缓存策略。
设置图片不加入到内存缓存

  1. Glide
  2. .with( context )
  3. .load( eatFoodyImages[0] ) .skipMemoryCache( true ) .into( imageViewInternet );
  4. 1
  5. 2
  6. 3
  7. 4
  8. 5
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5

设置图片不加入到磁盘缓存

  1. Glide
  2. .with( context )
  3. .load( eatFoodyImages[0] )
  4. .diskCacheStrategy( DiskCacheStrategy.NONE )
  5. .into( imageViewInternet );
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 1
  12. 2
  13. 3
  14. 4
  15. 5

Glide支持多种磁盘缓存策略:

  • DiskCacheStrategy.NONE :不缓存图片
  • DiskCacheStrategy.SOURCE :缓存图片源文件
  • DiskCacheStrategy.RESULT:缓存修改过的图片
  • DiskCacheStrategy.ALL:缓存所有的图片,默认

图片加载优先级

Glide支持为图片加载设置优先级,优先级高的先加载,优先级低的后加载:

  1. private void loadImageWithHighPriority() {
  2. Glide
  3. .with( context )
  4. .load( UsageExampleListViewAdapter.eatFoodyImages[0] )
  5. .priority( Priority.HIGH )
  6. .into( imageViewHero );
  7. }
  8. private void loadImagesWithLowPriority() {
  9. Glide
  10. .with( context )
  11. .load( UsageExampleListViewAdapter.eatFoodyImages[1] )
  12. .priority( Priority.LOW )
  13. .into( imageViewLowPrioLeft );
  14. Glide
  15. .with( context )
  16. .load( UsageExampleListViewAdapter.eatFoodyImages[2] )
  17. .priority( Priority.LOW )
  18. .into( imageViewLowPrioRight );
  19. }
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. 10
  30. 11
  31. 12
  32. 13
  33. 14
  34. 15
  35. 16
  36. 17
  37. 18
  38. 19
  39. 20
  40. 21
  41. 1
  42. 2
  43. 3
  44. 4
  45. 5
  46. 6
  47. 7
  48. 8
  49. 9
  50. 10
  51. 11
  52. 12
  53. 13
  54. 14
  55. 15
  56. 16
  57. 17
  58. 18
  59. 19
  60. 20
  61. 21

Glide获取Bitmap

Glide通过Target的回调获取Bitmap,最常用的是SimpleTarget:

  1. private SimpleTarget target = new SimpleTarget<Bitmap>() {
  2. @Override
  3. public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
  4. // do something with the bitmap
  5. // for demonstration purposes, let's just set it to an ImageView
  6. imageView1.setImageBitmap( bitmap );
  7. }
  8. };
  9. private void loadImageSimpleTarget() {
  10. Glide
  11. .with( context ) // could be an issue!
  12. .load( eatFoodyImages[0] )
  13. .asBitmap()
  14. .into( target );
  15. }
  16. 1
  17. 2
  18. 3
  19. 4
  20. 5
  21. 6
  22. 7
  23. 8
  24. 9
  25. 10
  26. 11
  27. 12
  28. 13
  29. 14
  30. 15
  31. 16
  32. 1
  33. 2
  34. 3
  35. 4
  36. 5
  37. 6
  38. 7
  39. 8
  40. 9
  41. 10
  42. 11
  43. 12
  44. 13
  45. 14
  46. 15
  47. 16

设置Bitmap的大小:

  1. private SimpleTarget target2 = new SimpleTarget<Bitmap>( 250, 250 ) {
  2. @Override
  3. public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
  4. imageView2.setImageBitmap( bitmap );
  5. }
  6. };
  7. private void loadImageSimpleTargetApplicationContext() {
  8. Glide
  9. .with( context.getApplicationContext() ) // safer!
  10. .load( eatFoodyImages[1] )
  11. .asBitmap()
  12. .into( target2 );
  13. }
  14. 1
  15. 2
  16. 3
  17. 4
  18. 5
  19. 6
  20. 7
  21. 8
  22. 9
  23. 10
  24. 11
  25. 12
  26. 13
  27. 14
  28. 1
  29. 2
  30. 3
  31. 4
  32. 5
  33. 6
  34. 7
  35. 8
  36. 9
  37. 10
  38. 11
  39. 12
  40. 13
  41. 14

调试和混淆

有时需要查看加载图片的一些信息,包括加载的时间,图片URL,图片的尺寸,这时,可以把调试的开关打开

  1. adb shell setprop log.tag.DecodeJob VERBOSE
  2. 1
  3. 1

把这个开关打开就能看到加载每张图片的一些信息:

这里写图片描述

如果项目开启了混淆,需要在app proguard文件中加入:

  1. #glide
  2. -keep public class * implements com.bumptech.glide.module.GlideModule -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  3. **[] $VALUES;
  4. public *;
  5. }

发表评论

表情:
评论列表 (有 0 条评论,330人围观)

还没有评论,来说两句吧...

相关阅读

    相关 图片库-Glide

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