我是Libgdx的新手,遇到了一些问题,无法将图像填充到整个屏幕。如果导入一个1900x1200的图像,它会填满整个屏幕,但如果使用1024x512的图像,则图像不会被拉伸到屏幕上。以下是我使用的代码。我认为“background.setSize(stageWidth,stageHeight)”会将图像缩放到屏幕大小,但实际上并没有发生。请告诉我我做错了什么?我已经尝试过使用“setFillParent”(如另一篇帖子中所提到的),但仍然没有效果。这里还包括一个当前显示内容的截图(在桌面实现和Android上均如此):http://tinypic.com/r/b7j20z/8
谢谢您的帮助!
package com.mygdx.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MyGdxGame implements ApplicationListener{
private Stage stage;
@Override
public void create() {
stage = new Stage();
float stageHeight = Gdx.graphics.getHeight();
float stageWidth = Gdx.graphics.getWidth();
// Background
Texture board = new Texture(Gdx.files.internal("data/frame.png"));
Image background = new Image(board);
background.setOrigin(0, 0);
background.setSize(stageWidth, stageHeight);
background.rotateBy(0);
background.setPosition(0, 0);
stage.addActor(background);
}
@Override
public void dispose() {
stage.dispose();
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
setFillParent
? - kabb