如何在 Android 中构建图形针规 UI 小部件?
我的应用程序需要一个图形针规(如速度计等),但这样的 UI 小部件不是 SDK 的一部分,因此我可能必须自己创建它。
我的想法是将带有刻度线和彩色字段(绿色、黄色、红色)的背景作为一个位图,将针作为另一位图绘制在背景之上,但以适当的角度旋转。
在我的《Professional Android 2 Application Development》一书中,有一个与罗盘有些类似的示例,尽管该示例是使用线条图形绘制的,而不是像我必须使用的预制图像以获得所需的外观。
但是,在指南针示例中,在绘制刻度线之前会旋转整个画布。我不能使用这种方法,因为它也会旋转仪表背景。所以我需要在叠加之前以某种方式旋转针图像(应该是透明的)。但我不知道如何做到这一点。
任何人都可以引导我正确的方向来了解如何进行针规吗?另外,如果有比上面列出的更好的构建仪表的方法,请告诉我。
I need a graphical needle gauge (like a speedometer etc) for my app but such a UI widget is not part of the SDK so I probably have to create it myself.
My idea is to have the background with the tickmarks and coloured fields (green, yellow, red) as one bitmap and the needle as another bitmap drawn on top of the background, but rotated in the appropriate angle.
In my book, Professional Android 2 Application Development, there is a somewhat similar example with a compass rose, although that one is drawn using line graphics, not pre-fabricated images like I will have to use to get the desired look.
However, in the compass example the whole canvas is rotated before drawing the tick marks. I cannot use this approach as it will also rotate the gauge background. So I need to somehow rotate the needle image (which should be transparent) before superimposing it. But I don't know how to do accomplish this.
Can anyone lead me in the right direction on how to proceed with the needle gauge? Also, if there is a better way to build the meter than sketched above, please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将量规分为不同的层。一张用于背景,一张用于刻度线。刻度线图层可以旋转以绘制标记,并且当转回并与“背景”图层组合时。
您可以使用上述分层技术查看以下示例: http://mindtherobot.com/blog/534/android-ui-making-an-analog-rotary-knob/
PS这不是我的博客,我刚刚在那里找到了这种技术。
You can divide your guage into different layers. One for background, one for tick marks. Layer for tick marks can be rotated to draw marks and when turned back and combined with 'background' layer.
You can see the following example with layer technique described above: http://mindtherobot.com/blog/534/android-ui-making-an-analog-rotary-knob/
P.S. This is not my blog, i've just found this technique there.