I have created sprite with texture size of 2048X1024 and attached to my scene.
Things are going fine but problem is that when i attach any other sprite with scene it takes time to display. Without this larger sprite things are displayed without any note able delay.
As I attache this larger sprite other attachments start getting time to display
I am working on AndEngine
Please help if any one solved this kind of problem
Thanks
I can't figure out why you would need an image of that size on your screen. The problem is that Android limits image sizes to around 1024 x 1024 so AndEngine is most likely swapping out the different images. Without seeing your code or knowing what you are trying to do, there's not much I can suggest. except to break your large image down into images smaller than 1024 x 1024. If you are doing a game and are using the image as a background I'd suggest looking into using a program called "Tiled" and loading it up in AndEndine using its TMXLoader. There is a tutorial on the AndEngine forum for this.
Related
I am stuck showing frame animation in android. My problem is that I have to animate 36 images. The smallest image is 69 kb in size and the biggest(last frame) is 526kb. A total of 11mb. I know its insane but I need to do it. See images below. The app is already made for iphone and I have to port it to android.
Requirements:
smooth animation
no frame loss
consistent with iphone
The solutions I tried are:
AnimationDrawable by providing in xml files. Problems: the start time is almost 6 -12 seconds. Images lose frames so jaggy experience.
Change imageview bitmap inside a handler. Problems: Frame loss and Out of memory
Create a custom imageview class. Define a method showAnimation() and inside it update bitmap and invalidate() the view unitl the last frame. Problems: Not fitting my requirement. This by far gave the best result but not half as required.
I am a noob in opengl and gamedevelopment. I am unable to find any alternative to my problem. Please help.
Smallest image:
Last image:
I can visualize the effect you are aiming at.I guess doing this will require pushing in different images onto the same imageView, with incremental frame rate.|
The accuracy will depend on the number of images you have. Greater the number of images finer will be the precision.
Also you need to implement incremental frame rate. So that it gives a smoother feel.
Doing this should be possible using a simple loop to change the image resource of the imageView Controller. There must be lag between two iterations, this lag will actually be the frame rate.
I think this can give satisfactory results.
Going for openGL, however, you will not need all these images. It will just require one image of the card that will be animated and duplicated to create the final image. This however is a complex piece of task(I think) so if you are catching some dead lines, you must go for a non-openGL solution like the 1st one, I have suggested.
I'm currently trying to create a game for a tablet device for Android using a SurfaceView. I have sprites and what not, but I'm struggling if I should use a whole spritesheet (With every animation in it), or just seperate the sprites on image files.
For example, my spritesheet is named bear.png, which contains 16 images inside. I can animate them beautifully using Rect (Thanks to mybringback for this):
canvas.drawBitmap(bitmap, rectSrc, rectDest, null);
But, I fear doing so would grow to larger than the heap size eventually.
However, there is another method. By seperating each image (bear1.png, bear2.png until bear16.png), I will also be able to create an animation with these sprites via:
Bitmap bitmap = Bitmap.decodeResource(context.getResources(), context.getResources().getIdentifier("bear" + index, "drawable", "com.example.game"));
then calling that to the canvas:
canvas.drawBitmap(bitmap, bitmapX, bitmapY, null);
Which also works fine for now. And of course, it greatly reduces my heap size since I'm only using 1 image at a time. But, because well.. Because, I'd be recalling Bitmap.decodeResource() at almost all times, I fear that when putting many animations together at one time, it may cause a significant lag.
So in short:
- Spritesheets: Increased heap size (which may eventually lead to an OutOfMemory error), but better performance
- Sprites on different image files: Decreased heap size, but may hinder performance
I'm not entirely sure which approach to use. Would it actually be okay if I go with the second method (Sprites on different image files)? Or would it be better with just spritesheets?
Never mind, I just answered my own question. It appears that rendering Bitmap.decodeResource() on every iteration lags the whole canvas, rather than rendering actual spritesheets and using Rect. Performance-wise, if experiencing too much lag on the canvas, there's no other way other than to shrink images using inSampleSize or using GLSurfaceView.
My app is loading a large image (a house floorplan), then drawing touch-reactive objects (furniture, lamps etc.) on the image. I have a base image file included with my app but the objects come from coords in my database.
I've successfully deployed the app in multiple iterations, but now I need to use larger base images and BitmapFactory is causing an OutOfMemory exception on many devices (both old and new devices; anything with < 32MB heap seems to crash). I've read the 157 OOM questions on SO, but I'm afraid the link they all seem to point to won't help me since resolution / zooming is critical to the app's function.
I've tried to test the device's available memory before loading, but the results are spotty at best (some devices like the galaxy S3 report plenty of heap but still crash). I've also tried decreasing resolution but the image became unusable when reduced to a safe size based on the above test.
Is there another way to implement this design without ever using bitmaps?
I need to:
Load large base image
Create clickable shapes on top of the base image, maintaining their position / scale relative to the base image
BONUS: in the iOS version of my app, I can do SVG-style text scaling so a long label on a small object will stay inside the object
instead of running across the map(and will be invisible until the
image is zoomed). Replicating this in android would make me a happy
code monkey.
I can post code if needed, but you've all seen it before (almost all of it came from SO).
Thanks in advance for reading, and for any help you can give.
you have a few options:
break your large image into tiles, load these tiles into an array, and move a camera object around and only load tiles that need to be drawn, as the comments suggest.
make your image small and scale it up using 'android:scaletype`
Draw lines and curves on a Canvas object at runtime.
Use OpenGL
The appropriate solution really depends on how you want it to look. Tiling will take more dev effort but will look better, just be careful that you are properly cleaning up any tiles that aren't being drawn...
dynamically scaling will be easier, but you cannot guarantee the image won't be blurry.
Drawing on a Canvas object at runtime could work well-- just use Lines of different width and circles and Rects etc.
Using OpenGL will have the steepest learning curve, and might be overkill. This depends on your purpose.
You might like to look into using a "largeHeap"
http://developer.android.com/reference/android/R.styleable.html#AndroidManifestApplication_largeHeap
Here are some options:
1) Use tiles. Use tiles and dynamically load your data. Honestly, this is the best solution. With this solution you can load arbitrarily large images.
I've successfully used this approach for an endless paint canvas and it works quite well. You really only need to draw what is directly visible to the user. Tiles is a way to cast away pieces you don't need. A pyramid of tiles (where you pre-downsample your images and create more tiles), allows you to do this in a clean and fast way.
2) Use native code. The memory restrictions on native code are not the same as Java code. You can get away with allocating more memory.
3) Use OpenGL. Once again, the memory restriction for OpenGL are not the same as Java code.
4) Convert your original plan to an SVG and use an SVG library like this one.
5) Use "largeHeap". I strongly discourage this, as I think a largeHeap is rarely the solution, there are generally cleaner ways to approach the problem.
if the image is static , you might wish to use this nice library:
https://github.com/ManuelPeinado/ImageLayout
if the library doesn't support auto-downsampling of the image, you should do it by yourself, in order to use the best image for the current device (so that you won't get OOM).
for auto-sizing text , you might have some luch with the next post:
Auto-fit TextView for Android
I am developing a live wallpaper, in which basically I am rotating around 80 frames (jpg images) - the image size is around 80KB each (800 x 1200 dimension) - total images folder size is around 6 MB.
The images loop fairly smoothly, but as soon as I scale the images (I am doing this to show images well in smaller screens) - the FPS goes for toss - any idea on how to scale the image effectively ?
I thought of two things initially - but it's not working well for me:
I thought of putting all the scaled images in ArrayList of Bitmap, but it's giving a VM Budget error (tried to do bitmap.recycle() too - but it's giving another error like, trying to use recycle image). Any thought on this would be highly appreciated.
I though of putting scaled images in SD card - but again, don't know why but FPS is getting affected - otherwise this can be the best way where I can detect the size of the phone and can download the images over internet and put it in SD card. But, as I said even this didn't work well.
If anyone can help with either of these approaches, or can point to a better way - that should be awesome.
First of all try to sample down the image then scale it . this will reduce your memory size. Secondly store this image and use that image there after. you can find sampling and scaling of imager over here. Let me know if you need more help.
so im trying to make a game with just a simple static background at the moment, but when i draw it to the screen (no scaling being done as the resolution of the image is the same as the screen) it draws the bottom portion of the image incorrectly where the bottom few hundred pixels of the image are exactly the same going down the image. Sorry it's so difficult to explain but being new here i cant actually post an image of what is going wrong.
Now im just using a simple sprite to render this background image. Here is the code being used:
// background layer: another image
background = CCSprite.sprite("WaterBackground.png");
// change the transform anchor point (optional)
background.setPosition(CGPoint.make(GAME_WIDTH/2, GAME_HEIGHT/2));
addChild(background);
am i doing something wrong here? Does Cocos2D not support such large images for sprites? (resolution of 800*1280)
Any help would be greatly appreciated!
Since i am now able to upload images, here are visuals of what is going wrong:
And the problem in my game:
As you can see, the problem is hard to describe. The problem only exists with this larger image; i scaled it down manually in GIMP and then scaled it up for the game and it looked fine (except for being a lower resolution). I also tried scaling down this larger image which still resulted in the same problem. Hopefully you guys can help me as I have no clue what could possibly cause this error. Especially since i read that Cocos2D's max supported image size is 2048*2048 and my image is well within that.
Thanks for any help you guys can provide!
This is due to limitations on the size of textures. Coсos2d-android supports images with a maximum size of 1024 x 1024 pixels.
I faced the same problem and looking for a way to solve it.
EDIT
I found the solution
In cocos2d project open file CCTexture2d.java in org.cocos2d.opengl package and change kMaxTextureSize from 1024 to 2048
I'm not certain, as from your code and looking at the cocos2d code I can't see a definite reason why this would be happening, but given the number of sprites you've got on the screen, i'd definitely take a look at my answer to this question as you may well be hitting one of cocos2d's quirky little rendering faults around multiple sprites. can't hurt to try spritesheets, and it's certainly the correct way to actually do sprites in cocos.
also, definitely create yourself a helper utility to determine the scaling ratio of a device compared to your original image sizes, as unlike on iphone, android does have a pretty much unlimited variation of screen resolutions, and a simple bit of "scale as you load" utility code now will save you lots of time in the future if you want to use this on any other device.
you should try to make one method for adding background,i am doing this this like this way try this hope it will help full for you...here addBackGroundScene is my method and i am adding my background with this method,scrXScaleFactor is maintaining scaling part of my screen try this...
private void addBackGroundScene() {
CCSprite bgForLevel1 = addBackgroundAtOrigin("prelevel1/bgMenu.jpg");
bgForLevel1 .setScaleX(scrXScaleFactor);
bgForLevel1 .setAnchorPoint(0, 0);
addChild(bgForLevel1 );
}