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
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.
i make an app which would fire different small animation, such a falling rotating star or a like a text floating from bottom to top, animated with canvas on a surfaceview
they are all constant once fired,
so i planned 2 ways for that, 1 is a big thread which
handles them all animations, updates each after each other animation 1 after 1
or having for each kind of animation such as a falling star, have it's own thread set up, and at each given point the main activity could have a command go on one of the threads, creating him, and starting, and than forgetting about him, letting him run in the background and at the end of the animation that thread will end it self.
so i don't have to worry about storing each fired threads and such, cause i don't know how many could start at a given time.
so will it be more memory efficient to fire random small short threads or have 1 big thread to handle it all? will it work more smoothly and better?
the animations are simply for visual enchantment and does nothing else, neither does it interact with other animations it simply starts, loops, ends.
i hope u understood :\
You probably want a SurfaceView with one thread handling all of your drawing. Having one thread per animation seems overly-complicated. You can certainly have one method per animation type, e.g. renderStars(), renderText(), etc. that are called whenever your one animation thread renders.
Recommended reading:
The SurfaceView documentation
The Canvas and Drawables API Guide - I've linked to the "On a SurfaceView" section, but you should probably read most of the document.
The Lunar Lander sample app - The online code is currently 404-ing after Google recently revamped their developer site, but this link shows you where to find the code in the SDK manager.
During animation you will have to update your UI on UI Thread you cannot use a different thread for running animations. However, for intensive animation you must use a Gaming Engine or write your own Game Thread, that will first update data of objects as per animation formulas and afterwards draw them on UI thread.
I am working on an android canvas game and have noticed a lagspike just as the main game form is being drawn. It started just after I started sprinkling synchronized(surfaceHandler) around different parts as an attempt to battle concurrent array modification. Did I over synchronize? What is the rule of thumb on when to synchronize and when not to, and when should visible data be modified?
Also is it possible that the spike is caused by something else? The draw method executes while this is happening, but no touch interaction is possible (most, if not all, of the touch method is synchronized).
Advice?
I think that as long as you are generated a canvas from the surface holder in one area, you shouldn't have to synchronize. This may or may not be causing the lagspike. If you are using surfaceHandler in multiple threads, you may want to condense all your drawing into one thread or you will need to keep the synchronize.
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! =]
After getting help with starting android pong, I realized that I painted myself into a corner.
For reference on the code(Its long) and advice, here is a link to the last thread Updating player score in an android game
Basically the frame rate is halved when i touch the screen. I think it is due to updating everything or drawing on the canvas. This also lends itself to bad communication between the pieces of the application(the view, frame, and oncreate). Is there a recommended way to create an android 2d game? This is based off the lunar landing game and it seemed fine but when i tried to recreate it , it seemed much worse. I could not find a lot of resource on a best practice for drawing and so far this seems a bit discouraging.
So in short
How would you modify the code posted in the link to improve performance? Or did I paint myself into a poorly made corner, and it is time to start a new? If so , how?
Thanks
You don't need invalidate() in onTouch because you are already running a frame animation by the additional thread.
Try to define all the fixed values/variables in the constructor of the SurfaceView or in onSizeChanged() where you know the size of the canvas and bitmaps loaded, so not to make any unneccesary variable creation, requests or calcualtions in onDraw, onTouch or other methods to handle game logic, otherwise it will slow down.