如何在 Android 中使用画布旋转文本
我正在 Android 中使用 canvas 绘制饼图。我正在使用下面的代码。我在该饼图的每个切片上绘制文本(在路径上绘制圆弧),现在我想按长度绘制文本,即从每个切片的中心到末端,那么如何使用起始角度和扫描角度旋转圆弧。
p.addArc(mEventsRect, fStartAngle, fSweepAngle);
mBgPaints.setColor(iTextColor);
canvas.drawTextOnPath(sTextValue, p, fHOffSet, fVOffSet, mBgPaints);
I was drawing a pie chart using canvas in Android. I am using the below code. I draw a text on each slice of that pie chart (draw arc on path), now I want to draw the text length wise i.e. from center to end of the each slice, so how to rotate the arc using start and sweep angle.
p.addArc(mEventsRect, fStartAngle, fSweepAngle);
mBgPaints.setColor(iTextColor);
canvas.drawTextOnPath(sTextValue, p, fHOffSet, fVOffSet, mBgPaints);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试以下代码片段:(来自: http://www.helloandroid .com/tutorials/how-use-canvas-your-android-apps-part-2)
You can try this snippet: (from: http://www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-2)
您已经有了文本的 x 和 y,使用它们来旋转画布。
负号否定第一次旋转。您可以交换它以沿相反方向旋转。
您可以在循环中执行此操作,但每次坐标更改时都必须完成旋转循环。
You'll already have the x and y for your text, use these to rotate the canvas
The negative sign negates the first rotation. You could swap it around to rotate in the opposite direction.
You could do this in a loop but the rotation cycle must be done each time either coordinate changes.
也许这会对你有所帮助,,
这里39.5是半径,这将在mdpi屏幕上完美显示结果
may be this will help you,,
here 39.5 is radius,, this will perfectly show result on mdpi screen
这是我在这个库的帮助下经过两天的搜索后终于做到的 https://github.com/Ken -Yang/AndroidPieChart
,则在我的朋友的帮助下完成居中文本的方程式,并在 MainActivity onCreate 或 oncreateView 上进行大量搜索:
使用片段
如果您在布局中
Here's how i finally did it after two days of search with help of this library https://github.com/Ken-Yang/AndroidPieChart
And equations to center text done with help of my friends and alot of search
on MainActivity onCreate or oncreateView if you are using fragments:
in your Layout:
这个问题很老了,但我想我应该写一个一般性的答案。在这里,我假设您想在画布中间绘制饼图,并且您在数组中拥有起始角度和渗透角度。
This question is pretty old, but I figured I would write a general answer.Here I assume you want to draw your pie chart in the middle of the canvas and that you have your start and seep angles in an array.
现在是 2023 年,可能还有其他答案,但这里有一个肯定有效的答案
it's 2023 there might be other answers out there but here is one that is sure to work