Animation of 2048 Game in android - 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 .

Related

Not overriding draw

Okay so far when I have used SurfaceView I would override the draw method and then call it from a seperate thread. However, recently I tried to not override it but simply make all my canvas drawing calls in a custom method, and it worked. I would just do all my drawing the same way but instead of all the code being in overridden draw method, I would just put it all in another method and it worked anyways. So whats the point of overriding anything? I honestly still don't know how all the drawing works behind the scenes as its not explained...
For SurfaceView as you noticed yourself it makes no difference. There's no point of overriding draw and then calling it yourself from the separate thread.
The SurfaceView only have the method there because it inherits it from the View class, but because you're, in a separate thread, acquiring the lock to the canvas, drawing and then releasing, it doesn't matter if you pass it to the draw method, if you use directly on the thread run or some other Runnable
Maybe one could argue that it's more organised (and do never underestimate the importance of a well organised code), but it's not a necessity.

android canvas onDraw timing

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.

game engine, how to organize animation and etc. in android?

I'm quite new to android and i want to make some animation (simple game later) like i did on windows (all programatically).
So I have questions what classes should be responsible for.
Activity
It's a base class for whole logic like updating variables (computing move etc.)
Should I create a new Thread on onCreate and run it onResume ? (I need continuous computing for moving objects)
View
Only for drawing animation
onDraw method for clearing Canvas (how to do this quickly? drawRect is efficent ? ) and drawing objects.
Should I invalidate that view or just call onDraw 30 times per second?
Regards
razor
Take a look at this http://developer.android.com/resources/samples/AccelerometerPlay/index.html

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.

The proper way to create a 2d android game

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.

Categories

Resources