Been working on a custom tile overlay implementation for an android app.
I'm having trouble with threading the image downloads in the background.
Quick overview:
I have a class (MapOverlay) that extends Overlay. The draw method basically calculates the images (bitmaps) needed and the x,y position to draw them on the screen. I then have an AsyncTask that loops through each tiles and download the images if it doesn't exist on the storage card (local caching) then once the AsyncTask has completed I then loop through the images and draw them to the canvas object that is passed into the draw method of the MapOverlay. But the bitmaps are never getting drawn.
I have done some rudimentary debugging by logging the width and the height of the canvas element at the time the AsyncTask completes but it is returning 0 for them so I'm guessing that the canvas object is no longer available to the code running in AsyncTask's onPostExecute() after the draw method has completed
Without the threading I had this working pretty well, except for the "freezing" while panning while the image downloading blocks the UI thread. Now I'm totally suck trying to get threading to work
Have you tried calling postInvalidate() when the AsyncTask done downloading? That way the call in MapView.draw() will get and draw the downloaded overlay (that is if your caching system works).
Related
I'm using the OSMdroid. My goal is adding and drawing items one by one in asychronous way on the overlay. So I'm looking for any way to customized overlay to achive such effect. Any suggestion how to implement overlay?
You can not draw asynchronously - all drawing must be done on the main thread. This is a pretty universal rule for all graphical drawing environments. What you probably want to do is load and create drawing items on a background thread and then pass them to the main thread where they can be drawn during the regular draw cycle.
The android class AsyncTask is designed just for things like this. You will want to load and create the drawing objects in doInBackground() which is run on a background thread and won't lock your GUI. Then you will want to load the drawing objects into the ItemizedIconOverlay in onPostExecute() which runs on the main thread and will also ensure synchronization with the list of drawing objects in ItemizedIconOverlay.
Android experts, I need some help. I'm an Android noob. I have an application where I need to draw a bitmap using canvas from outside the main UI thread. I stumbled onto the SurfaceView class and mocked up a quick demo. I created a seperate bitmap instance and draw into it. When the OnDraw for the surfaceView occurs, I copy this bitmap into the SurfaceView. It works great until the activity ends by, for example, going back to the home screen. At that point, the SurfaceView thread seems to stop, and I seem to lose the bitmap image I was working on. When you return to the app, it starts all over from scratch.
I would like to create the bitmap image using the canvas 2D drawing calls from a thread that is seperate from the main UI thread for performance reasons. I need to get that bitmap into the main user interface once in awhile. And I need the bitmap image to be retained and continue to be drawn into (to keep it current) when the activity is paused or stopped.
Since you must do all drawing in the main UI thread the SurfaceView seems like the only solution. But it doesn't quite work like I hoped either.
I need a background task or thread that NEVER stops running and I need to create a bitmap image there, and I need to get that bitmap into the main user interface once in awhile (say 10 hz updates).
HELP! How do I do this?
Use an asynctask to draw the image onto your ui.
However, the canvas needs to be redrawn when the app is re-opened. If you want to save the bitmap that you created you will need to store it somewhere. I think this can be handled using caching:
view.setDrawingCacheEnabled(true);
Else look into managing the bitmap yourself:
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
So I'm working on developing a game for a school project and so far its gone well, but I have been trying to use a bitmap image instead of just using drawColor for the background, but doing so has make the game sluggish, not respond to to Touch Events, and ultimately crash after a while. Here is my code so far for making that background that keeps crashing, I am using the getColor before the drawBitmap as to blank out the previous background, because without that the moving character threads were leaving a "trail" and not being erased after every movement.
canvas.drawColor(Color.BLACK);
canvas.drawBitmap
(BitmapFactory.decodeResource(getResources(),R.drawable.park),0,0,null);
So any more effective method to make a static background image for the thread to move over would be appreciated!
What you are doing is that you are loading your bitmap everytime you're rendering, which is why it runs slow and crashes after a while because of memory constraints. You should load your image once when you initialize everything:
// run once when you start the game
Bitmap background = BitmapFactory.decodeResource(getResources(),R.drawable.park);
In your render code:
canvas.drawBitmap(background);
And don't forget too free the image when you're done with it:
background.recycle();
The reason your application is slowing down is you are keeping too many references to the same bitmap and this is consuming the amount of memory that you have available, you need to release images as you dont need them. There a bunch of videos by Romain Guy on graphics and game graphics. Look on youtube and you'll learn how to handle this problem.
I have multiple animations on a single activity and each animation is its own surface view and thread. Some of these images are animated around a fixed image, it seems that in the thread of the surface draw function it keeps calling the onDraw() function for as long as the thread is running, this repaints the static image as well as the animation....making it very slow. Is there a way i can get it to only refresh or repaint the animated object?
Yes, but you want to use static types and do not instantiate them, this speeds up things a little. And also I don't think you should have those threads on the separate images... You should render a complete screen on a separate thread. This is mobile developmnet, they have hardware limitations, kepp that in mind! =]
Im currently developing a program that uses a scrollable/zoomable image as the main user interface. It uses a canvas which is manipulated by a matrix to traverse a large area. Instead of loading a super large bitmap into memory, i wanted to employ a tile system to load 256 by 256 squares. My problem is that the app will lag when the images are being loaded into memory. I use a simple outer and inner for loop to load the tiles if they are null, and if the user zooms out to a certain extent the tiles all disappear and a lower res version of the whole image is shown. Ive been steered into the direction of asynchronous image loading, which seems like it would prevent the lag while the image loads into memory but i have no idea how to start this, and was wondering if anyone had any advice on how to generally asynchronously load bitmaps into the canvas. Thanks!
I simply use a second thread to do ANYTHING that could take some time to make sure that the UI is always nice and responsive. You may even want to lower the priority of that thread if the UI contains animation (like scrolling) to get rid of stuttering.
You could use a second Thread object that sends a message via a Handler when it's done, or you can use AsyncTask.