画布旋转和绘制位图 [英] Canvas rotate and drawbitmap

查看:48
本文介绍了画布旋转和绘制位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

旋转角度为50

推荐答案

以下是使用矩阵的有效解决方案.

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

这篇关于画布旋转和绘制位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆