Android get bounds of rotated,scaled text on canva

2020-08-02 06:18发布

问题:

I have a textview with some text. Textview have touch events, so user can rotate, scale and translate text. For translation, rotation and scaling i'm using canvas. Canvas is doing this inside onDraw() function. Now i want to have outer bounds of text when it is scaled or rotated, can anybody help me. Thanks in Advance.

I try this piece of code to translate,rotate and scale text on canvas:

canvas.scale(fScaledFactorForTextview,fScaledFactorForTextview,getTextWidth() / (2.0f *   getLineCount()) + fTranslationX, (getTextSize() * getLineCount()) / 2.0f + fTranslationY);
canvas.rotate(fDegrees, getTextWidth() / (2.0f * getLineCount()) + fTranslationX,    (getTextSize() * getLineCount()) / 2.0f + fTranslationY);
canvas.translate(fTranslationX,fTranslationY);

回答1:

In general it's easier to do this if you use a matrix, rather than direct rotate and scale calls like this. If you use a matrix, you can just apply those same adjustments to the matrix. Then you can easily translate any point from normal Cartesion coordinates to the altered coordinates by multiplying the coordinates by that matrix. Matrix even provides the mapPoints function to do that.