Java 在不同屏幕尺寸上改变位置的元素
所以我在手机上运行我的应用程序,当然一切都很好,因为我一直在朝着它发展。然后我试着在我的平板电脑上运行这个应用程序,我的Java 在不同屏幕尺寸上改变位置的元素,java,libgdx,screen-resolution,Java,Libgdx,Screen Resolution,所以我在手机上运行我的应用程序,当然一切都很好,因为我一直在朝着它发展。然后我试着在我的平板电脑上运行这个应用程序,我的文本框出现在屏幕上完全不同的位置。我正在使用Libgdx框架,这就是类的外观: public class StoneScreen implements Screen { OrthographicCamera camera; final TombStone game; //Textures and art. public Texture background, sdStone
文本框出现在屏幕上完全不同的位置。我正在使用Libgdx框架,这就是类的外观:
public class StoneScreen implements Screen {
OrthographicCamera camera;
final TombStone game;
//Textures and art.
public Texture background, sdStone, arrowBack;
public Sprite backgrounds;
//TextField´s stuff
private Stage stage;
private Skin skin;
ImageButton btnArrow;
float screenWidth, screenHeight, stoneHeight, stoneWidth;
//private FitViewport viewport;
public StoneScreen(TombStone gam) {
this.game = gam;
//Set´s the current device´s height and width into a float for easier usage
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
//Set´s the tombstones height and width
stoneHeight = screenHeight * 0.60f;
stoneWidth = screenWidth *0.60f;
//viewport = new FitViewport(camera, screenWidth, screenHeight)
camera = new OrthographicCamera();
camera.setToOrtho(false, screenWidth, screenHeight);
game.assets.load();
loadStandard();
}
public void loadStandard(){
background = game.assets.background;
sdStone = game.assets.sdStone;
//backgrounds = Assets.backgrounds;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Recieves the screen width and height
float gameHeight = Gdx.graphics.getHeight();
float gameWidth = Gdx.graphics.getWidth();
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
Gdx.app.log("X", "FPS:" + Gdx.graphics.getFramesPerSecond());
game.batch.draw(background,0,0, screenWidth, screenHeight);
game.batch.draw(sdStone, screenWidth *0.20f, screenHeight * 0.20f, screenWidth * 0.60f, screenHeight * 0.60f);
//SpriteBatch batcher = (SpriteBatch)stage.getBatch();
game.batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void show() {
//Generates the new font
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("NothingYouCouldDoBold.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 30;
BitmapFont textFont = generator.generateFont(parameter);
generator.dispose(); //Avoiding memory leaks.
Preferences prefs = Gdx.app.getPreferences("preferences");
Skin skin = new Skin();
Skin textSkin = new Skin();
stage = new Stage();
Gdx.input.setInputProcessor(stage);
//Sets skin
skin = new Skin(Gdx.files.internal("uiskin.json"));
//Sets a color to the new font
Color fontcolor = Color.BLACK;
//Sets a new font to the textare n the textfield
TextFieldStyle textstyle = new TextFieldStyle();
textstyle.font = textFont;
textstyle.fontColor = fontcolor;
final TextArea textArea = new TextArea(prefs.getString("textArea", "Enter text:"), textstyle);
textArea.setX(420);
textArea.setY(700);
textArea.setWidth(350);
textArea.setHeight(350);
textArea.setMaxLength(50);
final TextField textField = new TextField(prefs.getString("textField", "Enter name:"), textstyle);
textField.setX(420);
textField.setY(800);
textField.setMaxLength(20);
textField.setWidth(350);
textField.setHeight(100);
//String text = Gdx.app.getPreferences("prefs").getString("text", "Default text if missimg");
//TextField textField = new TextField(text, skin);
//Backbutton
ImageButtonStyle styleTwo = new ImageButtonStyle();
TextureRegionDrawable arrowImage = new TextureRegionDrawable(new TextureRegion(new Texture("arrowLeft.png")));
styleTwo.up = skin.newDrawable(skin.newDrawable(arrowImage));
styleTwo.down = skin.newDrawable(skin.newDrawable(arrowImage));
//Back button
btnArrow = new ImageButton(styleTwo);
btnArrow.setSize(150, 150);
btnArrow.setPosition(450, 10);
stage.addActor(textArea);
stage.addActor(textField);
stage.addActor(btnArrow);
//Backbutton takes us back to mainmenu
btnArrow.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new MainScreen(game));
//Saves the entered text.
Preferences prefs = Gdx.app.getPreferences("preferences");
prefs.putString("textField", textField.getText());
prefs.putString("textArea", textArea.getText());
prefs.flush();
}
});
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub.
}
}
我真的不知道如何解决这个问题。我尝试了谷歌的一些解决方案,但没有任何帮助。是TextField和TextArea给我带来了麻烦 我认为这可能会帮助您@Angelangle是对的,制作视口
s就是为了解决这个问题。其中一些支持虚拟宽度和高度,并将此虚拟大小向上/向下缩放以适应真实的屏幕。所以你不需要关心像素,但是你可以用你自己的优先单位来计算所有的东西。它们还关注不同的纵横比,例如,它们拉伸物体以适应真实的纵横比,或者它们显示黑色边框以适应所需的纵横比。