Canvas rotate and drawbitmap

爷,独闯天下 提交于 2021-01-07 01:36:08

问题


I have path rect and bitmap, want to add bitmap inside path, While draw on start it appears as expected, but while rotate and draw bitmap goes outside bounds. Here is my code.

canvas!!.rotate(rotateAngle.toFloat(), rectF2.centerX(), rectF2.centerY())
canvas!!.drawPath(path, mPaint)
bitmap?.let {
    canvas!!.drawBitmap(it, rectF2.left, rectF2.top, mPaint)
}
canvas!!.restore()

RotateAngle is 0

RotateAngle is 50


回答1:


Here is the working solution with matrix.

bitmap?.let {
    val rotate = Matrix()
    rotate.setRotate(rotateAngle.toFloat(), it.width / 2.0f, it.height / 2.0f)
    rotate.postTranslate((rectF2.centerX() - it.width / 2.0f), (rectF2.centerY() - it.height / 2.0f))
    canvas!!.drawBitmap(it, rotate, mPaint)
}


来源:https://stackoverflow.com/questions/65435556/canvas-rotate-and-drawbitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!