如何使用 Glide 进行图片变换
来源:     阅读:7
易浩激活码
发布于 2025-11-28 23:03
查看主页

Glide 提供了强大的图片变换功能,可以在图片加载过程中对图片进行各种处理,如裁剪、缩放、旋转、圆角、滤镜等。以下是使用 Glide 进行图片变换的详细方法:


一、Glide 图片变换的核心组件

Glide 的图片变换主要依赖于以下两个核心类:

Transformation

图片变换的接口,所有具体的变换都需要实现这个接口。主要方法: transform(Bitmap toTransform, int outWidth, int outHeight),用于对图片进行变换处理。

Transformations

Glide 提供的一个工具类,包含了多种常用的图片变换,如  centerCrop fitCenter circleCrop roundedCorners 等。

二、使用 Glide 内置的图片变换

Glide 内置了多种常用的图片变换,可以直接通过  transform() 方法或链式调用使用。

1. 常用内置变换

变换方法功能
centerCrop()中心裁剪,将图片缩放至至少覆盖目标尺寸,并从中心裁剪成目标尺寸。
fitCenter()适应中心,将图片缩放至完全在目标尺寸内,并居中显示。
circleCrop()圆形裁剪,将图片裁剪成圆形。
roundedCorners(int radius)圆角裁剪,将图片的四个角裁剪成指定半径的圆角。
rotate(float degrees)旋转图片,按指定角度旋转。
scale(float scaleX, float scaleY)缩放图片,按指定比例缩放。

2. 使用示例

(1)基本变换


Glide.with(this)
        .load("https://example.com/image.jpg")
        .centerCrop() // 中心裁剪
        .into(imageView);


Glide.with(this)
        .load("https://example.com/image.jpg")
        .fitCenter() // 适应中心
        .into(imageView);
(2)圆形裁剪


Glide.with(this)
        .load("https://example.com/image.jpg")
        .circleCrop() // 圆形裁剪
        .into(imageView);
(3)圆角裁剪


Glide.with(this)
        .load("https://example.com/image.jpg")
        .roundedCorners(20) // 圆角半径为 20px
        .into(imageView);
(4)旋转图片


Glide.with(this)
        .load("https://example.com/image.jpg")
        .rotate(90) // 旋转 90 度
        .into(imageView);
(5)组合变换

可以使用  transform() 方法组合多个变换:



Glide.with(this)
        .load("https://example.com/image.jpg")
        .transform(new CenterCrop(), new RoundedCorners(20)) // 先中心裁剪,再圆角
        .into(imageView);

或者使用  Transformations 工具类:



Glide.with(this)
        .load("https://example.com/image.jpg")
        .transform(Transformations.centerCrop(), Transformations.roundedCorners(20))
        .into(imageView);

三、使用自定义图片变换

如果 Glide 内置的变换无法满足需求,可以自定义  Transformation

1. 自定义  Transformation 步骤

创建一个类实现  Transformation<Bitmap> 接口。实现  transform() 方法,在该方法中对图片进行自定义处理。(可选)实现  updateDiskCacheKey() 方法,用于更新磁盘缓存的 Key。

2. 自定义示例:灰度变换



import android.graphics.Bitmap;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
 
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
 
public class GrayscaleTransformation implements Transformation<Bitmap> {
 
    private final BitmapPool bitmapPool;
 
    public GrayscaleTransformation(BitmapPool bitmapPool) {
        this.bitmapPool = bitmapPool;
    }
 
    @Override
    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
        Bitmap source = resource.get();
 
        // 创建一个新的 Bitmap
        Bitmap bitmap = bitmapPool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
 
        // 创建画布和画笔
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
 
        // 设置颜色矩阵为灰度
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0); // 饱和度为 0,即灰度
        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
        paint.setColorFilter(filter);
 
        // 绘制灰度图片
        canvas.drawBitmap(source, 0, 0, paint);
 
        // 返回处理后的 Bitmap
        return BitmapResource.obtain(bitmap, bitmapPool);
    }
 
    @Override
    public void updateDiskCacheKey(java.util.UUID key) {
        // 更新磁盘缓存的 Key,确保缓存能正确失效
        key.update("grayscale");
    }
}

3. 使用自定义变换



Glide.with(this)
        .load("https://example.com/image.jpg")
        .transform(new GrayscaleTransformation(Glide.get(this).getBitmapPool()))
        .into(imageView);

四、使用第三方图片变换库

除了自定义变换,还可以使用第三方库提供的丰富变换效果,例如  glide-transformations

1. 添加依赖



dependencies {
    implementation 'jp.wasabeef:glide-transformations:4.3.0'
}

2. 使用示例

(1)模糊效果


Glide.with(this)
        .load("https://example.com/image.jpg")
        .transform(new BlurTransformation(25)) // 模糊半径为 25
        .into(imageView);
(2)滤镜效果


Glide.with(this)
        .load("https://example.com/image.jpg")
        .transform(new SepiaFilterTransformation()) // 棕褐色滤镜
        .into(imageView);
(3)组合多种效果


Glide.with(this)
        .load("https://example.com/image.jpg")
        .transform(
                new CenterCrop(),
                new RoundedCorners(20),
                new BlurTransformation(10)
        )
        .into(imageView);

五、注意事项

图片变换的顺序

多个变换的执行顺序会影响最终效果,例如先裁剪再圆角,与先圆角再裁剪的结果可能不同。

性能影响

复杂的图片变换(如模糊、滤镜)会消耗较多的 CPU 和内存,可能影响加载速度。建议在非主线程中执行复杂变换,或使用  RequestOptions.diskCacheStrategy() 缓存变换后的图片。

内存管理

自定义变换时,应使用 Glide 的  BitmapPool 来复用 Bitmap,避免频繁创建和销毁 Bitmap 导致内存泄漏。

版本兼容性

Glide 4.x 与 Glide 3.x 的变换 API 有所不同,使用时需注意版本兼容性。

总结

Glide 提供了灵活且强大的图片变换功能,无论是使用内置变换、自定义变换还是第三方库,都能轻松实现各种图片处理效果。在实际开发中,可以根据需求选择合适的变换方式,并注意性能和内存管理。

免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 系统环境
相关推荐
大数据数据质量避坑指南:10年资深工程师总结的30个实战经验
python web渗透测试学习2Web应使用交互1HTTP基础
8条关于Web前台性能的优化建议
基于Mock.js的jmeter插件
当Activity跳转偶遇单身多年的老汉
首页
搜索
订单
购物车
我的