Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用libgdx进行scene2d按钮缩放_Android_Button_Libgdx_Scaling_Scene2d - Fatal编程技术网

Android 使用libgdx进行scene2d按钮缩放

Android 使用libgdx进行scene2d按钮缩放,android,button,libgdx,scaling,scene2d,Android,Button,Libgdx,Scaling,Scene2d,我不知道是不是只有我一个人,但我很困惑。它们是为了保持给定的大小,还是有办法在其中缩放图像?我知道他们可以使用ninepatch通过拉伸图像来填充某些区域,但我想缩放它拉伸的图像。我正在使用 对于我的菜单按钮,但它们太大了,我想知道如何缩放它们。我正在从一个图集中检索皮肤,图集中有ninepatch图像。 以下是设置屏幕: 以下是我正在使用的软件包中的图像: 以下是TextureAtlas和蒙皮的初始化: buttonAtlas = new TextureAtlas(Gdx.files.inte

我不知道是不是只有我一个人,但我很困惑。它们是为了保持给定的大小,还是有办法在其中缩放图像?我知道他们可以使用ninepatch通过拉伸图像来填充某些区域,但我想缩放它拉伸的图像。我正在使用 对于我的菜单按钮,但它们太大了,我想知道如何缩放它们。我正在从一个图集中检索皮肤,图集中有ninepatch图像。 以下是设置屏幕: 以下是我正在使用的软件包中的图像: 以下是TextureAtlas和蒙皮的初始化:

buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
buttonSkin = new Skin(buttonAtlas); 
这是该菜单按钮的初始化

TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = Assets.buttonSkin.getDrawable("bluebutton");
textButtonStyle.down = Assets.buttonSkin.getDrawable("redbutton");
textButtonStyle.font = font;

buttonMenu = new TextButton("Menu", textButtonStyle);
buttonMenu.pad(20.0f);
buttonMenu.addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        game.fade.startFadeOut();
        super.clicked(event, x, y);
    }
});
也许我可以改变我把它放在桌子上的方式,或者我把桌子放在舞台上的方式

table.add(buttonMenu).width(Gdx.graphics.getWidth()/4);
stage.addActor(table);

作为最后手段,我想我可以尝试创建我自己的文本按钮演员,我可以缩放,谢谢您的时间。

首先,如果您知道您的图像太大:最好将图像本身缩放到较小的尺寸,然后使用TexturePacker生成包含较小图像的新TextureAtlas

不管怎样,如果您确实愿意,可以使用TableLayout缩放图像按钮,请参见示例:

table.add(buttonMenu).width(100).height(100);

上面的答案对我不起作用,但我有一个解决办法

您可以制作一个辅助摄影机并将其连接到舞台上。 要缩放表格,只需缩放辅助摄影机的视口大小

例如:

//Heres where you set the scale of your buttons and your table
Camera secondaryCamera = new Camera(Gdx.graphics.getWidth() * scale, 
                                    Gdx.graphics.getHeight() * scale); 


Stage stage = new Stage();
Table table = new Table();
ImageButton button = new ImageButton(drawableUp, drawableDown, drawableChecked);
table.setFillParent(true);

stage.getViewport().setCamera(secondaryCamera);

table.add(ImageButton);
stage.addActor(table);
当您将其用于触摸控制时,效果非常好。你可以为整个UI使用一个表,并根据自己的喜好进行缩放,而不依赖于其他游戏项目

表格。添加(按钮菜单)。宽度(100)。高度(100)
应该可以工作,您只需根据自己的喜好调整
宽度
高度
参数,也可以使用
设置边界
方法。以下是一个例子:

    TextButton myButton = new TextButton("Press Me", style);
    myButton.setBounds(xCoordinate, yCoordinate, width, height);

您可以在
ButtonStyle
中设置
Drawable
的大小,以调整
按钮的大小:

float scale = 0.5f;
float currentHeight = textButtonStyle.up.getMinHeight();
textButtonStyle.up.setMinHeight(currentHeight * scale);
有关详细信息,请参阅:

UI小部件不设置自己的大小和位置。相反,父窗口小部件设置每个子窗口的大小和位置小部件提供了父部件可以用作提示的最小、首选和最大大小。一些父部件(如表和容器)可以在如何调整子部件的大小和位置方面受到限制。为了在布局中为小部件指定一个特定的大小,小部件的最小、首选和最大大小将被保留,并且大小约束将在父级中设置

您可以在以下位置找到
按钮.getPrefHeight()
的实现:

public float getPrefHeight () {
    float height = super.getPrefHeight();
    if (style.up != null) height = Math.max(height, style.up.getMinHeight());
    if (style.down != null) height = Math.max(height, style.down.getMinHeight());
    if (style.checked != null) height = Math.max(height, style.checked.getMinHeight());
    return height;
}