Skip to content Skip to sidebar Skip to footer

How To Scale Image According To Different Screen Resolutions In Libgdx

I am using 480*800 image for my libgdx game. It is running absolutely fine on my smartphone but when i try to run it on high end devices or say tab with resolution 800*1280, it jus

Solution 1:

You can use ViewPort as maintained above or use your own solution, like this:

publicstaticfloatSCALE_RATIO= YOUR_IMAGE_WIDTH / Gdx.graphics.getWidth();

    publicstatic Sprite createScaledSprite(Texture texture) {
       Spritesprite=newSprite(texture);
       sprite.getTexture().setFilter(TextureFilter.Linear,
                TextureFilter.Linear);
       sprite.setSize(sprite.getWidth() / SCALE_RATIO,
            sprite.getHeight() / SCALE_RATIO);
       return sprite;
    }

Solution 2:

You can use viewports. https://github.com/libgdx/libgdx/wiki/Viewports

This way to are programming for a fixed resolution, programming to a relative resolution can be tricky.

Solution 3:

You can always access the width and height of the camera you are using. In your screen you should have a resize method which you can use to resize the viewport of the camera or so. Then you can scale towards the camera size.

Post a Comment for "How To Scale Image According To Different Screen Resolutions In Libgdx"