Android滑行:裁剪--从图像底部裁掉X个像素。

问题描述 投票:1回答:1

我需要把图片底部剪掉20px,然后缓存起来,这样设备就不用每次用户再看到图片的时候都要反复裁剪,否则对电池等不好吧?

这是我目前的情况。

        Glide
            .with(context)
            .load(imgUrl)
            .into(holder.image)



fun cropOffLogo(originalBitmap: Bitmap) : Bitmap {

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

我怎么能用 cropOffLogoglide?

EDIT:

我试着用 https:/github.combumptechglidewikiTransformations#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)' 
android crop android-glide
1个回答
0
投票

转化

要从图像中切割一些像素,你可以创建新的(自定义)变换。在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())

1
投票
© www.soinside.com 2019 - 2024. All rights reserved.