简体   繁体   English

在中心的椭圆形路径上绘制文本

[英]Draw text on oval path in the centre

I'm trying to draw some text on path using the canvas in Android. 我正在尝试使用Android中的画布在路径上绘制一些文本。 I can successfully get the text to draw on the top oval line. 我可以成功地将文本绘制在顶部椭圆线上。 What i'm struggling to do is make sure that the text is centered. 我正在努力做的是确保文本居中。

The problem is that while i can measure the text before it gets drawn round the top line of the oval, i cannot measure it after. 问题是,虽然我可以在圆形顶部线条绘制之前测量文本,但我无法测量它。 This makes estimating where the text should be drawn on the top oval line problematic as it's always a little bit out. 这使得估计应该在顶部椭圆线上绘制文本的位置存在问题,因为它总是有点过时。 This is because when i measure the text to be drawn initially it is measured in a straight line and not as presented on the top line of the oval. 这是因为当我测量最初要绘制的文本时,它是以直线测量的,而不是在椭圆的顶行上显示的。

I've tried applying setTextAlign(Paint.Align.CENTER) but this makes the text get drawn about 3/4's of the way round and not what i want. 我已经尝试过应用setTextAlign(Paint.Align.CENTER)但这会让文本得到大约3/4的方式而不是我想要的。

Does anyone have any ideas how i could calculate the point to draw the text so it is drawn dead centre of the top oval line or any alternative method that may help? 有没有人有任何想法如何我可以计算绘制文本的点,所以它被绘制为顶部椭圆线的死点或任何可能有帮助的替代方法?

This has to be drawn using the canvas. 这必须使用画布绘制。 It can't be done using XML TextViews etc. 使用XML TextViews等无法完成。

Thanks. 谢谢。

椭圆形在上面绘制文字

You can check how this library done it: https://github.com/amulyakhare/TextDrawable 您可以查看此库如何完成它: https//github.com/amulyakhare/TextDrawable

Its using the same approach. 它使用相同的方法。 But for multiple lines its almost impossible, theres an issue on this library where me and other developers tried to implement it, and we end up using just xml (a round drawable as a linearLayout view background and one textView inside it). 但是对于多行几乎不可能,这个库和我和其他开发人员试图实现它的问题,我们最终只使用xml(圆形drawable作为linearLayout视图背景和其中的一个textView)。

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

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