使用画布旋转位图中的文本

时间:2016-04-05 07:03:59

标签: android image text rotation android-canvas

位图只包含文本,因此位图的宽度和高度由文本的宽度和高度以及一些数学确定。现在当我使用

旋转文本时
canvas.rotate(tilt - 180, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
//the seek bar has a range of 0 to 360 so in order to get the right tilt i need to use (tilt - 180) instead of (tilt)

这就是发生的事情

enter image description here

显然旋转中心是错误的。我试图使用canvas.translate,结果变得更好,但它们还不正确:

    float actualTilt = Math.abs(tilt - 180);
    actualTilt = (actualTilt >= 90 && actualTilt != 180) ? 90 - actualTilt % 90 : actualTilt;
    if (actualTilt == 180) actualTilt = 0;
    // by above statements i make sure actualTilt is less than 90 and it's values are 0,1,...89,90,89,88,...0
    float v = actualTilt / 90;
    canvas.translate((-(textWidth / 2) * v), (textHeight / 2) * v);
    //i don't understand the above statement either but this is the result

enter image description here

如果文本变长,结果会变得更糟

1 个答案:

答案 0 :(得分:0)

我发现here以下代码在画布中旋转位图:

Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
yourCanvas.drawBitmap(yourBitmap, matrix, null);

你试过这个吗?