Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用固定的aspectratio在虚拟屏幕分辨率上处理输入_Java_Android_Libgdx - Fatal编程技术网

Java 使用固定的aspectratio在虚拟屏幕分辨率上处理输入

Java 使用固定的aspectratio在虚拟屏幕分辨率上处理输入,java,android,libgdx,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

我在我的相机上使用一个固定的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);
    }
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);
这真的很好,我可以用我的虚拟分辨率。但我的触摸板有问题。我只是这样开始:

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);
    }
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);
如果屏幕与虚拟屏幕不匹配,它在触摸正确时不会做出反应,甚至不会做出反应,因为键盘将位于左上角,但实际上位于虚拟凸轮上

有什么建议可以解决这个问题吗

这是它的一张照片。你看到的区域永远不会改变,所以你总是能看到整个地图。我给触摸板“所在”的区域上色。男厕所的按钮也有同样的问题


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

@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 ;)
    }
@覆盖
公共空心调整大小(整型宽度、整型高度){
cam.viewportHeight=高度;
cam.viewportWidth=宽度;
如果(Config.VIRTUAL\u VIEW\u WIDTH/cam.viewportWidth