0

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

enter image description here

RotateAngle is 50

enter image description here

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128

1 Answers1

0

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)
}
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • you dont have to use `drawBitmap` with `Matrix` parameter - if you first `rotate()` your `Canvas` it should work just fine - every `Canvas` related `draw*` call will operate on the rotated canvas – pskink Dec 28 '20 at 08:51
  • and what do you want to do actually? rotate everything with the pivot point located on the center of `rectF2`? in flutter it is as simple as: `canvas.translate(rectF2.center.dx, rectF2.center.dy); canvas.rotate(-math.pi * 0.1); canvas.translate(-rectF2.center.dx, -rectF2.center.dy); canvas.drawRect(rectF2, Paint()); canvas.drawRect(Rect.fromLTWH(100, 200, 200, 200), Paint()..color = Colors.green); ` – pskink Dec 28 '20 at 10:45
  • @pskink, Is that possible to implement pinch-zoom SurfaceView? – Gunaseelan Jan 29 '21 at 13:24
  • yes, with [MatrixGestureDetector](https://stackoverflow.com/a/21657145/2252830) – pskink Jan 29 '21 at 13:27
  • @pskink But actually this one zoom only individual layer only right? Can we zoom whole SurfaceView with this? – Gunaseelan Jan 29 '21 at 13:34