问题
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 just show the image at one corner of the screen for obvious reasons.
I am sure it just needs some camera settings to scale up and down according to the different screen resolutions. Please help me or provide any pointer.
回答1:
You can use ViewPort as maintained above or use your own solution, like this:
public static float SCALE_RATIO = YOUR_IMAGE_WIDTH / Gdx.graphics.getWidth();
public static Sprite createScaledSprite(Texture texture) {
Sprite sprite = new Sprite(texture);
sprite.getTexture().setFilter(TextureFilter.Linear,
TextureFilter.Linear);
sprite.setSize(sprite.getWidth() / SCALE_RATIO,
sprite.getHeight() / SCALE_RATIO);
return sprite;
}
回答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.
回答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.
来源:https://stackoverflow.com/questions/25399233/how-to-scale-image-according-to-different-screen-resolutions-in-libgdx