Android 中更高效的地图叠加

发布于 2024-08-09 16:12:40 字数 678 浏览 4 评论 0 原文

在我的应用程序中,我在 地图视图。这些路线有十几个到几百个 GPS 坐标来描述公交车的路线。

Hosted by imgur.com

我遇到的问题是,一旦我画出所有这些线,就会平移/缩放 MapView 非常慢(即使单击“后退”按钮也需要一分钟的时间)。

我不确定它有多相关,但我输入了一些调试代码,然后检查了 logcat 输出,并且 MapView 重复调用 draw() 方法a href="http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/Overlay.html" rel="nofollow noreferrer">叠加层是否有任何改变。这种情况每秒发生几次,并导致发生大量垃圾收集(每秒 2-3 MB)。

有人对尝试加快速度的方法有任何想法/建议吗?

In my app I am drawing bus routes on top of a MapView. The routes have anywhere between a dozen and a few hundred GPS coordinates that describe the route that the bus takes.

Hosted by imgur.com

The problem I'm having is that once I draw out all these lines panning/zooming the MapView is incredibly slow (even clicking the 'Back' button takes a minute to happen).

I'm not sure how relevant it is, but I put in some debug code then checked the logcat output and the MapView is repeatedly calling the draw() method of the Overlay whether anything has changed or not. This is happening several times a second and is causing a massive amount of garbage collection to happen (2-3 MB every second).

Does anyone have any ideas/suggestions for a method to try and speed this up?

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

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

发布评论

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

评论(2

说不完的你爱 2024-08-16 16:12:40

我只使用了 ItemizedOverlay,而不是 Overlay,所以这些建议纯粹是猜测。哎呀,我什至没有对 2D 图形 API 做太多事情。

显然,最好的答案是让它停止一直调用draw()。尝试记录一些堆栈跟踪,看看是否可以找出触发所有 draw() 调用的原因。例如,最近在 Android Google 群组中,有人注意到 Chronometer 会导致同一 UI 中的小部件每秒重新绘制一次。虽然我发现您没有 Chronometer,但您也许能够找出 draw() 调用的一些根本原因并进行更正。

假设这没有帮助,我猜测“是否有任何改变”的测试是 getLatitudeSpan()getLongitudeSpan() 的某种组合getZoomLevel(),也许还有其他 MapView 方法。而且,我假设在每个 draw() 中,您都会迭代 GPS 点并绘制路线。如果是这样,您可以尝试:

  1. 当您真正进行绘制时,首先绘制到由 Bitmap 支持的 Canvas,然后将 Bitmap 应用到您将收到 draw() 中的 Canvas,并缓存该 Bitmap
  2. 跟踪上一个 draw() 中使用了哪些值组合,如果下一个 draw() 相同,则只需重用现有的 Bitmap >。否则,请转到步骤#1,确保释放位图(或重用它,如果可能的话)。

我猜测,通过图形加速,将位图喷到画布上比迭代坐标和绘制线条更便宜。而且,通过缓存位图,您将减少垃圾生成。

无论如何,只是一个想法。

I have only used ItemizedOverlay, not Overlay, so these suggestions are pure conjecture. Heck, I haven't even done much with the 2D graphics API.

Obviously, the best answer is to get it to stop calling draw() all the time. Try logging some stack traces and see if you can figure out what is triggering all of the draw() calls. For example, in the Android Google Groups recently, somebody noticed that Chronometer causes widgets in the same UI to be redrawn every second. While I can see you don't have a Chronometer, you might be able to figure out some root cause to the draw() calls that you can correct.

Assuming that does not help, I am guessing that the test for "whether anything has changed or not" is some combination of getLatitudeSpan(), getLongitudeSpan(), getZoomLevel(), and maybe other MapView methods. And, I am assuming that on every draw() you are iterating over your GPS points and drawing the route. If so, you could try:

  1. When you really do draw, draw first to a Canvas backed by a Bitmap, then apply the Bitmap on the Canvas you are handed in draw(), and cache that Bitmap.
  2. Track what combination of values were used in the last draw(), and if the next draw() is the same, just reuse the existing Bitmap. Else, go to step #1, making sure to release the Bitmap (or reuse it, if that's possible).

I am guessing that with graphics acceleration, blasting a Bitmap onto the Canvas is cheaper than iterating over the coordinates and drawing lines. And, by caching the Bitmap, you will save on garbage generation.

Anyway, just a thought.

ぃ双果 2024-08-16 16:12:40

Overlay类中有两个draw方法。一种有 3 个参数,一种有 4 个参数。您必须使用 3 个参数重写绘制方法。
使用 4 个参数覆盖该方法会减慢您的应用程序的速度。这正是发生在我身上的事情。看来,互联网上哪里有同样错误的例子。

There are two draw methods in the overlay class. One with 3 arguments and one with 4 arguments. You have to override the draw method with 3 arguments.
Overriding the method with 4 arguments will slow down your application. This is exactly, what happened to me. It seems, where are examples around in the internet with the same error.

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