如何在Android中旋转画布上绘制的矩形?

Ant*_*met 4 android rotation rect draw android-canvas

我正在使用下面的代码在android画布上绘制文本

        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        canvas.translate(xPosition + position.getX(), yPosition + position.getY());
        paint.setColor(Color.BLUE);
        paint.setStyle(Style.STROKE);
        canvas.drawRect(rect, paint);
        paint.setStyle(Style.FILL);
        paint.setColor(text_color);
        canvas.translate(-(xPosition + position.getX()), -(yPosition + position.getY()));
        canvas.rotate(getDegreesFromRadians(angle), xPosition + position.getX() + rect.exactCenterX(), yPosition + position.getY() + rect.exactCenterY());
        canvas.drawText(text, xPosition + position.getX(), yPosition + position.getY(), paint);
Run Code Online (Sandbox Code Playgroud)

此代码负责文本的旋转,并且工作正常.我正在使用上面的代码在文本周围绘制一个蓝色矩形.现在我的问题是矩形不随文本一起旋转.它仍然保持不变.有没有办法旋转Android画布中绘制的矩形?

Nik*_*kyD 10

请用

canvas.save();
canvas.rotate();
//stuff to draw that should be rotated
canvas.restore();
Run Code Online (Sandbox Code Playgroud)

否则你必须补偿之后的每一次轮换