我知道如何使用像素坐标设置图像宽度和高度。有没有办法设置相对坐标?我想处理多个屏幕。
例如,我有1920x1080的屏幕尺寸。我的相对坐标宽度(-100,100)和身高= 200 * 1080/1920 = 112.5,身高(-56.25,56.25)
我可以使用此相对坐标设置宽度和高度吗?怎么做? 是否有最佳实践来处理不同的屏幕比例,正确处理方向变化?
提前感谢。
这是我在我的应用程序中使用它的方式:
创建()
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.update();
Skin skinBackground = new Skin();
skinBackground.add("background", new Texture(Gdx.files.absolute(FilePathSlicer.preparePath(taskSheet.background.getPicUrl()))));
background=new Image(skinBackground,"background");
if(background!=null) {
background.setWidth(width);
background.setHeight(height);
stage.addActor(background);
}
}
呈现
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(255/255f, 255/255f, 255/255f, 1);
stage.getBatch().setProjectionMatrix(camera.combined);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
答案 0 :(得分:1)
这是Viewports的用途。 Viewport是灵活的,与舞台相连(实际上就像透明度,你正在添加项目[按钮,标签等...])。
它通过创建适当类型的视口以您定义的方式进行缩放。
主要步骤是:
//create() method
stage = new Stage(); //creating a stage
viewport = new FillViewport( screenWidth, screenHeight ); //creating viewport choosing a proper Viewport class - for example Fill
stage.setViewport( viewport ); //applying actors (buttons, labels etc...) to a stage
//render() method
stage.act(); //acting a stage to calculate positions of actors etc
stage.draw(); //drawing it to render all
//resize(width, height) method
viewport.update(width, height); //updateing viewport that it scales in proper way
您有许多类型的视频,例如:
等
当然,你可以为每个阶段提供多个阶段和其他视口。
要了解有关舞台,演员和视口的更多信息,请访问:https://github.com/libgdx/libgdx/wiki/Scene2d
您不必关心屏幕方向 - 您需要做的就是在Android Manifest中定义您的应用方向