Android Threading - Custom Googlemaps Tile Overlay
I've been stuck for ages now trying to implement threading or background image loading for a custom tile overlay class I've been working on. I'm pretty useless when it comes to Java so threading isn't something I've had any experience with.
Basically I have extended an ItemizedOverlay and in the draw() method calculate the tiles I need to display over the standard google tiles then fetch those images with a URLConnection and using BitmapFactory decode them and draw them to the canvas at the correct position on the screen so that the images are on top of the google tiles with about 50% opacity ( the custom tiles are cellphone network coverage png's which are used for our web app)
I initially got this all working with the tiles loading in the correct places but hit issues with memory and "freezing" while panning so implemented some basic caching and memory management to only store the tiles that are displayed on the map in memory and when downloading a tile store it to SD card.
Then I figured the freezing while panning is due to the blocking nature of downloading the images so, have been trying to implement threading. I created a class that extends AsyncTask to download the images in the background and store them to SD, in the onPostExecute method I then try to draw all the tiles.
But onPostExec开发者_StackOverflowute never seems to fire, now I know that the AsyncTask has to be called in the main ui thread so tried to debug the execution with Thread.currentThread().getId() calls through out the code. The thread Ids through out is 1 and then 10 for the doInBackground method for the AsyncTask. Now I'm not sure if 1 is the main UI thread or not. In the extened MapActivity class I have, the onCreate method's Thread.currentThread().getId() returns 1 but I'm not sure if the MapActivity class is executed in the main ui thread?
Here Romain Guy mentions that the onPostExecute method should have @Override but when I do that eclipse flags it as an error
"The method onPostExecute(Boolean...) of type TileCache.ImageLoadTask must override or implement a supertype method"
So I guess something is wrong there but the only fix eclipse offers is to remove the @Override So I guess with out post all my source code, the question's I have are:
- Is the MapActivity onCreate method called in the main ui thread?
- What does the @Override error mean? or why does it show that error
- Does anyone have an example of a custom tile overlay implementation with background image loading and tile caching?
Thanks
Answers for your first two questions:
- Yes. In fact, unless specified, all function in Activity class are called in UI Thread.
- Your @Override error seems... your ImageLoadTask is not an extend to AsyncTask.
精彩评论