Glide库中的BitmapTransformation无法按预期工作

11

我刚接触 Glide 库,正在遵循此处找到的“Transformations guide”指南: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(...) 根本没有被调用。谢谢!


也许他们的测试用例会有用:https://github.com/bumptech/glide/blob/bf53fa653849217ddd25d6b3c54d8e05312369ee/library/src/test/java/com/bumptech/glide/load/resource/bitmap/BitmapTransformationTest.java - Dave S
我在这里没有看到明显的错误,想在GitHub上提交一个问题以便我们进行调查吗?https://github.com/bumptech/glide/issues/new - Sam Judd
@samajudd 刚刚添加了这个问题,感谢您的回复! - JMRboosties
跨链:https://github.com/bumptech/glide/issues/492 - TWiStErRob
1个回答

21

你很可能正在经历缓存问题。第一次编译和执行代码时,转换的结果会被缓存,所以下次在相同的源图像上使用它时,就不必再应用一遍。

每个转换都有一个getId()方法,用于确定转换结果是否已更改。通常情况下,转换不会更改,而仅仅是应用或未应用。您可以在开发过程中的每次构建中更改它,但这可能会很繁琐。

为了解决这个问题,您可以在Glide加载行中添加以下两个调用:

// TODO remove after transformation is done
.diskCacheStrategy(SOURCE) // override default RESULT cache and apply transform always
.skipMemoryCache(true) // do not reuse the transformed result while running

第一个选项可以更改为"NONE",但这样你每次都必须等待从互联网加载URL,而不是只读取手机中的图像。如果您可以导航到和离开涉及的转换,并且想要进行调试,则第二个选项很有用。它有助于在每次加载以清除内存缓存之前不需要重新启动。

在完成Transformation的开发后,请不要忘记将其删除,因为它们会对生产性能产生很大影响,只应在仔细考虑后使用(如果使用)。

看起来您正在尝试在加载之前将图像大小调整为某个大小,您可以使用.override(width, height).centerCrop()/.fitCenter()/.dontTransform()相结合。


谢谢。关于您的附加说明,我想要做的是使图像在顶部居中(通常是面部所在的位置)。就像如果您的imageview边界只有1个正方形的宽度/高度,而您的图像看起来像这样: [1] [2] [3] [4] [5] [6] [7] [8] [9] 我希望正方形2是imageview显示的内容。 - JMRboosties
好的,评论都放在一个段落里了,但我想你明白我的意思。无论如何,你的问题解决了我的转换未被调用的问题,谢谢! - JMRboosties

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接