ItemizedOverlay 似乎是用“损坏的”绘图来绘制的。投影

发布于 2024-09-14 17:13:36 字数 1186 浏览 1 评论 0原文

我有一个奇怪的问题,我真的不知道如何解决,所以我想知道以前是否有人遇到过类似的问题。

我重写了 ItemizedOverlay 子类中的 draw(Canvas canvas, MapView mapView, boolean Shadow) 方法,以在项目之间绘制一些路径、文本等。我在覆盖中做的第一件事是使用所有这些参数调用 super.draw 。当我启动应用程序时,我在覆盖中绘制的线条位于它们应该在的位置,但 super.draw 绘制的实际 OverlayItems 却偏离了原来的位置。有趣的是,当我将地图移向左上角(坐标 0,0)时,OverlayItems 越来越接近我期望它们所在的位置(事实上,当我获得第一个值时,它们似乎完美匹配)项到点 (0,0))。当我移动地图以使 OverlayItems 远离 (0,0) 点时,它们会线性地远离其地理位置。

如果以前没有人遇到过这个问题,而且这不是显而易见的问题,我将尝试制作一个仍然具有此行为的最小示例并将其发布在这里。实际代码相对较大,因此如果有人知道可能是什么问题,我想避免那么多工作。

编辑:一条可能有用的附加信息...我的绘制方法如下所示:

public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, shadow);
    if (!shadow) {
        // code for drawing the lines etc.
        // on the canvas (uses mapView.getProjection()) 
    }
}

叠加层的自定义部分没有阴影层(我在毫无问题地扩展叠加层时使用了相同的逻辑)。 super.draw 为 OverlayItems 绘制的阴影层实际上位于 Drawable 和每个特定项目的预期地理位置之间。

以下是来自 avd 的树屏幕截图,可以更好地说明正在发生的情况。四个红色图钉应该位于它们上方的红线上,并且当我将地图向左上方滑动时,它们实际上会朝该线移动。两个引脚和它们之间的线是在我的自定义覆盖中绘制的,super.draw 绘制四个阴影和四个额外的红色引脚。

由于这是我的第一篇文章,我无法发布多个链接,因此我将所有三个屏幕截图放在同一个 jpg 上。
屏幕截图

I have a strange problem that I don't really know how to attack so I'm wondering if someone has had a similar problem before.

I override the draw(Canvas canvas, MapView mapView, boolean shadow) method in an ItemizedOverlay subclass to draw some paths, text etc. between the items. The first thing I do in the override is call super.draw with all these parameters. When I start the app, the lines that I draw in my override are where they are supposed to be, but the actual OverlayItems that the super.draw draws are way off. The interesting thing is that as I move the map towards the upper left corner (coords 0,0) the OverlayItems get nearer and nearer to the place where I expect them to be (in fact, they appear to match perfectly when I get the first item to point (0,0)). As I move the map so that the OverlayItems get further from the (0,0) point, they linearly get further from their geolocation.

In case nobody has faced this problem before and it isn't something obvious, I'll try to make a minimal example that still has this behavior and post it here. The actual code is relatively large so I'd like to avoid that much work if someone has any idea what might be the problem.

edit: an additional piece of information that might be helpful... my draw method looks like this:

public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, shadow);
    if (!shadow) {
        // code for drawing the lines etc.
        // on the canvas (uses mapView.getProjection()) 
    }
}

The custom part of the overlay doesn't have a shadow layer (I've used the same logic when extending Overlay without trouble). The shadow layer that gets drawn for the OverlayItems by super.draw is actually halfway between the Drawable and the expected geolocation for each particular item.

Here are tree screenshots from the avd to better illustrate what is happening. The four red pins should be on the red line above them and actually move towards the line as I slide the map towards the top left. The two pins and the line between them is drawn in my custom override, super.draw draws the four shadows and the four extra red pins.

Since this is my first post and I can't post more than one link, I put all three screenshots on the same jpg.
screenshot

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

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

发布评论

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

评论(2

饭团 2024-09-21 17:13:36

不看代码很难诊断。不过,我会检查一些事情:

  1. 您是否使用 MapView 的投影?是否确保每次调用draw() 时都检索一个新的投影?

  2. 您是否确定为您的 Drawables 提供了绑定方法?例如,boundCenter()boundCenterBottom()

It's hard to diagnose without looking at the code. I would check a few things, though:

  1. Are you using the MapView's projection? Are making sure to you retrieve a new one each time draw() is called?

  2. Did you make sure to a bound method for your Drawables? e.g., boundCenter() or boundCenterBottom()

格子衫的從容 2024-09-21 17:13:36

我已经解决了这个问题,我想我得到的见解可能对其他人有用,所以我将解释这里发生的事情。我不确定回答我自己的问题是否合适 - 如果不合适,请指示我应该将该文本放在哪里,我将相应地编辑/删除。

问题在于我对 Drawables 绘制的基本方式的误解。这个误解来自于阅读 Drawable.draw 的 JavaDoc,其中说道:

在其边界内绘制(通过 setBounds 设置)
尊重可选效果,例如
Alpha(通过 setAlpha 设置)和颜色
过滤器(通过 setColorFilter 设置)。

由于我最初有一个 Overlay 子类绘制了您可以在屏幕截图上看到的路径,并决定使用 ItemizedOverlay 作为该路径上的标记(以便它们可以轻松点击等),我开始稍微改变我的类。在某一时刻,为了查看事情是否按照我预期的方式工作,我添加了一些项目并使用与路径端点之一相同的 Drawable。在 Draw 方法的 JavaDoc 的指导下,我使用类似的方法来绘制路径端点

private void drawMarker(Canvas canvas, Projection projection,
    GeoPoint location, Drawable drawable) {
    Point point = projection.toPixels(location, null);
    drawable.setBounds(
        point.x,
        point.y - drawable.getIntrinsicHeight(),
        point.x + drawable.getIntrinsicWidth(),
        point.y);
    drawable.draw(canvas);
}  

但是,当我跟踪 super.draw 调用时,我注意到它最终调用了 Drawable.draw 但可绘制对象始终具有相同的边界 -我之前调用此 drawMarker 方法时设置的那些。我仍然不太明白 Drawable.draw 如何知道实际将 Drawable 放在哪里,但可以肯定的是,此时我在问题中描述的行为非常明显。事后看来,我可能应该意识到boundCenter和boundCenterBottom实际上定义了Drawable的边界(这是他们的JavaDoc所说的),但我想我认为它只是改变了“最终”确定边界的方式,当您想要绘制的实际点是已知的。

无论如何,我添加了一些额外的功能来使用我实际上想用于 OverlayItems 的可绘制对象,果然,它工作得很好。我仍然想知道我在drawMarker中所做的是否是它应该完成的方式,但我不知道有任何其他方法来告诉画布将Drawable放在哪里。

I have solved this problem and I guess the insight I got might be useful to others, so I'll explain what happened here. I'm not sure if it is appropriate to answer my own question - if not, please instruct me where I should put this text and I will edit/delete accordingly.

The problem was in my misunderstanding of the fundamental way Drawables get drawn. This misunderstanding came about from reading the JavaDoc for Drawable.draw which says:

Draw in its bounds (set via setBounds)
respecting optional effects such as
alpha (set via setAlpha) and color
filter (set via setColorFilter).

Since I originally had an Overlay subclass drawing the path you can see on the screenshot and decided to use ItemizedOverlay for the markers along that path (so that they are easily tappable etc.) I set out to slightly alter my class. At a certain point, to see if things were working the way I had expected, I added some items and used the same Drawable as for one of the path endpoints. Guided by the JavaDoc for the draw method, I used something like this for drawing the path endpoints

private void drawMarker(Canvas canvas, Projection projection,
    GeoPoint location, Drawable drawable) {
    Point point = projection.toPixels(location, null);
    drawable.setBounds(
        point.x,
        point.y - drawable.getIntrinsicHeight(),
        point.x + drawable.getIntrinsicWidth(),
        point.y);
    drawable.draw(canvas);
}  

However, when I traced through the super.draw call, I noticed that it ended up calling Drawable.draw but the drawable always had the same bounds - the ones I set with the previous call to this drawMarker method. I still don't really understand how Drawable.draw knows where to actually put the Drawable, but sure enough, at that point the behavior I described in the question was pretty obvious. In hindsight, I probably should have realized that boundCenter and boundCenterBottom actually define the bounds on a Drawable (which is what their JavaDoc says), but instead I guess I thought it just changed the way that the bounds are determined "eventually", when the actual point you want to draw at is known.

In any case, I added a bit of extra functionality to use the drawables I actually wanted to use for the OverlayItems and sure enough, it works perfectly. I'm still wondering if what I'm doing in drawMarker is the way it is supposed to be done, but I don't know of any other way to tell the canvas where to put the Drawable.

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