harsh_v
harsh_v

Reputation: 3269

How to resize a texture maintaining aspect ratio in libgdx

I have a .png image "circle.png". The problem is when I try to resize this in sb.draw() method it disorientates (stretches). It works fine when I draw it with it's default size;

    // inside constructor 
{
        Texture texture = new Texture("circle.png");
        ...
        .
}

// render method
public void render (SpriteBatch sb){
sb.begin();
sb.setProjectionMatrix(camera.combined);
sb.draw(texture,x,y,100,100);// 100 for both width and height
sb.end();
}

//camera setup

camera = new OrthographicCamera();
camera.setToOrtho(false, width / 2, height / 2);

// width n height is 480x640

First image is one without resizing Second image is while resizing without resizingwhile resizing

Upvotes: 1

Views: 1193

Answers (1)

staticcasty
staticcasty

Reputation: 102

Your image is probably not a square. To maintain the aspect ratio, use math:

100 = texture.getWidth()/a
a = texture.getWidth()/100


So use:

sb.draw(texture,x,y,100,texture.getHeight()/(texture.getWidth()/100);

I hope it helps!
staticcasty

Upvotes: 2

Related Questions