我是 Glide 库的新手,遵循此处的转换指南:https ://github.com/bumptech/glide/wiki/Transformations
我正在尝试创建一个自定义转换,但是当我在 Transformation 类的transform
方法中放置一个断线时,我可以看到它从未被调用。
下面是我的代码:
private static class CustomTransformation extends BitmapTransformation {
private Context aContext;
public CustomTransformation(Context context) {
super(context);
aContext = context;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return bitmapChanger(toTransform, 1080, (int) aContext.getResources().getDimension(R.dimen.big_image));
}
@Override
public String getId() {
return "some_id";
}
}
private static Bitmap bitmapChanger(Bitmap bitmap, int desiredWidth, int desiredHeight) {
float originalWidth = bitmap.getWidth();
float originalHeight = bitmap.getHeight();
float scaleX = desiredWidth / originalWidth;
float scaleY = desiredHeight / originalHeight;
//Use the larger of the two scales to maintain aspect ratio
float scale = Math.max(scaleX, scaleY);
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
//If the scaleY is greater, we need to center the image
if(scaleX < scaleY) {
float tx = (scale * originalWidth - desiredWidth) / 2f;
matrix.postTranslate(-tx, 0f);
}
return Bitmap.createBitmap(bitmap, 0, 0, (int) originalWidth, (int) originalHeight, matrix, true);
}
我尝试以两种方式启动 Glide:
Glide.with(this).load(url).asBitmap().transform(new CustomTransformation(this)).into(imageView);
和
Glide.with(this).load(url).bitmapTransform(new CustomTransformation(this)).into(imageView);
但两者都不起作用。有任何想法吗?再说一次,我不是在寻找关于 Matrix 本身的建议,我只是不明白为什么transform(...)
根本没有被调用。谢谢!