21

I am working android game development and I am new in it. I want to develop a game which screen resolution free. It will be enough for me if i get to know that how to catch screen resolution of an android device using libgdx library. After some googling i read the following code on different sites, but it is not for libgdx.

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;

Except this i also used technique given on the following website that used "OrthographicCamera" class for solving the problem, and didn't work for me either.

http://www.java-gaming.org/index.php?topic=25685.0

How can i get the screen resolution or any way to adjust screen resolution for my game.

4

5 回答 5

99
Gdx.graphics.getWidth();
Gdx.graphics.getHeight();

:)

于 2013-11-12T23:30:16.317 回答
9

好的,您可以通过此获得屏幕分辨率

Gdx.graphics.getWidth();

//屏幕宽度

Gdx.graphics.getHeight();

//屏幕高度

如果你想制作一个屏幕分辨率免费的游戏,你可以像这样设置视口

float scrw=320;

float scrh=480;

scrw 是视口的宽度,scrh 是视口的高度

并将相机设置为

camera = new OrthographicCamera();
camera.setToOrtho(false, scrw, scrh);
camera.update(); 
于 2013-11-13T06:06:44.570 回答
8

虽然这里有一些关于我的问题的答案。这正是对我有用的。我必须在“Gdx”之后添加“app”才能正常工作。

Gdx.app.graphics.getWidth(); 
Gdx.app.graphics.getHeight();
于 2014-07-17T01:17:11.970 回答
5

在较新的 LibGDX 版本中,代码现在是

int width = Gdx.app.getGraphics().getWidth();
int height = Gdx.app.getGraphics().getHeight();
于 2016-06-21T18:48:19.873 回答
-1
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();

为我工作。

于 2017-10-18T17:16:48.427 回答