我在游戏中设置缩放效果时遇到问题。所以我有 Stage
对象,它由 ScrollPane
填充,其中包括 Table
和几个 Actor's
。我希望能够使用手势放大和缩小该表格的元素(并且滚动条调整到新的大小)。我尝试过 stage.getCamera().zoom ,但是它改变了整个 ScrollPane 的大小,这是我不想要的。我想出了改变该表中所有参与者的大小的想法,但我不确定这是否是最有效的方法。有人可以帮我解决这个问题吗?
最佳答案
我认为最好的方法是,就像你说的那样,更改滚动 Pane 中表格的大小。实际上,我最终扩展了堆栈并更改了堆栈中表格的比例,然后我有了另一个实际大小的表格。我的应用程序需要它:
public ZoomableStack() {
imageTable = new Table();
sizeTable = new Table();
this.add(imageTable);
this.add(sizeTable);
layout();
imageTable.setTransform(true);
}
获取表格的缩放尺寸:
@Override
public float getPrefWidth() {
return imageTable.getWidth() * imageTable.getScaleX();
}
@Override
public float getPrefHeight() {
return imageTable.getHeight() * imageTable.getScaleY();
}
像这样重写布局方法:
@Override
public void layout() {
float width = this.getWidth();
float height = this.getHeight();
imageTable.setSize(imageTable.getPrefWidth(), imageTable.getPrefHeight());
float tableWidth = imageTable.getWidth();
float tableHeight = imageTable.getHeight();
imageTable.setOrigin(tableWidth / 2, tableHeight / 2);
imageTable.setPosition((width / 2 - (tableWidth / 2)), (height / 2 - (tableHeight / 2)));
sizeTable.setBounds(((width - getPrefWidth()) / 2), ((height - getPrefHeight()) / 2), getPrefWidth(), getPrefHeight());
}
您可能需要重写 setCullingArea(),因为表格最终会比屏幕大,然后表格中的所有 Actor 都会消失:
@Override
public void setCullingArea(Rectangle cullingArea) {
}
然后创建缩放方法,在其中调用 invalidateHierarchy():
public void zoom(float amount) {
imageTable.scaleBy(amount);
invalidateHierarchy();
}
然后,您只需将堆栈添加到滚动 Pane 中并使用缩放即可:
ZoomableStack zoomableStack = new ZoomableStack();
new ScrollPane(zoomableStack);
zoomableStack.zoom(speed * amount);
关于java - 如何在 ScrollPane 中进行缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37767509/