java - 如何使用 ViewPorts 为 libgdx 中的不同分辨率缩放 ImageButtons?
问题描述
我是尝试制作游戏的 libgdx 框架的新手,但这里的问题是我不知道如何缩放ImageButton
不同的屏幕尺寸,它在桌面上看起来很大,但在 android 设备上看起来很小。我正在使用几种ImageButtons
不同的尺寸并使用表格来组织它们。任何人都可以进行一些代码更改,以便我可以理解如何使用视口缩放此处的图像?
public class BabyPhone extends Game {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public void create () {
bg = new BabyActor();
bg.setTexture(new Texture("background-orange.png"));
bg.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
phone = new BabyActor();
phone.setTexture(new Texture("blue-phone.png"));
phone.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
TextureRegion btLeft = new TextureRegion(new Texture("10OFF.png"));
Drawable drawableLeft = new TextureRegionDrawable(new TextureRegion(btLeft));
buttonLeft = new ImageButton(drawableLeft);
TextureRegion btRight = new TextureRegion(new Texture("12OFF.png"));
Drawable drawableRight = new TextureRegionDrawable(new TextureRegion(btRight));
buttonRight = new ImageButton(drawableRight);
TextureRegion btCenter = new TextureRegion(new Texture("11OFF.png"));
Drawable drawableCenter = new TextureRegionDrawable(new TextureRegion(btCenter));
buttonCenter = new ImageButton(drawableCenter);
TextureRegion bt1 = new TextureRegion(new Texture("1OFF.png"));
Drawable drawable = new TextureRegionDrawable(new TextureRegion(bt1));
button1 = new ImageButton(drawable);
TextureRegion bt2 = new TextureRegion(new Texture("2OFF.png"));
Drawable drawable1 = new TextureRegionDrawable(new TextureRegion(bt2));
button2 = new ImageButton(drawable1);
TextureRegion bt3 = new TextureRegion(new Texture("3OFF.png"));
Drawable drawable2 = new TextureRegionDrawable(new TextureRegion(bt3));
button3 = new ImageButton(drawable2);
TextureRegion bt4 = new TextureRegion(new Texture("4OFF.png"));
Drawable drawable3 = new TextureRegionDrawable(new TextureRegion(bt4));
button4 = new ImageButton(drawable3);
TextureRegion bt5 = new TextureRegion(new Texture("5OFF.png"));
Drawable drawable4 = new TextureRegionDrawable(new TextureRegion(bt5));
button5 = new ImageButton(drawable4);
TextureRegion bt6 = new TextureRegion(new Texture("6OFF.png"));
Drawable drawable5 = new TextureRegionDrawable(new TextureRegion(bt6));
button6 = new ImageButton(drawable5);
TextureRegion bt7 = new TextureRegion(new Texture("7OFF.png"));
Drawable drawable6 = new TextureRegionDrawable(new TextureRegion(bt7));
button7 = new ImageButton(drawable6);
TextureRegion bt8 = new TextureRegion(new Texture("8OFF.png"));
Drawable drawable7 = new TextureRegionDrawable(new TextureRegion(bt8));
button8 = new ImageButton(drawable7);
TextureRegion bt9 = new TextureRegion(new Texture("9OFF.png"));
Drawable drawable8 = new TextureRegionDrawable(new TextureRegion(bt9));
button9 = new ImageButton(drawable8);
gamecam = new OrthographicCamera(WIDTH,HEIGHT);
gamecam.position.set(WIDTH/2,HEIGHT/2,0);
gamePort = new ScreenViewport(gamecam);
backStage = new Stage(gamePort);
fitPort = new FitViewport(gamecam.viewportWidth,gamecam.viewportHeight,gamecam);
frontStage = new Stage(fitPort);
backStage.addActor(bg);
frontStage.addActor(phone);
Table table = new Table();
table.padLeft(40);
table.setPosition(phone.getWidth()/2,phone.getHeight()/2*0.6f);
table.row().size(95,95);
table.add(buttonLeft);
table.add(buttonCenter);
table.add(buttonRight);
table.row().size(95,95);
table.add(button1);
table.add(button2);
table.add(button3);
table.row().size(95,95);
table.add(button4);
table.add(button5);
table.add(button6);
table.row().size(95,95);
table.add(button7);
table.add(button8);
table.add(button9);
frontStage.addActor(table);
}
public void render(){
backStage.act(Gdx.graphics.getDeltaTime());
frontStage.act(Gdx.graphics.getDeltaTime());
backStage.draw();
frontStage.draw();
}
@Override
public void resize(int width, int height) {
gamePort.update(width, height);
frontStage.getViewport().update(width, height);
}
解决方案
我只是将其设置为设备大小的百分比。它看起来像这样。
float BUTTON_SIZE = 0.1f;
table.add(button2).size(Gdx.graphics.getWidth()*BUTTON_SIZE,Gdx.graphics.getWidth()*BUTTON_SIZE);
推荐阅读
- html - Git 实时预览 HTML 文件不起作用
- automated-tests - 为 QAF 编写 BDD 文件以检查特定路径上 excel 表(单元格中的数组)中的值
- swift - 如何实现将根据提供的项目数量(物理项目的数量)滚动的集合视图?
- javascript - 如何在服务器中正确显示我的项目?
- enums - 使用 Flags 属性将整数转换为枚举类型
- python - 取消后等待 asyncio Future
- rust - 将 Option 与 self 的可变借用和 None 的不可变借用匹配
- ios - 在应用程序启动时从底部显示目标 c 或情节提要中的主视图控制器
- python - 如何在选项菜单中添加 YouTube 链接?
- python - 如何确定请求语言?(没有要求)