安卓Glide:裁剪 - 从图像底部剪切X个像素

6
我需要从图片底部裁剪20像素并缓存它,这样设备在用户再次查看图片时就不需要一遍又一遍地裁剪,否则会对电池等产生不良影响,对吧?
目前,我已经做到了这一点:
        Glide
            .with(context)
            .load(imgUrl)
            .into(holder.image)



fun cropOffLogo(originalBitmap: Bitmap) : Bitmap {

    return Bitmap.createBitmap(
        originalBitmap,
        0,
        0,
        originalBitmap.width,
        originalBitmap.height - 20
    )
}

我如何在glide中使用cropOffLogo

编辑:

我尝试使用https://github.com/bumptech/glide/wiki/Transformations#custom-transformations

private static class CutOffLogo extends BitmapTransformation {

    public CutOffLogo(Context context) {
        super(context);
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
                               int outWidth, int outHeight) {

        Bitmap myTransformedBitmap = Bitmap.createBitmap(
                toTransform,
                10,
                10,
                toTransform.getWidth(),
                toTransform.getHeight() - 20);

        return myTransformedBitmap;
    }
}

并且出现这些错误:

Modifier 'private' not allowed here

Modifier 'static' not allowed here

'BitmapTransformation()' in 'com.bumptech.glide.load.resource.bitmap.BitmapTransformation' cannot be applied to '(android.content.Context)' 

可能是您在错误的位置创建了类。请查看此问题 - Vinicius Veríssimo
我把它放在其他类所在的同一个位置。当我将private static更改为public abstract时,没有出现任何错误,但是在super(context)中仍然存在“'com.bumptech.glide.load.resource.bitmap.BitmapTransformation'中的'BitmapTransformation()'无法应用于'(android.content.Context)'”错误。 - user12880465
2个回答

6

变换

如果您想从图像中裁剪一些像素,则可以创建新的(自定义)变换。在 Kotlin 中:

class CutOffLogo : BitmapTransformation() {
    override fun transform(
        pool: BitmapPool,
        toTransform: Bitmap,
        outWidth: Int,
        outHeight: Int
    ): Bitmap =
        Bitmap.createBitmap(
            toTransform,
            0,
            0,
            toTransform.width,
            toTransform.height - 20   // numer of pixels
        )

    override fun updateDiskCacheKey(messageDigest: MessageDigest) {}
}

或者在Java中:

public class CutOffLogo extends BitmapTransformation {

    @Override
    protected Bitmap transform(
            @NotNull BitmapPool pool,
            @NotNull Bitmap toTransform,
            int outWidth,
            int outHeight
    ) {

        return Bitmap.createBitmap(
                toTransform,
                0,
                0,
                toTransform.getWidth(),
                toTransform.getHeight() - 20   // numer of pixels
        );
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }
}

调用它

Kotlin 中

.transform(CutOffLogo())

或者在Java中

.transform(new CutOffLogo())

0

我在这个类中遇到了各种错误:修饰符“private”不允许在此处,修饰符“static”不允许在此处,“com.bumptech.glide.load.resource.bitmap.BitmapTransformation”中的“BitmapTransformation()”无法应用于“(android.content.Context)”。 - user12880465
你能把这次尝试添加到问题中吗? - Vinicius Veríssimo
好的,我做了。到目前为止我并没有真正学过JAVA,与Kotlin相比,它绝对是令人讨厌的 xD。 - user12880465

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