android canvas onDraw timing - android

I am making an Android game in which some graphical elements move fast.
I am going to use Canvas but fear that the onDraw method will be called at irregular interevals making the fast elements move at irregular speeds.
Is there a way to make sure that the onDraw method is called at regular intervals.
Alternately if I can get the system time in msec I can take the difference between two consecutive calls to onDraw and based on the speed of these fast elements make the their movement smooth.
Kindly help,
Sameer

I am going to use Canvas but fear that the onDraw method will be called at irregular interevals making the fast elements move at irregular speeds
You should not update the position of objects in onDraw(), because:
Indeed, the method might not be called at predefined intervals
It is meant to draw objects, not update objects
Instead, update the objects/world in another method that you call at regular intervals using a timer.
After updating the world, call invalidate() on your View to trigger the onDraw() method. If your application has some spare time it will be called. If not, no problem since the world update method will keep on running and thus updating your world.
(Of course, if your application is too busy the view is never updated and you have problems to solve!)
Alternately if I can get the system time in msec I can take the difference between two consecutive calls to onDraw and based on the speed of these fast elements make the their movement smooth.
This is the way to go in the 'world update method'!
It will correct for the irregular timer intervals.

Related

Animation of 2048 Game in android

I have made a "2048 game" with java in Eclipse . All things is good and work correctly, even the animation of cells that move in 4 directions.
But the problem is, that the animation does not work fine & smooth, I think because of heavy processing load.
I have made a class that extends ImageView. and I have overridden the
onDraw() method. Then in another method and in a thread, I call the postInvalidate() method frequently for the animation (so the onDraw() method is called frequently).
My algorithm in the onDraw() method is fine and my animation runs correctly but I think using this way (using ImageView class and calling onDraw() method frequently) is not an accurate way, because the onDraw() method redraws everything unnecessarily. Some things in this game are fix and do not need to be redrawn, so this way the process is heavy and I don't like this.
Is there another way for this game, than drawing the page frequently for animation without jumping? thank's for your answer .

How fast invalidate refreshers the canvas?

if i call invlidate() at the end of onDraw() function in a View, at what rate does the system refreshers?.
In emulator it takes around 1 second to call the method.
What are you using the canvas for? If you're doing a game or something that needs to be refreshed constantly you should really use a SurfaceView. That will take care of calling the draw method at regular intervals without you having to call invalidate. That said the refresh rate of a SurfaceView really depends on what you're drawing. If all you're doing is clearing it then it can do many hundreds of frames per second.

How to animate in Android Canvas?

I want to draw something at about 30 frames per seconds on Android Canvas or other convenient object for this purpose. In my application different graphic objects are drawn and if any of the graphic object is touched, the graphic object changes its shape. I looked at the
onDraw(Canvas canvas) callback of View subclass but calling invalidate() does not help here: first I cannot control the frame rate and second if the objects are moving too fast, the motion appears jerky.
I personally dislike Android's built-in Animation classes, so I tend to do all animations with Canvas by hand. I have found the most luck with creating a list of the images you want to use in your animation and then an int variable to store the current "frame" you are on. To advance the frame, I create a thread that sleeps for, say, 30 ms and then update the frame variable accordingly. Then in whatever update handler you are using, you can just create a switch statement or something of the like, and draw the respective frame.
It may seem like a lot of work, but it really isn't. Shove it all into a class and you will love yourself for many animations to come.

How should I call the onDraw() method of an Android view 30 times per second

For Android, I have a custom view which I fill up with primitive shapes in the onDraw() method.
Coming from a Processing background, I expected the draw method to be called automatically 30 times per second, but its clear that that's not how android views work.
So how should I go about calling this method 30 times per second?
Use an Animation, and call startAnimation() on it from your View.
I don't know that you can set a target framerate -- rather, you're expected to set start and end points in time, and be able to interpolate for any point in time between the two.
If you don't like this approach, you might consider having another thread which periodically calls view.postInvalidate() to request that your View be redrawn.

More efficient map overlays in Android

In my app I am drawing bus routes on top of a MapView. The routes have anywhere between a dozen and a few hundred GPS coordinates that describe the route that the bus takes.
The problem I'm having is that once I draw out all these lines panning/zooming the MapView is incredibly slow (even clicking the 'Back' button takes a minute to happen).
I'm not sure how relevant it is, but I put in some debug code then checked the logcat output and the MapView is repeatedly calling the draw() method of the Overlay whether anything has changed or not. This is happening several times a second and is causing a massive amount of garbage collection to happen (2-3 MB every second).
Does anyone have any ideas/suggestions for a method to try and speed this up?
I have only used ItemizedOverlay, not Overlay, so these suggestions are pure conjecture. Heck, I haven't even done much with the 2D graphics API.
Obviously, the best answer is to get it to stop calling draw() all the time. Try logging some stack traces and see if you can figure out what is triggering all of the draw() calls. For example, in the Android Google Groups recently, somebody noticed that Chronometer causes widgets in the same UI to be redrawn every second. While I can see you don't have a Chronometer, you might be able to figure out some root cause to the draw() calls that you can correct.
Assuming that does not help, I am guessing that the test for "whether anything has changed or not" is some combination of getLatitudeSpan(), getLongitudeSpan(), getZoomLevel(), and maybe other MapView methods. And, I am assuming that on every draw() you are iterating over your GPS points and drawing the route. If so, you could try:
When you really do draw, draw first to a Canvas backed by a Bitmap, then apply the Bitmap on the Canvas you are handed in draw(), and cache that Bitmap.
Track what combination of values were used in the last draw(), and if the next draw() is the same, just reuse the existing Bitmap. Else, go to step #1, making sure to release the Bitmap (or reuse it, if that's possible).
I am guessing that with graphics acceleration, blasting a Bitmap onto the Canvas is cheaper than iterating over the coordinates and drawing lines. And, by caching the Bitmap, you will save on garbage generation.
Anyway, just a thought.
There are two draw methods in the overlay class. One with 3 arguments and one with 4 arguments. You have to override the draw method with 3 arguments.
Overriding the method with 4 arguments will slow down your application. This is exactly, what happened to me. It seems, where are examples around in the internet with the same error.

Categories

Resources