6

我不知道是不是只有我,但是drawables让我很困惑。他们是打算保持给定的大小,还是有办法在其中缩放图像?我知道他们可以使用 Ninepatch 通过拉伸图像来填充某些区域,但我想缩放它拉伸的图像。我正在为我的菜单按钮使用TextButton ,但它们太大了,我很想知道如何缩放它们。我正在从地图集中检索皮肤,其中包含九个补丁图像。这是设置屏幕: 设置画面 这是我正在使用的包中的图像: 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);

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

4

4 回答 4

1

First of all if you know that your images are too big: The best would be to scale the images themselves to a smaller size and then use the TexturePacker too generate a new TextureAtlas that contain the smaller images.

Anyhow you can scale the image button with the TableLayout if you really want to, see example:

table.add(buttonMenu).width(100).height(100);
于 2014-06-17T18:21:10.157 回答
0

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

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

一个例子是:

//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 使用一个表格,并根据您的喜好将其缩放到独立于您的其他游戏项目;

于 2014-09-01T06:00:30.300 回答
0

您可以设置DrawableinButtonStyle的大小来调整您的大小Button

float scale = 0.5f;
float currentHeight = textButtonStyle.up.getMinHeight();
textButtonStyle.up.setMinHeight(currentHeight * scale);

有关更多信息,请参阅官方文档布局

UI 小部件不设置自己的大小和位置。相反,父窗口小部件设置每个孩子的大小和位置。小部件提供父级可以用作提示的最小、首选和最大大小。一些父窗口小部件,例如 Table 和 Container,可以限制子窗口的大小和位置。要在布局中为小部件指定特定尺寸,小部件的最小、首选和最大尺寸将保持不变,并且在父级中设置尺寸约束。

你可以Button.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;
}
于 2017-05-14T13:52:07.043 回答
0

table.add(buttonMenu).width(100).height(100);应该可以工作,你只需要根据自己的喜好调整widthheight参数,或者你可以使用该setBounds方法。这是一个例子:

    TextButton myButton = new TextButton("Press Me", style);
    myButton.setBounds(xCoordinate, yCoordinate, width, height);
于 2016-03-20T07:00:55.290 回答