Adding and drawing items in asynchronous way on overlay? - android

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.

Related

Optimize onDraw() in custom complicated view

I have icons which are represented as View's and all drawing are fully calculating in onDraw method with 'invalidation' on every frame (https://github.com/torryharris/Skycons). I have no ability to get these icons in gif format or other so I need to optimize their drawing because after like 30 second I get 'XXX skiped frame' message in logcat and frozen ui. I know that in OpenGL you could make calculating in background thread and then just swap drawing's buffers so animation will be smooth and nice. Can I do something similiar in Android or has Android some tricks for optimizing drawing views?
The solution is to use SurfaceView with background thread.

Surface view android Threads

Do I need to create a Thread to handle the drawing on SurfaceView, or it is automatically created by Android? I am drawing just some circles, without moving.
Thank you in advance.
You don't need to create a thread unless you really want to create one thread ( if you are building a simple game of some kind, for the game loop you may have to create a Thread which calls the onDraw() method forever :) )
find More about game loops in this tutorial

Using ananimation at a random location on custom view

Hi, In my app, I'm trying to make an explosion animation occur, wherever an enemy is destroyed on my custom view. What is the best way to accomplish this, because I know you can't draw an imageView onto a canvas using onDraw? How do I get the animation to play at the location of the enemy?
You could first create a list of exploding and show them one after the other using a thread and handler. For example, if you created 10 images, namely explosion-1.png, explosion-2.png...explosion-10.png then, using canvas.drawBitmap, you could make them appear one after the other until the sequence finishes using a loop.

Android Surface View update

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! =]

Android Google maps - custom tile overlay

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).

Categories

Resources