如何TextView的旋转无削减其边界?边界、TextView

由网友(笑你像狗)分享简介:我试图用旋转我的的TextView 的子 canvas.rotate():I'm trying to rotate my subclass of TextView using canvas.rotate():canvas.save();final int w = getWidth();final int h =...

我试图用旋转我的的TextView 的子 canvas.rotate()

I'm trying to rotate my subclass of TextView using 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();

TextView的旋转,但是我的观点的界限,剪辑:

TextView is rotated, however bounds of my view are clipped:

据我所知,这是因为我的看法的大小 - 它旋转过程中不会改变,而它应该。但是,如果我会改变宽度高度 onMeasure 问题始终存在 - 我使用 LayoutParams.WRAP_CONTENT ,所以的TextView 只是根据 setMeasuredDimensions 提供的值改变它的大小。

I understand that this is because of my view's size - it is not changed during rotation while it should. But if I'll change widthheight in onMeasure problem will remain - I'm using LayoutParams.WRAP_CONTENT, so TextView just change it's size according to values provided in setMeasuredDimensions.

我该如何解决这个问题呢?

How can I solve this problem?

推荐答案

目前由G.布雷克美克提供的第一次看的解决方案是一个良好的开端,但还有很多问题upcome。

At the first look solution provided by G. Blake Meike is a good way to start, however there is a lot of problems upcome.

要解决的问题有一个另一种方式。这很简单,但一般不应使用(因为我刚刚删除unneccesary对我来说,从原生Android源代码的东西)。采取控制混凝土的图纸查看则可以覆盖 drawChild 的方法它的父(是的,你需要自己的子类对的ViewGroup 或更多的东西像的FrameLayout 混凝土)。下面是我的解决方案的例子:

To overcome problems there is an another way. It's quite simple, but generally shouldn't be used (since I've just deleted unneccesary for me stuff from original android source). To take control over drawing of concrete View you can override drawChild method of it's parent (yes, you need your own subclass of ViewGroup or something more concrete like FrameLayout). Here is an example of my solution:

@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;
}

主要的想法是,我给予非限幅帆布孩子。

Main idea is that I'm granting non-clipped canvas to child.

阅读全文

相关推荐

最新文章