Answer the question
In order to leave comments, you need to log in
libgdx + android + Multiple Screens
Hello!
Perhaps there are people on Habré who use libgdx for android development.
Please share your experience, how exactly did you solve the problem of supporting various screen sizes, resolutions, dpi when using ligdx?
Which method do you prefer? Share a description, ideas, links, or more.
The android system itself is able to "select" the necessary resources from drawable-hdpi / drawable-ldpi / and so on.
But when using libgdx, this useful property is left aside, alas.
In addition, libgdx "looks" for files in assets/, which is how it's designed from the start.
Another question: is it possible to first get the desired file from drawable/ (so that the system itself chooses the preferred one) and only after that “feed” it to libgdx?
I will be glad to any answers.
Thanks in advance.
Answer the question
In order to leave comments, you need to log in
In libgdx, in a good way, you need to use AssetManager to load resources, assigning loaders for different types of resources to it:
AssetManager manager = new AssetManager();
manager.setLoader(Texture.class, new TextureLoader( new InternalFileHandleResolver() ));
Resolution[] resolutions = { new Resolution(320, 480, ".320480"),
new Resolution(480, 800, ".480800"),
new Resolution(480, 856, ".480854") };
ResolutionFileResolver resolver = new ResolutionFileResolver(new InternalFileHandleResolver(), resolutions);
Work with relative coordinates, and when outputting already with absolute ones.
Determine the screen size in relative coordinates.
float CAMERA_WIDTH = 12f;
float CAMERA_HEIGHT = 10f;
CAMERA_WIDTH = CAMERA_HEIGHT* Gdx.graphics.getWidth()/Gdx.graphics.getHeight();
ppuX = (float)Gdx.graphics.getWidth() / CAMERA_WIDTH;
ppuY = (float)Gdx.graphics.getHeight() / CAMERA_HEIGHT;
If you rigidly set the dimensions in pixels, then yes, the same display on different screens will not work. Therefore, as an option, you can rely on Gdx.graphics.getDensity(), changing the dimensions depending on this value. However, this approach seems to me not entirely correct, but I don’t know how to do it right.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question