[英]Stage object won't center and scale to be same on every screen
I have 2 questions which both seem should be easy. 我有两个问题似乎都很容易。
First, I am working on a LibGDX game but one of my Table objects won't center 首先,我正在开发LibGDX游戏,但我的Table对象之一不会居中
Table table = new Table();
table.setFillParent(true);
table.add(preview).size(preview.getWidth(), preview.getHeight()).center().padBottom(40);
table.row();
table.add(last).size(last.getWidth(), last.getHeight()).center().left().padLeft(250);
table.add(next).size(next.getWidth(), next.getHeight()).center().right().padRight(250);
I tried the code above but it turns out like (I want the Level Preview centered) 我尝试了上面的代码,但结果却像(我希望“关卡预览”居中)
Question 2 问题2
On my phone these images all fit perfectly. 在我的手机上,这些图像都非常适合。 but on this phone they are smaller, How would i scale it to be the same size on every phone, is it something with FitViewPort? 但是在这部手机上它们更小,我如何将其缩放为每部手机上的相同大小,FitViewPort是否有用?
Question #1: 问题1:
Change 更改
table.add(preview).size(preview.getWidth(), preview.getHeight()).center().padBottom(40);
to 至
table.add(preview).size(preview.getWidth(), preview.getHeight()).center().padBottom(40).colspan(2);
Question #2: 问题2:
The FitViewport
may lead to black bars, but the images scale in relation to the aspect ratio. FitViewport
可能会导致黑条,但图像会根据纵横比进行缩放。 Try using the StretchViewport
instead, if you want to scale your images in relation to the phone size. 如果要根据手机尺寸缩放图像,请尝试使用StretchViewport
。
To get the preview Image centered, it's cell needs to span all the columns. 为了使预览图像居中,它的单元格需要跨越所有列。 It is unnecessary to try to size the cell to the Image if you want the image to exactly fit. 如果您希望图像完全适合,则无需尝试将单元格调整为图像大小。
And if you want the two buttons right at the middle, make each of their cells as wide as possible and bias them toward the side of the cell that is at the middle. 而且,如果您希望两个按钮都位于中间,则使它们的每个单元格尽可能宽,然后将它们向中间的单元格一侧倾斜。
So do this: 这样做:
table.setFillParent(true);
table.add(preview).colSpan(2).expandX().center().padBottom(40);
table.row();
table.add(last).expandX().right();
table.add(next).expandX().left();
ScreenViewport is nice for UI stages because it results in nice crisp text. ScreenViewport非常适合UI阶段,因为它可以产生清晰的文本。 But the downside is that you need to have multiple UI asset sizes ready to load depending on the device resolution. 但是不利的是,您需要准备好多个UI资产大小来加载,具体取决于设备分辨率。
Alternatively you can use ExtendViewport for your UI so you only need one asset scale. 另外,您可以将ExtendViewport用于您的UI,因此您只需要一个资产比例。
The rest of the viewport classes are junk (IMO): 其余的视口类是垃圾(IMO):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.