Glide简单使用(二)

来源:互联网 发布:自学java如何找工作 编辑:程序博客网 时间:2024/06/04 19:54

1. Glide缓存
分为两种,

  • 内存缓存
    • skipMemoryCache(true)
  • 磁盘缓存
    • DiskCacheStrategy.NONE 什么都不缓存,
    • DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像
    • DiskCacheStrategy.RESULT 仅仅缓存最终的图像,即,降低分辨率后的(或者是转换后的)
    • DiskCacheStrategy.ALL 缓存所有版本的图像(默认行为)

内存缓存
Glide默认将图片资源缓存到内存,当我们不想使用内存缓存时。跳过内存缓存skipMemoryCache(true)见名知意。同时,Glide将会仍然使用磁盘缓存,来避免重复请求网络数据。

Glide.with(this)                .load("http://2f.zol-img.com.cn/product/104_1200x900/305/cevDJaCdeLQ6.gif")                .override(800, 300)                .skipMemoryCache(true) //跳过内存缓存                .placeholder(R.mipmap.ic_launcher)                .error(R.mipmap.ic_launcher_round)                .into(iv_2);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

磁盘缓存
就不多说,直接上代码。
我们需要根据上述所给的几种类型,来选择你需要的方式。

Glide.with(this)                .load("http://2f.zol-img.com.cn/product/104_1200x900/305/cevDJaCdeLQ6.gif")                .override(800, 300)                .diskCacheStrategy(DiskCacheStrategy.NONE)                .placeholder(R.mipmap.ic_launcher)                .error(R.mipmap.ic_launcher_round)                .into(iv_2);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

两种缓存都不使用
同时调用两个方法

Glide.with(this)                .load("http://2f.zol-img.com.cn/product/104_1200x900/305/cevDJaCdeLQ6.gif")                .override(800, 300)                .skipMemoryCache(true) //跳过内存缓存                .diskCacheStrategy(DiskCacheStrategy.NONE)                .placeholder(R.mipmap.ic_launcher)                .error(R.mipmap.ic_launcher_round)                .into(iv_2);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.图片请求的优先级
在开发过程中,比如A图片是一张非常大的图片,B,C图片相对要小。这个时候我们需要让A图片先加载显示。我们就需要使用该方法。
这个枚举给了四个不同的选项,下面是按照递增priority(优先级)的列表:

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

    具体代码

这个时候会先加载 带有Priority.HIGH属性的图片资源,然后才去加载Priority.LOW属性的图片资源。
还有一个情况,是我在测试的时候遇到:
发现,如果优先级较低的图片有设置缓存数据,当我们第二次再次进入这个界面时,该图片会优先于未设置缓存数据的图片,先加载出来。可以自行测试

Glide.with(this)                .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg")                .placeholder(R.drawable.ic_launcher)                .error(R.mipmap.ic_launcher_round)                .priority(Priority.LOW)                .into(iv_1);        tv_title2.setText("加载gif资源");        Glide.with(this)                .load("http://2f.zol-img.com.cn/product/104_1200x900/305/cevDJaCdeLQ6.gif")                .override(800, 300)                .priority(Priority.HIGH)                .placeholder(R.mipmap.ic_launcher)                .error(R.mipmap.ic_launcher_round)                .into(iv_2);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3.自定义缩略图
概述:缩略图不同于之前博客提到的占位符。占位符必须附带应用程序捆绑的资源才行。缩略图是动态占位符。它也可以从网络中加载。缩略图将会在实际请求加载完或者处理完之后才显示。如果缩略图对于任何原因,在原始图像到达之后,它不会取代原始图像。它只会被抹除。

Glide.with(this)                .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg")                .thumbnail(0.1f)                .into(iv_8);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

thumbnail(0.1f)这个属性,实现的效果是原图1000*1000,缩小十倍100*100缩略图显示出来。

下面这种自定义缩略图
它的优点多说了,看上面的概述。

DrawableRequestBuilder<Integer> thumbail = Glide.with(this)                .load(R.drawable.ic_pb_default);        Glide.with(this)                .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg")                .thumbnail(thumbail)                .into(iv_8);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

所不同的是,第一个缩略图请求是完全独立于第二个原始请求的。该缩略图可以是不同的资源或图片 URL,你可以为其应用不同的转换,等等。


3.Glide 中的回调:Targets
概述:前面所讲的在into()里设置ImageVeiw,试想一下,如果这将不是最后一步,可以在into()里设置Targets的方式去接受图片资源的Bitmap。Targets 是没有任何别的回调,它在 Glide 做完所有的加载和处理之后返回结果。

private SimpleTarget simpleTarget = new SimpleTarget<Bitmap>(200,200) {            @Override            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {                iv_9.setImageBitmap(resource);            }        };        tv_title9.setText("Glide 中的回调:Targets");        Glide.with(this)                .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg")                .asBitmap()                .into(simpleTarget);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 从代码中我们可以看到,使用了asBitmap()方法,主要是因为后台返回的资源地址,有GIF等多种类型,而SimpleTarget参数类型是Bitmap类型。所以要把Glide加载的图片资源强制转换成asBitmap()资源。
  • 根据JAVA/android语法规定,允许init()方法里去使用匿名内部类。Glide在请求加载图片之前,在手机内存较低的情况下,会导致Android垃圾回收机制移除了匿名内部类。当图片加载成时,就不会再去回调该方法。所以我们要保证把simpleTarget声明成一个字段对象,这样就可以防止被Android垃圾回收机制移除。
  • 第二个关键部分是 Glide 建造者中这行:with(context)。 这里的问题实际是 Glide 的功能:当你传了一个 context,例如是当前应用的 activity,Glide将会自动停止请求当请求的 activity 已经停止的时候。这整合到了应用的生命周期中通常是非常有帮助的,但是有时工作起来是困难的,如果你的 target 是独立于应用的 activity 生命周期。这里的解决方案是用 application 的 context: .with(context.getApplicationContext))。当应用资深完全停止时,Glide 才会杀死这个图片请求。请求记住,再说一次,如果你的请求需要在 activity 生命周期之外去做时,才用下面这样的代码:
Glide.with(getApplicationContext))                .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg")                .asBitmap()                .into(simpleTarget);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

当然可能有人再问,如果我想设置ImageView大小。这我Glide已经有解决方法,Target是可以指定尺寸。使用方法,看上面的代码。具体看源码。

这里写图片描述

ViewTarget自定义视图
这个主要是用在自定义View,比如我们程序中有一个组合自定义控件,里面有ImageView需要我们设置。这个时候用ViewTarget更容易实现。

FutureStudioView studioView = (FutureStudioView) findViewById(R.id.custom_view);        ViewTarget viewTarget = new ViewTarget<FutureStudioView, GlideDrawable>(studioView) {            @Override            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {                this.view.setImage(resource.getCurrent());               this.view.setText(String.valueOf(resource.getCurrent()));            }        };        Glide.with(this)                .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg")                .into(viewTarget);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

认真研究,你会发现,我们还可以在ViewTarget里面去给TextView设置值。具体自己去测试吧。

加载图片到Notifications
见名知意,就是通知栏图标嘛。Glide还提供了方便舒适的方式:NotificationTarget。我们用效果图说话吧。
这里写图片描述
模拟器不是特别美观,讲究这看下。也别太在意这些细节。具体代码会有下载地址。

0 0
原创粉丝点击