如何旋转TextView而不剪切其边界?

Dmi*_*sev 10 android android-canvas

我正在尝试旋转我的子类TextView使用canvas.rotate():

canvas.save();

final int w = getWidth();
final int h = getHeight();

float px = w/2f;
float py = h/2f;
canvas.rotate(mAngle, px, py);

super.draw(canvas);

canvas.restore();
Run Code Online (Sandbox Code Playgroud)

旋转TextView,但我的视图边界被剪裁:

插图

我知道这是因为我的视图大小 - 它在旋转期间不会发生变化.但是,如果我将改变宽度\高度onMeasure问题将保持 - 我正在使用LayoutParams.WRAP_CONTENT,所以TextView只需根据提供的值更改它的大小setMeasuredDimensions.

我怎么解决这个问题?

Dmi*_*sev 2

乍一看,G. Blake Meike 提供的解决方案是一个很好的开始,但随之而来的是很多问题。

要解决问题还有另一种方法。它非常简单,但通常不应该使用(因为我刚刚从原始 Android 源中删除了对我来说不必要的东西)。要控制混凝土的绘制,View您可以覆盖drawChild其父级的方法(是的,您需要自己的子类ViewGroup或更具体的东西,例如FrameLayout)。这是我的解决方案的示例:

@Override
protected boolean drawChild(Canvas canvas, View child, long time) {
    //TextBaloon - is view that I'm trying to rotate
    if(!(child instanceof TextBaloon)) {
        return super.drawChild(canvas, child, time);
    }

    final int width = child.getWidth();
    final int height = child.getHeight();

    final int left = child.getLeft();
    final int top = child.getTop();
    final int right = left + width;
    final int bottom = top + height;

    int restoreTo = canvas.save();

    canvas.translate(left, top);

    invalidate(left - width, top - height, right + width, bottom + height);
    child.draw(canvas);

    canvas.restoreToCount(restoreTo);

    return true;
}
Run Code Online (Sandbox Code Playgroud)

主要想法是我向孩子授予未裁剪的画布。