I normally load my spritesheet bundled with my app this way:
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile( "myspritesheet.plist");
CCSprite *pSprite = CCSprite::createWithSpriteFrame( CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("sprite_monster.png");
pSprite->setPosition(ccp(100.0f,100.0f));
this->addChild(pSprite);
However, as the number of textures are growing and to accomodate easier updates, I am planning to place some of the spritesheets to the server and download it to the sdcard (or Cache folder in iOS). However, I don't see ways of loading it in game.
This link show how to load the png texture. But if I have multiple sprite packed into a single texture, I'll need to load the .plist file and feed it into CCSpriteFrameCache. Anybody knows how to do that? or any other solution how to load the individual CCSprite from the texture that has multiple sprites?
Thanks in advance!
try this code:
CCSpriteBatchNode *batchNode = CCSpriteBatchNode::create("myspritesheet.png");
this->addChild(batchNode);
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("myspritesheet.plist");
CCSprite *pSprite = CCSprite::createWithSpriteFrameName("sprite_monster.png");
pSprite->setPosition(ccp(100.0f,100.0f));
this->addChild(pSprite);
Related
I want to upload an internal png image to my backend, the API supplied with the backend only allows for byte[] data to be uploaded.
But so far, I haven't found a way of extracting byte[] data from a texture. If it's an internal resource or not, I'm not sure matters?
So what ways are there to achieve this using Libgdx framework?
The image I want to use is loaded with the AssetManager.
Before trying to do this, make sure to understand the following:
A Texture is an OpenGL resource which resides in video memory (VRAM). The texture data itself is not (necessarily) available in RAM. So you can not access it directly. Transferring that data from VRAM to RAM is comparable to taking a screenshot. In general it is something you want to avoid.
However, if you load the image using AssetManager then you are loading it from file and thus have the data available in RAM already. In that case it is not called a Texture but a Pixmap instead. To get the data from the pixmap goes like this:
Pixmap pixmap = new Pixmap(Gdx.files.internal(filename));
ByteBuffer nativeData = pixmap.getPixels();
byte[] managedData = new byte[nativeData.remaining()];
nativeData.get(managedData);
pixmap.dispose();
Note that you can load the Pixmap using AssetManager as well (in that case you would unload instead of dispose it). The nativeData contains the raw memory, most API's can use that also, so check if you can use that directly. Otherwise you can use the managedData managed byte array.
I want to create grid view using JSON.JSON contain image urls.I am reading number of tutorials but i not getting what am want.JSON contain many images(more than 100).please help me how can i solve this issu
I can recommend a different way that works like a charm: Android Query.
You can download jar from here
AQuery androidAQuery = new AQuery(this);
As an example:
androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true); // true, true means image caching in file and in sdcard, if you want to show images fast then keep this true.
It's very fast and accurate, and using this you can find many more features like animation when loading, getting a bitmap (if needed), etc.
Shouldn't this work?
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/texture.png"));
AtlasRegion region = atlas.findRegion("ape_glad");
Sprite ape= new Sprite(region);
Instead I get: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: data/texture.png at the first line above O.o
Thanks for helping!
First, you need to create a texture atlas, using TexturePacker is the recommended way for libgdx. It results in the texture image and another file (containing the required information for libgdx TextureAtlas).
In your code, you need to provide the atlas file to the constructor, see TextureAtlas() documentation, instead of the image itself:
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/texture.atlas"));
(Notice the use of the 'atlas'-file instead of the image file)
I'm working on a project that needs to use a large image as a map. The image is about 95MB and has a resolution of 12100 x 8000 pixels.
I don't need the whole image at once, I just need a detail of 1000 x 1000 Pixel (it's not always the same detail, just grabbing the same part is not a solution I can use). So I can't just sample it down with the BitmapOptions.
I looked around and found the idea to create a FileInputStream (the image is on the SD-Card) and then I can just load the detail with decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts). That way I wouldn't load the whole thing into the memory. I tried it, but it's just crashing when I try to load the image. Here's my code:
FileInputStream stream = null;
try {
stream = new FileInputStream(path);
} catch(Exception e) {
Log.e("inputstream",e.toString());
}
Rect rect = new Rect(a,b,c,d);
return BitmapFactory.decodeStream(stream, rect, null);
When I try to load the image, the activity closes and LogCat tells me java.lang.outOfMemoryError. Why does it crash? I thought with the stream it should work on the image "on-the-fly", but the only explication I have for the Error is the it trys to load the hole image into the memory. Does anybody have an idea how I can load the detail out of the image, or why this idea doesn't work?
It crashed because all these 95M are sucked into memory for processing. This call will not ignore parts of the stream - it will put the whole thing to memory and then try to manipulate it. The only solution you can have is to have some sort of server side code that does the same sort of manipulation or if you don't want to do it on server - provide thumbnails of your large image. And I would strongly advise against pulling whole 95M at any time anyways
Does BitmapRegionDecoder not help (I realise its level 10)?
In a typical as3 or flex project, after loading xml file, i load jpg files (thumbnails etc) manually, so i can use them in sprites / movie clips etc..
currently i am working on air mobile project. and i am attempting to load some thumnails(jpg) file to list view (spark) and using custom item renderer.
itemrenderer has an spark image component in it. and its data property is set to Image object.
i can check that image files do exists in file application directory.
do i need to load all those thumbnails in memory. then use them?
image object will autoload source file object?? once assigned?
do i have to tell it explicitly to load file object? what events should i use to amke sure , image file object is loaded.?
any ideas?
thanks in advance.
Spark images are really easy to work with. All you need is a url.
<s:Image source="http://someimagesite.com/someimage.png" width="100%" height="100%" />
You can also use bitmap data or even embed directly into the source tag.
[Embed(source="image.png")] private var myImage:Class
mySparkImage.source = new myImage() as BitmapData;
<s:Image source="#Embed('image.png')" />
Take a look:
http://help.adobe.com/en_US/flex/using/WSc5cd04c102ae3e97-33ad5caa12c719dc7c8-8000.html