Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.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
Android Libgdx:Clicklistener无法调整大小_Android_Libgdx_Screen Resolution_Aspect Ratio - Fatal编程技术网

Android Libgdx:Clicklistener无法调整大小

Android Libgdx:Clicklistener无法调整大小,android,libgdx,screen-resolution,aspect-ratio,Android,Libgdx,Screen Resolution,Aspect Ratio,我的代码是完全功能,如果我去掉屏幕大小的关注。但现在我已经在包括平板电脑在内的多个设备上试用过,分辨率非常差。所以我试图在viewport的帮助下保持相同的纵横比。我正在使用stage添加我的按钮和图像。我的clicklistener正在响应“边沟部分”和“播放部分”按钮。Stackoverflow不允许我发布图像,因为有了新帐户。请任何人帮助我理解我做错了什么,并确保这段代码在手机和桌面上正常工作 private static final int VIRTUAL_WIDTH = 480; pr

我的代码是完全功能,如果我去掉屏幕大小的关注。但现在我已经在包括平板电脑在内的多个设备上试用过,分辨率非常差。所以我试图在viewport的帮助下保持相同的纵横比。我正在使用stage添加我的按钮和图像。我的clicklistener正在响应“边沟部分”和“播放部分”按钮。Stackoverflow不允许我发布图像,因为有了新帐户。请任何人帮助我理解我做错了什么,并确保这段代码在手机和桌面上正常工作

private static final int VIRTUAL_WIDTH = 480;
private static final int VIRTUAL_HEIGHT = 800;

public void resize(int width, int height) {
    // calculate new viewport

    Vector2 size = Scaling.fit.apply(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, width, height);
    int viewportX = (int)(width - size.x) / 2; 
    int viewportY = (int)(height - size.y) / 2;
    int viewportWidth = (int)size.x;
    int viewportHeight = (int)size.y;

    Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
}

@Override
public void show() {

    batch = new SpriteBatch();
    stage = new Stage();
    camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

    camera.setToOrtho(true, VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

    worldCoords  = new Vector3( VIRTUAL_WIDTH, VIRTUAL_HEIGHT, 0);
    camera.unproject(worldCoords);
    camera.update();


    white = new BitmapFont(Gdx.files.internal("font/white.fnt"), true);
    white.setScale(1f, -1f);

    skin = new Skin();
    atlas = new TextureAtlas("button.pack.pack");
    skin.addRegions(atlas);
    buttonStyle = new TextButtonStyle();
    buttonStyle.up = skin.getDrawable("button");
    buttonStyle.down = skin.getDrawable("buttonpressed");
    buttonStyle.font = white;
    buttonStyle.fontColor = Color.BLACK;




    buttonPlay =new TextButton("Play", buttonStyle);
    buttonPlay.setWidth(VIRTUAL_WIDTH/3);
    buttonPlay.setHeight(VIRTUAL_HEIGHT/12);

    buttonPlay.setPosition(stage.getViewport().getRightGutterWidth(), stage.getViewport().getWorldHeight()/2);


    buttonPlay.addListener(new ClickListener(){

        @Override
        public void clicked(InputEvent event, float x, float y){

            ((Game) Gdx.app.getApplicationListener()).setScreen(new MainFlow(g));

        }
    });

    Gdx.input.setInputProcessor(stage);


    Image img = new Image(new Texture(Gdx.files.internal("Optimized-BG_2.png")));
    img.setFillParent(true);


    stage.addActor(img);
    stage.addActor(buttonPlay);


}

@Override
public void render(float delta) {

    camera.update();
    batch.setProjectionMatrix(camera.combined);

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.act();
    stage.draw();

}

我不知道您到底想要什么,但首先尝试用resize方法更新stage,并读取libgdx


Viewport类执行您想要的所有操作。。
  stage.getViewport().update(width, height, true);