绘制具有一定像素宽度的线

发布于 2024-11-01 20:33:11 字数 520 浏览 1 评论 0原文

我想使用具有一定宽度(以像素为单位,理想情况下为“dp”)的线条在画布上显示 X/Y 数据。我尝试过 Paint 的 setStrokeWidth(..) 方法,这确实改变了线宽,但这不是我需要的。在我的例子中,我使用matrix.preScale(xScale, yScale)将画布缩放为“真实工程单位”,因此X比例代表0到100,Y比例代表0到1。Paint对象的setStrokeWidth()方法似乎设置笔划,使其遵循我的矩阵 preScale() 设置。换句话说,水平线画得很细,垂直线画得很粗。

有没有办法配置Paint,使得无论从哪个方向绘制线条,其宽度都是一致的像素数?

我尝试定义一个 Drawable ,它是一条线,并从中创建一个 ShapeDrawable ,然后将其应用到 Paint ,但在运行时遇到了一些令人讨厌的类转换错误。这让我觉得这是错误的做法。但也许我放弃得太早了。

据我所知,有许多可用的 Android 绘图/图表包,其中一些带有源代码,但我真的希望了解这里的平台,而不是使用第三方解决方案。

感谢您的任何提示! 富有的

I would like to display X/Y data on a canvas using lines with a certain width (in pixels, or "dp" ideally). I have tried the setStrokeWidth(..) method of Paint, and that indeed does change the line width but it isn't what I need. In my case I've scaled my canvas to "real Engineering units" using matrix.preScale(xScale, yScale) so the X scale represents 0 to 100 and the Y is 0 to 1. The setStrokeWidth() method of the Paint object seems to set the stroke so that it follows my matrix preScale() settings. In other words, horizontal lines are drawn really thin, and vertical lines are drawn really thick.

Is there a way to configure the Paint so that no matter which direction the line is drawn, its width is a consistent number of pixels?

I have tried defining a Drawable that is a line, and making a ShapeDrawable from that, and then applying it to the Paint but ran into some nasty class casting errors at run time. This made me think that this was the wrong way to go about it. But maybe I gave up too soon.

I understand that are a number of Android plotting/charting packages available, some with source, but I'm really looking to understand the platform here, rather than using a third-party solution.

Thanks for any hints!
Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2024-11-08 20:33:11

例如在 Android 上:paint.setStrokeWidth(3);该方法设置线宽。在本例中,线宽为 3 像素。我正在黑莓上搜索就像这种方法。

试试这个:

    Graphics.setColor(Color.RED);
    Graphics.setStrokeWidth(20);
    Graphics.drawRect(0, 0, 100, 100);

它们在 net.rim.device.api.ui.Graphics 包中可用。

For example on Android : paint.setStrokeWidth(3); This method sets line width. In this case, line width is 3 pixels. I am searching just like this method on BlackBerry.

Try this :

    Graphics.setColor(Color.RED);
    Graphics.setStrokeWidth(20);
    Graphics.drawRect(0, 0, 100, 100);

They are available in net.rim.device.api.ui.Graphics package.

留一抹残留的笑 2024-11-08 20:33:11

在地图上绘制叠加层时,我必须做类似的事情。

由于用户可以使用多点触控动态缩放地图,因此当我进入绘制例程时,我计算 x 和 y 轴的缩放因子,将其应用到画布,绘制地图,然后反转该缩放因子并应用它到覆盖层的线宽。因此,画布被缩放,覆盖层被缩放,然后反缩放,因此它们实际上是固定宽度的。

我发现这非常有效,没有真正的性能影响。

I had to do something similar when drawing overlays onto a map.

As the user can use multi-touch to scale the map dynamically, when I enter the draw routine I calculate a scaling factor for the x and y axes, apply it to the canvas, draw the map, then invert this scaling factor and apply it to the line widths for the overlays. Thus the canvas is scaled and the overlays are scaled, then anti-scaled, so they are in effect fixed width.

I found this works extremely well with no real performance hit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文