Glide 提供了强大的图片变换功能,可以在图片加载过程中对图片进行各种处理,如裁剪、缩放、旋转、圆角、滤镜等。以下是使用 Glide 进行图片变换的详细方法:
Glide 的图片变换主要依赖于以下两个核心类:
Transformation:
transform(Bitmap toTransform, int outWidth, int outHeight),用于对图片进行变换处理。
Transformations:
centerCrop、
fitCenter、
circleCrop、
roundedCorners 等。
Glide 内置了多种常用的图片变换,可以直接通过
transform() 方法或链式调用使用。
| 变换方法 | 功能 |
|---|---|
centerCrop() | 中心裁剪,将图片缩放至至少覆盖目标尺寸,并从中心裁剪成目标尺寸。 |
fitCenter() | 适应中心,将图片缩放至完全在目标尺寸内,并居中显示。 |
circleCrop() | 圆形裁剪,将图片裁剪成圆形。 |
roundedCorners(int radius) | 圆角裁剪,将图片的四个角裁剪成指定半径的圆角。 |
rotate(float degrees) | 旋转图片,按指定角度旋转。 |
scale(float scaleX, float scaleY) | 缩放图片,按指定比例缩放。 |
Glide.with(this)
.load("https://example.com/image.jpg")
.centerCrop() // 中心裁剪
.into(imageView);
Glide.with(this)
.load("https://example.com/image.jpg")
.fitCenter() // 适应中心
.into(imageView);
Glide.with(this)
.load("https://example.com/image.jpg")
.circleCrop() // 圆形裁剪
.into(imageView);
Glide.with(this)
.load("https://example.com/image.jpg")
.roundedCorners(20) // 圆角半径为 20px
.into(imageView);
Glide.with(this)
.load("https://example.com/image.jpg")
.rotate(90) // 旋转 90 度
.into(imageView);
可以使用
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。
Transformation 步骤
Transformation<Bitmap> 接口。实现
transform() 方法,在该方法中对图片进行自定义处理。(可选)实现
updateDiskCacheKey() 方法,用于更新磁盘缓存的 Key。
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");
}
}
Glide.with(this)
.load("https://example.com/image.jpg")
.transform(new GrayscaleTransformation(Glide.get(this).getBitmapPool()))
.into(imageView);
除了自定义变换,还可以使用第三方库提供的丰富变换效果,例如
glide-transformations。
dependencies {
implementation 'jp.wasabeef:glide-transformations:4.3.0'
}
Glide.with(this)
.load("https://example.com/image.jpg")
.transform(new BlurTransformation(25)) // 模糊半径为 25
.into(imageView);
Glide.with(this)
.load("https://example.com/image.jpg")
.transform(new SepiaFilterTransformation()) // 棕褐色滤镜
.into(imageView);
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 提供了灵活且强大的图片变换功能,无论是使用内置变换、自定义变换还是第三方库,都能轻松实现各种图片处理效果。在实际开发中,可以根据需求选择合适的变换方式,并注意性能和内存管理。