学而实习之 不亦乐乎

Android 使用 Glide 实现自定义变换详细过程

2023-07-17 06:45:43

一、概述

在图片显示出之前可以对图片进行变换处理。如:大小、边框、色彩、像素点等。在 Glide 中实现图片变换可以使用其内置的一些方法实现一些常规变换,但对于一些特殊需求,就需要进行定义变换,这里进行重点描述。

二、自定义变换

1、实现自定义变换

为了实现自定义变换,你需要创建一个新的类去实现变换接口。这个方法需要实现的内容还是相当复杂的,你需要深入探索Glide的内部结构才能让其工作好。如果你只是想要常规的图片(不包括Gif和视频)变换,建议只要处理抽象的 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";
    }
}

2、应用一个简单的变换

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

Glide从网络下载的图片自动实现模糊算法。

三、实现多重变换

通常,Glide 的流接口(fluent interface)允许方法被连接在一起,然而变换并不是这样的。确保你只调用.transform()或者.bitmapTransform()一次,否则之前的设置将会被覆盖。你可以通过传递多个转换对象当作参数到.transform()(或者.bitmapTransform())中来进行多重变换,如下代码:

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

在这段代码中,我们先对图片进行了灰度变换,然后模糊处理。Glide会为你自动进行两个转换。

提示:当你使用变换的时候,你不能使用.centerCrop()或者.fitCenter()。

四、Glide 的变换集合(Glide-transformations)

Glide-transformations。它提供了许多变换的集合。这个库有2个不同版本。扩展库包括更多的变换,并且是用手机的GPU进行计算。需要一个额外的依赖,所以这两个版本的设置还有点不一样。你应当看看支持的变换的列表,再决定你需要用哪个版本。

使用也很简单,只需要在 build.gradle 里加入依赖,如下:

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

如果你想要使用GPU变换:

repositories {  
    jcenter()
    mavenCentral()
}
 
dependencies {  
    compile 'jp.wasabeef:glide-transformations:2.0.0'
    compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0'
}

加入依赖后,同步了 Android Studio 的 builde.gradle 文件后,就可以进行使用变换集合了。如下:

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

像前面一样,可以应用一组变换,也可以是一个单独的变换。