简体   繁体   English

更改TextView的画布大小

[英]change canvas size for TextView

I need use TextView with outline text. 我需要将TextView与大纲文本一起使用。 I try use it: https://stackoverflow.com/a/10294290/4181010 , but it doesn't work correctly with android:layout_width="wrap_content" , because it doesn't increase canvas when add outlines. 我尝试使用它: https : //stackoverflow.com/a/10294290/4181010 ,但它不能与android:layout_width="wrap_content"一起正常使用,因为添加轮廓时它不会增加画布。

在此处输入图片说明

I need to increase the canvas to include the value of strokeWidth. 我需要增加画布以包含strokeWidth的值。

I suggest I have to override onMeasure somehow, but TextView call final method setMeasuredDimension at the end of onMeasure to apply view size and I can not intervene at this point so as not to have to rewrite the whole method. 我建议我必须以某种方式重写onMeasure,但是TextView在onMeasure的末尾调用最终方法setMeasuredDimension以应用视图大小,并且我此时不能进行干预,以免不必重写整个方法。

Scaling the canvas in OnDraw does not work either, because TextView scales draw text with canvas. 在OnDraw中缩放画布也不起作用,因为TextView缩放使用画布绘制文本。

My solution is increase canvas and use canvas.translate() to move in right position before draw text. 我的解决方案是增加canvas并使用canvas.translate()在绘制文本之前移到正确的位置。
And my question: How can I increase the size of the canvas with minimal intervention? 我的问题是:如何以最少的干预增加画布的尺寸? Or someone know another solution for this problem? 还是有人知道这个问题的另一种解决方案?

I'm guessing you are drawing the text twice, once with the orange (manually on the canvas) and once with the white (what the TextView is drawing with super.onDraw ). 我猜您正在两次绘制文本,一次是用橙色(通常在画布上)绘制,一次是用白色( TextViewsuper.onDraw绘制)。

Instead of drawing the larger orange manually, you could instead draw the smaller white text and leave the orange text to TextView 's super.onDraw . 您可以手动绘制较小的白色文本,而不是手动绘制较大的橙色文本,而将橙色文本留给TextViewsuper.onDraw That way everything should fit within the canvas. 这样,所有内容都应适合画布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM