使用固定的aspectratio处理虚拟Screenresolution上的输入

时间:2013-03-26 14:29:16

标签: java android libgdx

我在相机上使用具有固定aspec比率的虚拟屏幕分辨率。 Init看起来像这样:

this.cam = new OrthographicCamera(Config.VIRTUAL_VIEW_WIDTH,
                Config.VIRTUAL_VIEW_HEIGHT);
        this.cam.setToOrtho(false, Config.VIRTUAL_VIEW_WIDTH,
                Config.VIRTUAL_VIEW_HEIGHT);
        // create our stage
        this.stage = new Stage(0, 0, true);
        this.stage.clear(); // always clear the stage too to not get double
                            // stuff in a stage

        stage.setCamera(cam);
//some more stuff
@Override
    public void resize(int width, int height) {
        // this.cam.setToOrtho(false, width, height);

        // calculate new viewport
        float aspectRatio = (float) width / (float) height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);

        if (aspectRatio > Config.ASPECT_RATIO) {
            scale = (float) height / (float) Config.VIRTUAL_VIEW_HEIGHT;
            crop.x = (width - Config.VIRTUAL_VIEW_WIDTH * scale) / 2f;
        } else if (aspectRatio < Config.ASPECT_RATIO) {
            scale = (float) width / (float) Config.VIRTUAL_VIEW_WIDTH;
            crop.y = (height - Config.VIRTUAL_VIEW_HEIGHT * scale) / 2f;
        } else {
            scale = (float) width / (float) Config.VIRTUAL_VIEW_WIDTH;
        }

        float w = (float) Config.VIRTUAL_VIEW_WIDTH * scale;
        float h = (float) Config.VIRTUAL_VIEW_HEIGHT * scale;
        viewport = new Rectangle(crop.x, crop.y, w, h);
    }

渲染本身很短:

@Override
public void render(float delta) {
    cam.update();
    // setting the viewport
    Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
            (int) viewport.width, (int) viewport.height);
    // the following code clears the screen with the given RGB color (black)
    Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // update and draw the stage actors
    stage.act(delta);
    stage.draw();

这非常好用,我可以使用我的虚拟分辨率。但是我遇到了触控板的问题。我只是这样做:

Gdx.input.setInputProcessor(stage);

skin = new Skin();
        skin.add("knob", new Texture("touchpad/touchKnob.png"));
        skin.add("background", new Texture("touchpad/touchBackground.png"));

        style = new TouchpadStyle();
        // skin.add
        style.knob = skin.getDrawable("knob");
        style.background = skin.getDrawable("background");

        pad = new Touchpad(10, style);
        pad.setBounds(0, Config.VIRTUAL_VIEW_HEIGHT - 150, 150, 150);
        this.stage.addActor(pad);

如果屏幕不适合虚拟屏幕,它在触摸上没有反应正确甚至没有反应,因为垫片位于左上边缘但是在虚拟凸轮上是真实的。

有任何建议如何解决?

这是一张照片。您看到的区域永远不会改变,因此您始终可以看到整个地图。我为触摸板“是”的区域着色。 Menü中的按钮也遇到了同样的问题。

enter image description here

1 个答案:

答案 0 :(得分:0)

在其他论坛的帮助下解决了这个问题。我更改了调整大小的方法,以便相机缩小/放大:

@Override
    public void resize(int width, int height) {
        cam.viewportHeight = height;
        cam.viewportWidth = width;
        if (Config.VIRTUAL_VIEW_WIDTH / cam.viewportWidth < Config.VIRTUAL_VIEW_HEIGHT
                / cam.viewportHeight) {
            cam.zoom = Config.VIRTUAL_VIEW_HEIGHT / cam.viewportHeight;
        } else {
            cam.zoom = Config.VIRTUAL_VIEW_WIDTH / cam.viewportWidth;
        }
        cam.update(); // update it ;)
    }