How to make route drawing more efficient?

限于喜欢 提交于 2019-11-30 10:12:38

You can draw the path to a transparent Bitmap object (whatever size you see fitting - the bigger it is, the better the detail of the path yet higher memory consumption).

Make sure you create it with Bitmap.config.ARGB_8888 for transparency.

Once you've done this, you'll be using two rectangles to display the path on the Overlay:

  • A source rectangle to determine which part of the path is visible
  • A destination rectangle to determine where you want to display this piece of the path on the Overlay's canvas.

You'll be using Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

Shouldn't be too difficult, you've done most of the important calculations in your transformPath method.

Added:

You can actually do a combination of both holding a path drawn to a Bitmap and redrawing the actual path points. Use the technique described above for when the user moves around the map or zooms in/out then redraw the path when the user lets go of the screen.

Best way to increase speed of drawing is reducing number of point the path includes. Probably they are not necessary - lot of them just lays between previous and next, so you can filter them by:

minimal distance from previous point (easy way)

minimal bearing change (a bit harder, although Location class and it's method bearingTo() should help.

  1. You should know that draw() method draws one draw cycle about 50 times. I do not know why, but you can test it. This slows down the performance. And if you have to draw 1000 object and draw() draws them 30-50 each...it is getting very very clumsy.. To avoid this, you should create a cache overlay.This cache will draw all objects only once and will reject the other draws.

  2. To speed up the process, draw in background thread with low priority.

  3. The fastest way to draw a line between multiple points is to use the method Canvas.drawLines(float[] pts, int offset, int count, Paint paint). I have tested all methods and this is the fastest method that android offers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!