how to rotate a bitmap 90 degrees

后端 未结 10 1596
离开以前
离开以前 2020-11-28 03:09

There is a statement in android canvas.drawBitmap(visiblePage, 0, 0, paint);

When I add canvas.rotate(90), there is no effect. But if I wri

10条回答
  •  无人及你
    2020-11-28 03:34

    Just be careful of Bitmap type from java platform call like from comm1x's and Gnzlt's answers, because it might return null. I think it is also more flexible if the parameter can be any Number and use infix for readability, depends on your coding style.

    infix fun Bitmap.rotate(degrees: Number): Bitmap? {
        return Bitmap.createBitmap(
            this,
            0,
            0,
            width,
            height,
            Matrix().apply { postRotate(degrees.toFloat()) },
            true
        )
    }
    

    How to use?

    bitmap rotate 90
    // or
    bitmap.rotate(90)
    

提交回复
热议问题