Automatic density resize

I’ve started doing some tests with the 2D API of Android, and to start with something simple, I have an animated sprite walking on platforms at different heights.
To avoid unnecessary complications, I just added the images in the folder drawable-mdpi (which corresponds to my device).
To make sure that the sprite has its feet on the platforms, I coded in the program a coordinate in pixels corresponding to the average position of the feet of the sprite images.


Android does not require you to include images for all possible densities, an image for one density is enough, but when you use the application on a device with a different density, Android automatically scales the image to fit the target density. And if you provide different image sizes for different densities, the problem is the same, your pixel coordinates will only fit one density.
This means that math in a game should not use absolute pixel measures, because the results will be incorrect. Android adapts its own calculations when you use images in layouts, but does little for a game developer that draw on the canvas freely.

My solution?

I code normalized coordinates in sprite space. This is slower, as implies floating point multiplications to get the final pixel integer values, but I don’t know a better solution to this problem (if you know it, you are welcome to share).


Acerca de Rubén L.

Software Engineer
Esta entrada fue publicada en android, English y etiquetada , . Guarda el enlace permanente.


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s