简体   繁体   中英

How to scale sprites in libgdx according to screen resolutions?

I am trying to scale the texture to fit the screen width. This is what I tried, but it simply repeats the texture. It does not scale it.

In the init method:

TextureLoader.TextureParameter param = new TextureLoader.TextureParameter();
param.minFilter = Texture.TextureFilter.MipMapLinearLinear;
param.genMipMaps = true;
param.wrapU = Texture.TextureWrap.ClampToEdge;
param.wrapV = Texture.TextureWrap.ClampToEdge;
manager.load("textures/texture.png", Texture.class, param);

In the render method:

Texture tex = manager.get("textures/texture.png", Texture.class);
float scale = (float)( (float)Gdx.graphics.getWidth() / (float)(tex.getWidth()));

batch.begin();
Sprite s = new Sprite(tex, 0,0,tex.getWidth(),tex.getHeight());

s.setPosition(0, 0);
s.setOriginCenter();
//s.setScale(scale);
s.setSize(Gdx.graphics.getWidth(), scale * tex.getHeight());
s.setOrigin(0,0);
s.draw(batch);
batch.end();

Does anyone have an idea what I am doing wrong?

Apparently this happened because I was using mipmaps. Without mipmapping, the sprites scale appropriately.

 
 
 
  
  param.minFilter = Texture.TextureFilter.MipMapLinearLinear; param.genMipMaps = true;
 
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM