Skip to content

Instantly share code, notes, and snippets.

@hyuni
Created December 25, 2021 09:52
Show Gist options
  • Save hyuni/b6c24312871aeeb9da7c7328a8ead3f0 to your computer and use it in GitHub Desktop.
Save hyuni/b6c24312871aeeb9da7c7328a8ead3f0 to your computer and use it in GitHub Desktop.
matrix rotate wrong position
override fun onDraw(canvas: Canvas?) {
contentBitmap.draw(canvas)
// get rotate from matrix
val matrixRotate = contentBitmapLayer.matrix.rotation
// get contentBitmapLayer bounds from matrix
val x1 = bitmapPoints[0]
val y1 = bitmapPoints[1]
val x2 = bitmapPoints[2]
val y2 = bitmapPoints[3]
val x3 = bitmapPoints[4]
val y3 = bitmapPoints[5]
val x4 = bitmapPoints[6]
val y4 = bitmapPoints[7]
// calculate mid point
val contentMidPoint = calculateMidPoint()
val tx = contentMidPoint.x - (rotateIcon.width / 2).toFloat()
val ty = contentMidPoint.y - (rotateIcon.height / 2.toFloat())
// rotateIcon draw start point
val nx = (w - rotateIcon.width) / 2.0f
val ny = rotateIcon.height + marginHeight
// rotateIcon center translate contentBitmap center
rotateIcon.matrix.setTranslate(tx, ty)
rotateIcon.matrix.postRotate(matrixRotate, rotateIcon.width / 2.toFloat(), rotateIcon.height / 2.toFloat())
rotateIcon.matrix.postTranslate(-tx, -ty)
rotateIcon.matrix.postTranslate(x1 + nx, y1 - ny)
rotateIcon.draw(canvas)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment