简体   繁体   English

舞台对象不会在每个屏幕上居中和缩放相同

[英]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):

  • FitViewport creates black bars (your users won't like this because phone screens are too small as it is). FitViewport会创建黑条(您的用户不会喜欢,因为电话屏幕太小了)。 It's OK if you're using it for the UI layer, but then you need a different viewport to apply for everything else before you draw your UI. 如果将其用于UI层可以,但是在绘制UI之前,您需要一个其他视口来应用其他所有内容。 And maybe if you're doing a cutscene where black bars are forgivable, you could temporarily use one. 也许如果您在过场动画中可以忽略黑条,则可以暂时使用。
  • StretchViewport results in ugly deformed stretching. StretchViewport导致丑陋的变形拉伸。
  • FillViewport results in part of your virtual viewport size being cropped off so you have to do all kinds of math to make sure your scene is fully visible on all devices. FillViewport会导致部分虚拟视口大小被裁剪,因此您必须进行各种数学运算以确保场景在所有设备上均完全可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM