Glide进阶详解(七)

本文详细介绍了Glide的转换功能,包括如何设置日志、实现自定义Transformation,以及如何应用单个和多个转换。还提到了Glide转换集合库glide-transformations的使用,并给出了设置和使用的示例。

Glide 的 GeneralRequest 类提供了一个方法去设置 log 的级别。不幸的是,在生产过程中,使用这个类并不容易。然而,有一个非常简单的方法去获得 Glide 的调试日志。你所要做的就是通过 adb的 shell 来激活。打开你的终端,使用以下命令:

adb shell setprop log.tag.GenericRequest DEBUG  

最后一个 DEBUG 来自标准的 Android 日志常量。因此,你你可以选择 debug 的优先级:

  • VERBOSE
  • DEBUG
  • INFO
  • WARN
  • ERROR

输出,万一图像不存在,在会像这样:

io.futurestud.tutorials.glide D/GenericRequest: load failed  
io.futurestud.tutorials.glide D/GenericRequest: java.io.IOException: Request failed 404: Not Found  
...

可能你已猜到,这只当你的设备能接收实际的值并且你正在开发和测试你的 App。为了记录在生产中的 App,你将需要用一个不同的方式。答案是,依然用回调,我们在下一节来探讨。

常规异常日志记录

Glide 不能直接去访问 GenericRequest 类去设置日志,但万一一些请求出错了你是可以捕获异常的。比如,如果图片不可用,Glide 会(默默地)抛出一个异常,并且显示一个 drawable ,如果你已经指定了 .error() 的话。如果你明确想要知道这个异常,创建一个监听并传 .listener() 方法到 Glide 的建造者中。

首先,创建一个监听作为一个字段对象去避免垃圾回收(注:之前说过不要用匿名内部类的形式):

private RequestListener<String, GlideDrawable> requestListener = new RequestListener<String, GlideDrawable>() {  
    @Override
    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
        // todo log exception

        // important to return false so the error placeholder can be placed
        return false;
    }

    @Override
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
        return false;
    }
};

在 onException 方法中, 你可以捕获错误,并且你可以决定要做什么,比如,打个 log。重要的是如果 Glide 要在后续处理的话,如显示一个错误的占位符等情况的话,你需要返回了 false 在 onException 方法中。

你可以设置一个监听在 Glide 建造者中:

Glide  
    .with( context )
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .listener( requestListener )
    .error( R.drawable.cupcake )
    .into( imageViewPlaceholder );

要使日志工作正常的话,.error() 并不是必须的。然而,如果你在监听的 onException 中返回 false 的话,R.drawable.cupcake 只是显示出来而已。

Transformations

在图片被显示之前,transformations(转换) 可以被用于图像的操作处理。比如,如果你的应用需要显示一个灰色的图像,但是我们只能访问到原始色彩的版本,你可以用 transformation 去操作 bitmap,从而将一个明亮色彩版本的图片转换成灰暗的版本。不要理解错啦,transformation 不仅限于颜色转换。你可以图片的任意属性:尺寸,范围,颜色,像素位置等等!Glide 已经包含了2个 transformation,我们之前已经看了图像重设大小,即:fitCenter 和 centerCrop。这两个选项都非常有意义,他们在 Glide 中拥有自己的实现。当然,我们这篇博客不再介绍他们。

实现你自己的 Transformation

为了实践自定义转换,你将需要创建一个新类,它实现了 Transformation 接口。要实现这个方法还是比较复杂的,你必须要有对 Glide 内部架构方面的洞察力才能做的比较棒。如果你只是想要对图片(不是 Gif 和 video)做常规的 bitmap 转换,我们推荐你使用抽象类 BitmapTransformation。它简化了很多的实现,这应该能覆盖 95% 的应用场景啦。

所以,来看看 BitmapTransformation 实现实例。如果你定期阅读这个博客,你会知道我们喜欢的转换是 用 Renderscript 模糊图像。我们可以将之前的所有代码重用到 Glide 的转换中。因为我们继承 BitmapTransformation 类,我们用这样的框架:

public class BlurTransformation extends BitmapTransformation {

    public BlurTransformation(Context context) {
        super( context );
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return null; // todo
    }

    @Override
    public String getId() {
        return null; // todo
    }
}

现在我们将之前博客中用 Renderscript 来模糊图像的代码放到我们这里来:

public class BlurTransformation extends BitmapTransformation {

    private RenderScript rs;

    public BlurTransformation(Context context) {
        super( context );

        rs = RenderScript.create( context );
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap blurredBitmap = toTransform.copy( Bitmap.Config.ARGB_8888, true );

        // Allocate memory for Renderscript to work with
        Allocation input = Allocation.createFromBitmap(
            rs, 
            blurredBitmap, 
            Allocation.MipmapControl.MIPMAP_FULL, 
            Allocation.USAGE_SHARED
        );
        Allocation output = Allocation.createTyped(rs, input.getType());

        // Load up an instance of the specific script that we want to use.
        ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setInput(input);

        // Set the blur radius
        script.setRadius(10);

        // Start the ScriptIntrinisicBlur
        script.forEach(output);

        // Copy the output to the blurred bitmap
        output.copyTo(blurredBitmap);

        toTransform.recycle();

        return blurredBitmap;
    }

    @Override
    public String getId() {
        return "blur";
    }
}

再说一次,如果你对于代码块 transform() 里的实现是困惑的,去读之前的博客getId() 方法描述了这个转换的唯一标识符。Glide 使用该键作为缓存系统的一部分,为了避免意外的问题,你要确保它是唯一的。

下一节,我们要学习如何应用我们之前创建的转换。

单个转换的应用

Glide 有两种方式去使用转换。首先是传一个的你的类的实例作为参数给 .transform()。你这里你可以使用任何转换,无论它是否是用于图像还是 Gif。其他选择是使用 .bitmapTransform(),它只能用于 bitmap 的转换。因为我们上面的实现是为 bitmap 设计的,这两者我们都可以用:

Glide  
    .with( context )
    .load( eatFoodyImages[0] )
    .transform( new BlurTransformation( context ) )
    //.bitmapTransform( new BlurTransformation( context ) ) // this would work too!
    .into( imageView1 );

运用多种转换

通常,Glide 的流式接口允许方法以链式的形式。然而对于转换却并不在这种场景下。确保你只调用了一次 .transform() 或 .bitmapTransform(),否则,之前的配置就会被覆盖掉的!然而,你还是可以运用多种转换的,通过传递多个转换对象作为参数传给 .transform() 或 .bitmapTransform()

Glide  
    .with( context )
    .load( eatFoodyImages[1] )
    .transform( new GreyscaleTransformation( context ), new BlurTransformation( context ) )
    .into( imageView2 );

这个代码片段中,我们把一个图像设置了灰度,然后做了模糊。Glide 为你自动执行了这两个转换。Awesome!

提示:当你用了转换后你就不能使用 .centerCrop() 或 .fitCenter() 了。

Glide 转换集合

如果你已经有了做什么样转换的想法,你可以会想要用到你的 App 里,花点时间看下这个库:glide-transformations。它为 Glide 转换提供了多种多样的实现。非常值得去看一下,说不定你的想法已经在它那里实现了。

这个库有两个不同的版本。扩展版本包含了更多的转换,它是通过设备的 GPU 来计算处理的。这个版本需要有额外的依赖,所以这两个版本的设置有一点不同。你应该看看所拥有的转换方法的列表,再去决定你需要使用哪个版本。

Glide 转换设置

设置起来很简单,对于基础版本你只需要在你当前的 build.gradle 中添加一行代码就可以了。

dependencies {  
    compile 'jp.wasabeef:glide-transformations:1.2.1'
}

如果你想要使用 GPU 转换:

repositories {  
    jcenter()
    mavenCentral()
}

dependencies {  
    compile 'jp.wasabeef:glide-transformations:1.2.1'
    compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0'
}

如果你想使用 BlurTransformation,你需要多一个步骤。如果你还没做的话,那就添加下面这些代码到你的 build.gradle 中。

android {  
    ...
    defaultConfig {
        ...
        renderscriptTargetApi 23
        renderscriptSupportModeEnabled true
    }
}

如果你想要知道更多关于这个步骤的东西,去看看我们的这篇博客: blog post about Renderscript

使用 Glide 的转换

当你将 build.gradle 文件在 Android Studio 同步了之后,你可以去使用这个转换集合了。使用模式和你自己定义转换的方式相同。假设我们想要做用这个集合的模糊转换去模糊一张图片:

Glide  
    .with( context )
    .load( eatFoodyImages[2] )
    .bitmapTransform( new jp.wasabeef.glide.transformations.BlurTransformation( context, 25, 2 ) )
    .into( imageView3 );

就像我们上面所以用的,你也可以使用一连串的转换。.bitmapTransform() 方法都接受一个或多个转换。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值