所以我的文本框最终会在不同的地方结束,这取决于我运行它的设备正在撕裂我的灵魂。我真的无法解决这个问题。
这是代码: 我知道它现在是一团糟,因为我正在使用Libgdx提供的视口API。但它没有任何帮助。 描述我尝试的最后一件事是我设置X&文本框的Y坐标是最终的,没有单位分配给它。虽然在我的平板电脑上运行时,文本框总是最终完全远离我们手机上设置的位置。
public class StoneScreen implements Screen, purchaseInterface {
OrthographicCamera camera;
Viewport view;
final TombStone game;
purchaseInterface pInt;
//Textures and art.
public Texture background, sdStone, arrowBack, stone_3, flowerTex, flowerTex_2, flowerTemp;
public Sprite backgrounds;
//TextField´s stuff
private Stage stage;
private Skin skin;
ImageButton btnArrow, btnFB;
Image stoneImage, flowerImage, flowerImage_2, flowerImageTemp;
private static int counter= 1;
private static final float screenWidth = 1000;
private static final float screenHeight = 1500;
private final float textX = 200;
private final float textY = 700;
private final float textAreaX = 200;
private final float textAreaY = 400;
private final int textWidth = 350;
private final int textHeight = 100;
private final float stoneWidth = 600;
private final float stoneHeight = 800;
public StoneScreen(TombStone gam) {
this.game = gam;
this.pInt = game.pi;
//Set´s the current device´s height and width into a float for easier usage
//Set´s the tombstones height and width
//viewport = new FitViewport(camera, screenWidth, screenHeight)
float aspectRatio = (float) Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth();
camera = new OrthographicCamera();
camera.setToOrtho(false, screenWidth, screenHeight);
game.assets.load();
loadStandard();
stoneImage = new Image(sdStone);
flowerImage = new Image(flowerTemp);
//flowerImageTemp = new Image(flowerTemp);
//flowerImage_2 = new Image(flowerTex_2);
}
public void loadStandard(){
background = game.assets.background;
sdStone = game.assets.sdStone;
stone_3 = game.assets.stone3;
flowerTex = game.assets.flowerTexture;
flowerTemp = game.assets.flowerTemp;
//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);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
Gdx.app.log("X", "FPS:" + Gdx.graphics.getFramesPerSecond());
game.batch.draw(background,0,0);
//game.batch.draw(sdStone, 160, 190, screenWidth * 0.60f, screenHeight * 0.60f);
stoneImage.setBounds(180, 190, stoneWidth, stoneHeight);
stoneImage.draw(game.batch, 2);
flowerImage.setBounds(300, 50, screenWidth * 0.65f, screenHeight * 0.30f);
flowerImage.draw(game.batch, 2);
//SpriteBatch batcher = (SpriteBatch)stage.getBatch();
game.batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void resize(int width, int height) {
//float aspectRatio = (float) height / (float) width;
//camera = new OrthographicCamera(screenWidth, screenHeight * aspectRatio);
//view.update(width, height);
camera.update();
}
@Override
public void show() {
//Generates the new font
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("AcademyEngraved.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 40;
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(textAreaX);
textArea.setY(textAreaY);
textArea.setWidth(textWidth);
textArea.setHeight(textHeight);
textArea.setMaxLength(30);
textArea.setZIndex(5);
final TextField textField = new TextField(prefs.getString("textField", "Enter name:"), textstyle);
textField.setX(textX);
textField.setY(textY);
textField.setMaxLength(20);
textField.setWidth(textWidth);
textField.setHeight(textHeight);
//String text = Gdx.app.getPreferences("prefs").getString("text", "Default text if missimg");
//TextField textField = new TextField(text, skin);
//Backbutton
ImageButtonStyle styleTwo = new ImageButtonStyle();
//ImageButtonStyle styleFB = new ImageButtonStyle();
TextureRegionDrawable arrowImage = new TextureRegionDrawable(new TextureRegion(new Texture("pil.png")));
TextureRegionDrawable arrowImageDown = new TextureRegionDrawable(new TextureRegion(new Texture("pilD.png")));
//TextureRegionDrawable fbImage = new TextureRegionDrawable(new TextureRegion(new Texture("fb.png")));
//TextureRegionDrawable fbImageDown = new TextureRegionDrawable(new TextureRegion(new Texture("fbDown.png")));
styleTwo.up = skin.newDrawable(skin.newDrawable(arrowImage));
styleTwo.down = skin.newDrawable(skin.newDrawable(arrowImageDown));
styleTwo.pressedOffsetX = -1;
styleTwo.pressedOffsetY = -1;
//styleFB.up = skin.newDrawable(skin.newDrawable(fbImage));
//styleFB.down = skin.newDrawable(skin.newDrawable(fbImageDown));
//styleFB.pressedOffsetX = -1;
//styleFB.pressedOffsetY = -1;
/*Object[] dropMenu = new Object[2];
dropMenu[0] = new Label("this iaios", skin);
final SelectBox<Object> sb = new SelectBox<Object>(skin);
sb.setItems(dropMenu);
Table table = new Table();
table.setFillParent(true);
table.setPosition(0, 800);
table.add(sb);*/
//Back button
btnArrow = new ImageButton(styleTwo);
//btnFB = new ImageButton(styleFB);
btnArrow.setSize(150, 150);
btnArrow.setPosition(50, 10);
//btnFB.setSize(100, 100);
//btnFB.setPosition(800, 10);
stage.addActor(textArea);
stage.addActor(textField);
stage.addActor(btnArrow);
//stage.addActor(table);
//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
}
public void changeStone1() {
//sdStone.dispose();
stoneImage.setDrawable(new SpriteDrawable(new Sprite(stone_3)));
camera.update();
}
private static void saveScreenshot() {
try{
FileHandle fh;
do{
fh = new FileHandle("screenshot" + counter++ + ".png");
}while(fh.exists());
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
PixmapIO.writePNG(fh, pixmap);
pixmap.dispose();
}catch(Exception e) {
}
}
private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){
final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);
if(yDown) {
ByteBuffer pixels = pixmap.getPixels();
int numBytes = w * h * 4;
byte[] lines = new byte[numBytes];
int numBytesPerLine = w * 4;
for (int i = 0; i < h; i++) {
pixels.position((h - i - 1) * numBytesPerLine);
pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
}
pixels.clear();
pixels.put(lines);
}
return pixmap;
}
@Override
public void applyTombstone() {
// TODO Auto-generated method stub
}
@Override
public void applyTombstone1() {
// TODO Auto-generated method stub
}
@Override
public void applyTombstone2() {
// TODO Auto-generated method stub
}
@Override
public void applyFlower() {
// TODO Auto-generated method stub
flowerImage.setDrawable(new SpriteDrawable(new Sprite(flowerTex)));
camera.update();
}
@Override
public void applyFlower1() {
// TODO Auto-generated method stub
}
@Override
public void applyLight() {
// TODO Auto-generated method stub
}
@Override
public void processPurchases() {
// TODO Auto-generated method stub
}
@Override
public void changeStone() {
// TODO Auto-generated method stub
stoneImage.setDrawable(new SpriteDrawable(new Sprite(stone_3)));
camera.update();
}
}
答案 0 :(得分:0)
所以这是解决方案:
@Override
public void resize(int width, int height) {
stageText.getViewport().update(width, height, true);
}
我只是将textField设置为一个单独的Stage,以便我的应用程序中的其他元素不会受到影响。