I was looking for information on how to implement an animation to my android application in the most efficient way (without out-of-memory error).
Let's say I have the same animation for smoke as this app does:
https://play.google.com/store/apps/details?id=com.apps4you.virtualsmoking
(The smoke effects)
And I made these with png's frame by frame animation in photoshop.
(150 frames);
When I add these to my app (using animation-list), I get a memory error.
I'm wondering how he added this animation without a memory crash error.
There can be many solutions to the problem:
1)Use canvas
2)You can even use GLSurfaceview
3)Use some game engine like Cocos2d-x,libgdx,unity or rajawali
Related
I m trying to track after the screen rendering in Android for move a simple Object on screen but with no luck.
For Example: The background and the bars in flappy bird game, How does they "move" and the bird is flying like?!
I m trying to avoid from using OpenGL.
Any tutorial? Any Tip/Trick?
In iOS it's super simple, In android is it the same?
Thanks in advance!
If you are trying to build a game (especialy if you build it for android and IOS) and need to use some tweening go and use platform such as
Corona:
http://coronalabs.com/products/corona-sdk/
Unity:
http://unity3d.com/
you could use openGL but this is a lot of power for some little app...
if you just want animations you could try to use the android animations:
http://developer.android.com/training/animation/index.html
also a nice tutorial for animations and tweening:
http://code.tutsplus.com/tutorials/android-sdk-creating-a-simple-tween-animation--mobile-14898
or extend the view class and override onDraw, this way you could draw stuff on a canvas and even create your own animations by calling invalidate() again.
all depend on your needs.
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 am developing a game in android with Cocos2d framework with latest build from github(Weikuan Zhou).
In my game, I used lots of images(total images size is around 11MB).
Problem:
I am getting the black box instead of images when I play my game more than 3 times.
What steps will reproduce the problem?
1. When I play my game more than 3 times via "Play Again" functionality of my game.
What is the expected output? What do you see instead?
- images should be displayed properly instead of "BLACK BOX".
and in my logcat, I see the Heap memory goes around 13Mb.
I already release Texture via below method
CCTextureCache.sharedTextureCache().removeAllTextures();
I also tried to remove sprite manually ex. removeChild() method.
But so far not succeeding to find any solution.
If any one have solution for this please let me know.
From what you're describing, you're hitting exactly the same problem i did, which is that cocos2d for android is really buggy when dealing with lots of single sprites loaded individually.
The best route to take to resolve this problem is to get hold of (unless you've got a mac) the free flash version of zwoptex from here http://zwopple.com/zwoptex/static/downloads/zwoptex-flashversion.zip
this will let you build up spritesheets, i suggest relating as many sprites as you can onto each sheet, whilst trying to keep them sensibly grouped.
This is mainly down to cocos doing a single render for ALL sprites in a spritesheet, rather than one render per sprite for normal sprites, hence massively reduced processing time, and much less memory usage.
you can then load the spritesheet with code such as (can't guarantee this code will execute as i'm grabbing snippets from a structured project but it will lead you to the right solution)
CCSpriteFrameCache.sharedSpriteFrameCache().addSpriteFrames("menus.plist"); // loads the spritesheet into the frame cache
CCSpriteSheet menuSpriteSheet = CCSpriteSheet.spriteSheet("menus.png", 20); // loads the spritesheet from the cache ready for use
.... // menu is a CCLayer
CCSprite sprite = CCSprite.sprite(CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName("name of sprite from inside spritesheet.png"));
menuSpriteSheet.addChild(sprite, 1, 1); / you add the sprite to its spritesheet
menu.addChild(menuSpriteSheet, 1); // then add the spritesheet to the layer
This problem happens when you load resources at run time. So it is better to load resources before the scene starts.You can do as follows.
Using the sprite sheets for resources in your game.
Implement your Ui in constructor of your class.
Implement your functionality in overridden method onEnter() in your layer.
You must unload your sprite sheets after finishing your scene.
These is the procedure that I am following.
Thanks.
I am making the classic AR app for Android.
I am using a SurfaceView to show the camera.
I want to know, how I put things over it... Currently I am using widgets, but they have two issues:
First, they are slow.
Second, you cannot rotate them in 1.5 without using view animation, and noone I asked so far (here, forums, google, irc...) knows how view animation work, so I am failing to make the view animation work in sync with the custom view that put widgets over the camera (resulting in flickering).
Thus I decided to change the method of putting things on the screen, so what other ways exist? (note I am bound to Android 1.5)
Seemly it cannot be done easily, but I could put a image and rotate it easily using Matrix.
I basically placed on layout over the preview a ImageView, then I used setMatrix to a rotation matrix, and voilá, I got a rotated image that works in 1.5
You must use OpenGL for drawing Objects over your SurfaceView.
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.