When inverting colors of the bitmap dimensions changes (image crops) on Android

Viewed 26

I want to invert the colors of the bitmap and I have this code:

private fun Bitmap.invertColors(): Bitmap {
    val bitmap = Bitmap.createBitmap(
        width,
        height,
        Bitmap.Config.ARGB_8888
    )

    val matrixInvert = ColorMatrix().apply {
        set(
            floatArrayOf(
                -1.0f, 0.0f, 0.0f, 0.0f, 255.0f,
                0.0f, -1.0f, 0.0f, 0.0f, 255.0f,
                0.0f, 0.0f, -1.0f, 0.0f, 255.0f,
                0.0f, 0.0f, 0.0f, 1.0f, 0.0f
            )
        )
    }

    val paint = Paint()
    ColorMatrixColorFilter(matrixInvert).apply {
        paint.colorFilter = this
    }

    Canvas(bitmap).drawBitmap(this, 0f, 0f, paint)
    return bitmap
}

But after inversion, dimensions of the image changes (like 50% crop) and I don't know why. If I pass in this function dimensions of the Image View it crops less but still, it crops.

Please, help me. Thanks!

0 Answers
Related