Android libgdx big screen resolution

眉间皱痕 提交于 2019-12-03 13:49:24

问题


How can I support (make an algorithm) for libgdx for supporting multiple screen resolution? I made my app work on HTC Tattoo using if statments with parameters like:

if (Gdx.input.getX()==40) {

What is a good algorithm for making this work on bigger screens? I tried this but without any result:

publis static int translatex() {
     float p = (float)Gdx.graphics.getHeight()*340;
     return (int) p*Gdx.input.getX();
}

340 is the base x (the x resolution on my phone) used by me on the HTC Tattoo. So.....how can I make a function for supporting big screens with absolute values. I don`t want to change the if-statements.


回答1:


It's pretty easy. You basically build your entire program for your native resolution, but in everything dealing with positioning and texture sizing have something like this:

private void resize()
{
    float x = Gdx.graphics.getWidth();
    float y = Gdx.graphics.getHeight();

    float changeX = x / assumeX; //being your screen size that you're developing with
    float changeY = y / assumeY;

    position = new Vector2(position.x * changeX, position.y * changeY);
    width = startWidth * changeX;
    height = startHeight * changeY;
    bounds = new Vector2 (position.x, (Gdx.graphics.getHeight() - position.y) - height);

}

Basically what you're doing is taking every object generated and running it through something that increases/decreases depending on the change in the x/y values in your resolution. The further it is from the native, the bigger it will be. When checking of something is somewhere or putting something somewhere, always code it as your desired resolution but run it through a resize function before displaying it or letting it interact with the rest of your program.




回答2:


See a more elegant solution at http://www.java-gaming.org/index.php?topic=25685.0 (also see Nate's comment to that solution).

Also, if you use scene2d, note that currently (libgdx 0.9.6) stage.setViewport method should have this functionality, but it doesn't really behave as one would expect.

Update: setViewPort was fixed, so this works just as expected.



来源:https://stackoverflow.com/questions/5988308/android-libgdx-big-screen-resolution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!