Search code examples
androidlibgdx

Libgdx Choosing correct textures based on DPI?


How to implement a system which takes the best texture based on dpi from a set like in android SDK, because LibGDX is platform independent, and can't use the already existent one?


Solution

  • May be my solution is not the best but I used it in real project and it worked.

    In your Game class use Gdx.graphics.getDensity() method to choose appropriate folder, store its name in public field and load your assets:

    public class MyGame extends Game {
    
    public static String folder;
    private AssetManager assets;
    
    @Override
    public void create() {
       if (Gdx.graphics.getDensity < 1) {
          folder = "lowDpiImages/";
       } else {
          folder = "highDpiImages/";
       }
       ...
       assets = new AssetManager();
       assets.load(folder + "image.png", Texture.class, paramsNearest);
       ...
    }
    

    Then in you other classes use folder name to get assets from AssetManager:

    assets.get(MyGame.folder + "image.png", Texture.class);
    

    You can write more sophisticated folder choosing algorithm of cause ;-)