java.lang.Object | |
↳ | com.neomades.graphics.Image |
Image represents an image data.
The Image class handles all the resources transformed into Images that will be used to display graphic elements on screen.
Here again, the Images are natively transformed to be compliant with OpenGLES i.e. they are transformed into textures respecting OpenGLES constraints.
First of all, there is one thing to understand clearly. To create OpenGLES textures, it is needed to use a temporary standard Android Bitmap from which the texture will be created. This means that every Image creation will use temporarily some standard heap bitmap memory.
This memory allocation can be temporary only if the Image comes from a resource loaded from the apk. If the Image is created with methods like createImage(w, h) or any other Image creator building the Image from something else than a resource, then the temporary bitmap will have to stay in memory until the Image is destroyed. The explanation is simple. With the Android technology, in the event of a suspend/ resume (i.e. incoming call or equivalent), it is needed to reload all the textures contained in the OpenGLES memory. OpenGLES might have trashed them during the Suspend/ Resume. Therefore, in order to reload those textures, there must be a way to reload them on the fly. For Images coming from resources, we keep the resource name within the Image object in order to reload it on the fly when the Suspend/Resume is over. For Images coming from other objects, we do not have any other choice than to keep the original bitmap used to create the Texture in memory to be able to re-upload them into OpenGLES memory.
The temporary memory is taken from the normal Android Bitmap heap memory. Be careful that this memory is limited (and device dependent). This has 2 implications:
With this new Image class, it is important to destroy the images explicitly and not set them simply to null. It is needed to explicitly call the destroy method to free the OpenGLES memory used by the Image.
The best practice in terms of performances for OpenGLES is to try to avoid texture swapping. Texture swapping is when you change the current texture being drawn from one texture to another. As a consequence, if possible, it is a good idea to regroup multiple images into single bigger one (typically creating an image atlas containing a large number of small images and use the drawRegion method from the Graphics class to display them. The best is to create large Images sized 1024x1024 containing a large number of small pictures inside and sort them so they are grouped by drawing calls.
Example:
If your code displays:
Background 2
Background 1
Ground
Foreground
Hero
Enemies
Shots
HUD
Then try to regroup your images into various 1024x1024 images but minimizing the texture swapping by grouping them according to the order of the drawing calls.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static Image |
createImage(InputStream stream)
Creates an image based on an InputStream.
| ||||||||||
int |
getHeight()
Returns the height of the image
| ||||||||||
int |
getHeightDP()
Returns the image height in dp.
| ||||||||||
int |
getWidth()
Returns the width of the image
| ||||||||||
int |
getWidthDP()
Returns the image width in dp.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Creates an image based on an InputStream.
stream | the data stream |
---|
NullPointerException | if the input stream is null |
---|---|
IllegalArgumentException | if input stream is incorrectly formatted or otherwise cannot be decoded |
Returns the height of the image
Returns the image height in dp.
Returns the width of the image
Returns the image width in dp.