How to view images with a proportional screen size with libgdx?

Asked

Viewed 378 times

1

How do I display my game images in the size proportional to the device screen size, so that if an image is 1/3 wide of the screen on a 480x800 screen it should also be 1/3 wide on the 320x480 or 720x1280? I think it’s with 'camera.setToOrtho()', someone knows how to do?

  • You are using a Sprite for the image ?

  • yes friend. Actor with Sprite

  • would be more or less that (https://igoralves1.github.io/) what you want? The image can be opened by any device. But in my case I chose to use 100% of the screen.

1 answer

1


Well, I’ve worked very little with libgdx, but from what I’ve seen, you can use Viewports to resize your image, however, I confess that I have never tested it in this way.

Another solution, which I’ve seen being implemented is to create your own function to resize your Sprite, for example:

 public static float MINHA_PROPORCAO = WIDTH_IMAGEM / Gdx.graphics.getWidth(); // Calcula uma proporção baseada no tamanho da tela.

  public static Sprite CriarSprite(Texture textura) {

   Sprite sprite = new Sprite(textura);
   sprite.getTexture().setFilter(TextureFilter.Linear,TextureFilter.Linear);
   sprite.setSize(sprite.getWidth() / MINHA_PROPORCAO ,
   sprite.getHeight() / MINHA_PROPORCAO );
   return sprite;
  }

I hope it helps :)

  • Okay, SCALE_RATIO is the constant ratio?

  • That’s right, I’ll edit sorry

  • @Emersonbarcellos If the answer helped you and cleared your doubts, consider granting the promised bonus :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.