我正在尝试旋转我的子类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 被旋转,但是我的视图的边界被剪裁:
我知道这是因为我的视图的大小 - 它在旋转过程中没有改变,而它应该改变。但是如果我改变宽度\高度onMeasure
问题仍然存在 - 我正在使用LayoutParams.WRAP_CONTENT
, so TextView
只需根据中提供的值更改其大小setMeasuredDimensions
.
我怎么解决这个问题?
我认为这里的整个问题是您正在使用 WRAP_CONTENT。当您执行此操作时,视图的剪辑矩形就是文本内容的大小。解决该问题的最简单方法是使用填充。像这样的东西,对我来说效果很好:
<com.example.twistedtext.TwistedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
twistedtext:rotation="30.0f"
android:text="@string/hello_world" />
当然,如果您这样做,则必须为每个内容选择稍微不同的填充。如果您无法做到这一点,请重写 onMeasure,以便完全执行 TextView 的 onMeasure 的操作,然后根据旋转的需要添加相应的填充。
稍后添加:
事实上,弄清楚这一点很有趣。我有以下 onMeasure,效果很好:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int w = getMeasuredWidth();
int h = getMeasuredHeight();
w = (int) Math.round(w * cosA + h * sinA);
h = (int) Math.round(h * cosA + w * sinA);
setMeasuredDimension(w, h);
}
唯一剩下的问题是文本根据预旋转尺寸进行换行。你也必须解决这个问题...
sinA 和 cosA 在设置 mAngle 时计算。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)