我正在開發與libgdx
遊戲,我在不同的設備上卡住長寬比。
很多的思考之後,我計算過,下面是這個問題最好的解決辦法:在libgdx處理高寬比android
我想有相機始終固定爲16:9
寬高比,並使用該相機
如果設備方面是例如4:3
借鑑一切我只想展示部分視圖,而不是展示它。
應當看起來像這樣
blue
是虛擬屏幕(照相機視口)和red
是設備屏幕(可見區域爲4:3層的設備)
如果例如設備的屏幕也16:9
全視圖應該可見。
問題是我不知道如何實現這一點。
我正在開發與libgdx
遊戲,我在不同的設備上卡住長寬比。
很多的思考之後,我計算過,下面是這個問題最好的解決辦法:在libgdx處理高寬比android
我想有相機始終固定爲16:9
寬高比,並使用該相機
如果設備方面是例如4:3
借鑑一切我只想展示部分視圖,而不是展示它。
應當看起來像這樣
blue
是虛擬屏幕(照相機視口)和red
是設備屏幕(可見區域爲4:3層的設備)
如果例如設備的屏幕也16:9
全視圖應該可見。
問題是我不知道如何實現這一點。
我已經爲肖像屏幕做了這個,在頂部和底部留下了一些空白空間。正如你在下面的圖片中看到的那樣,遊戲保持在4:3的比例,並留下剩下的空白。
內容總是居中,並且通過不拉伸不均勻的內容保持其寬高比。所以這裏有一些示例代碼我用它來實現它。
public static final float worldW = 3;
public static final float worldH = 4;
public static OrthographicCamera camera;
...
//CALCULATING THE SCREENSIZE
float tempCalc = Gdx.graphics.getHeight() * GdxGame.worldW/Gdx.graphics.getWidth();
if(tempCalc < GdxGame.worldH){
//Adjust width
camera.viewportHeight = GdxGame.worldH;
camera.viewportWidth = Gdx.graphics.getWidth() * GdxGame.worldH/Gdx.graphics.getHeight();
worldWDiff = camera.viewportWidth - GdxGame.worldW;
}else{
//Adjust Height
camera.viewportHeight = tempCalc;
camera.viewportWidth = GdxGame.worldW;
worldHDiff = camera.viewportHeight - GdxGame.worldH;
}
camera.position.set(camera.viewportWidth/2f - worldWDiff/2f, camera.viewportHeight/2f - worldHDiff/2f, 0f);
camera.zoom = 1f;
camera.update();
我敢肯定,我不是提出了完美的解決方案,但你可以用值如何攝像機的位置和視口計算這樣你就可以達到預期的效果發揮。我猜想比沒有好。
此外,衝突的奧運選手開發商談如何實現類似的東西,仍然使它看起來不同的設備上良好的(這是真的很有趣,但沒有代碼雖然):Our solution to handle multiple screen sizes in Android - Part one
較新版本libGDX爲glViewport提供了一個名爲Viewport的包裝器。
Camera camera = new OrthographicCamera();
//the viewport object will handle camera's attributes
//the aspect provided (worldWidth/worldHeight) will be kept
Viewport viewport = new FitViewport(worldWidth, worldHeight, camera);
//your resize method:
public void resize(int width, int height)
{
//notice that the method receives the entire screen size
//the last argument tells the viewport to center the camera in the screen
viewport.update(width, height, true);
}
我寫了一個函數,可以處理不同大小的屏幕和圖像。如果可能,圖像可能會縮放並翻譯。
public void transformAndDrawImage(SpriteBatch spriteBatch, Texture background, float scl, float offsetX, float offseY){
float width;
float height;
float _offsetX;
float _offsetY;
float bh2sh = 1f*background.getHeight()/Gdx.graphics.getHeight();
float bw2sw = 1f*background.getWidth()/Gdx.graphics.getWidth();
float aspectRatio = 1f*background.getHeight()/background.getWidth();
if(bh2sh>bw2sw){
width = background.getWidth()/bw2sw * scl;
height = width * aspectRatio;
}
else{
height = background.getHeight()/bh2sh * scl;
width = height/aspectRatio;
}
_offsetX = (-width+Gdx.graphics.getWidth())/2;
_offsetX += offsetX;
if(_offsetX-Gdx.graphics.getWidth()<=-width) _offsetX=-width+Gdx.graphics.getWidth();
if(_offsetX>0) _offsetX=0;
_offsetY = (-height+Gdx.graphics.getHeight())/2;
_offsetY += offseY;
if(_offsetY-Gdx.graphics.getHeight()<=-height) _offsetY=-height+Gdx.graphics.getHeight();
if(_offsetY>0) _offsetY=0;
spriteBatch.draw(background, _offsetX, _offsetY, width, height);
}
如何使用它?這很容易:
@Override
public void render(float delta) {
Gdx.graphics.getGL10().glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
//zoom the image by 20%
float zoom = 1.2f; //Must be not less than 1.0
//translating the image depends on a few parameters such as zooming, aspect ratio of screen and image
offsetX+=0.1f; //offset the image to the left, if it's possible
offsetY+=0.1f; //offset the image to the bottom, if it's possible
transformAndDrawImage(spriteBatch, background, zoom, offsetX, offsetY);
//draw something else...
spriteBatch.end();
}
你看過新的'Viewport'類嗎?看看這個鏈接:https://github.com/libgdx/libgdx/wiki/Viewports 也許它有一個類,你可以使用它。 – Springrbua