Canvas 旋转并绘制位图

Canvas rotate and drawbitmap

我有路径矩形和位图,想在路径内添加位图,开始绘制时它按预期显示,但旋转和绘制位图时超出范围。这是我的代码。

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 为 0

RotateAngle 为 50

这是矩阵的工作解决方案。

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)
}