在我的应用程序中,我在 地图视图。这些路线有十几个到几百个 GPS 坐标来描述公交车的路线。
我遇到的问题是,一旦我画出所有这些线,就会平移/缩放 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.
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?
发布评论
评论(2)
我只使用了
ItemizedOverlay
,而不是Overlay
,所以这些建议纯粹是猜测。哎呀,我什至没有对 2D 图形 API 做太多事情。显然,最好的答案是让它停止一直调用
draw()
。尝试记录一些堆栈跟踪,看看是否可以找出触发所有draw()
调用的原因。例如,最近在 Android Google 群组中,有人注意到Chronometer
会导致同一 UI 中的小部件每秒重新绘制一次。虽然我发现您没有Chronometer
,但您也许能够找出draw()
调用的一些根本原因并进行更正。假设这没有帮助,我猜测“是否有任何改变”的测试是
getLatitudeSpan()
、getLongitudeSpan()
、的某种组合getZoomLevel()
,也许还有其他MapView
方法。而且,我假设在每个draw()
中,您都会迭代 GPS 点并绘制路线。如果是这样,您可以尝试:Bitmap
支持的Canvas
,然后将Bitmap
应用到您将收到draw()
中的Canvas
,并缓存该Bitmap
。draw()
中使用了哪些值组合,如果下一个draw()
相同,则只需重用现有的Bitmap
>。否则,请转到步骤#1,确保释放位图(或重用它,如果可能的话)。我猜测,通过图形加速,将位图喷到画布上比迭代坐标和绘制线条更便宜。而且,通过缓存位图,您将减少垃圾生成。
无论如何,只是一个想法。
I have only used
ItemizedOverlay
, notOverlay
, 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 thedraw()
calls. For example, in the Android Google Groups recently, somebody noticed thatChronometer
causes widgets in the same UI to be redrawn every second. While I can see you don't have aChronometer
, you might be able to figure out some root cause to thedraw()
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 otherMapView
methods. And, I am assuming that on everydraw()
you are iterating over your GPS points and drawing the route. If so, you could try:Canvas
backed by aBitmap
, then apply theBitmap
on theCanvas
you are handed indraw()
, and cache thatBitmap
.draw()
, and if the nextdraw()
is the same, just reuse the existingBitmap
. 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 theCanvas
is cheaper than iterating over the coordinates and drawing lines. And, by caching theBitmap
, you will save on garbage generation.Anyway, just a thought.
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.