如何TextView的旋转无削减其边界? [英] How to rotate TextView without clipping its bounds?

查看:132
本文介绍了如何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 = 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 width\height 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.

这篇关于如何TextView的旋转无削减其边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆