【问题标题】:scene2d button scaling with libgdx使用 libgdx 进行scene2d 按钮缩放
【发布时间】:2014-05-11 15:43:00
【问题描述】:

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

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);

作为最后的手段,我想我可以尝试创建自己的可以缩放的文本按钮 actor,感谢您抽出宝贵时间。

【问题讨论】:

    标签: android button libgdx scaling scene2d


    【解决方案1】:

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

    如果你真的想的话,无论如何你可以用 TableLayout 来缩放图片按钮,看例子:

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

    【讨论】:

      【解决方案2】:

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

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

      一个例子是:

      //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 使用表格,并根据自己的喜好对其进行缩放,而与其他游戏项目无关;

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          您可以在ButtonStyle 中设置Drawable 的大小来调整Button 的大小:

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

          欲了解更多信息,请参阅official document for layout

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

          你可以在source code中找到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;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-11-15
            • 2015-09-04
            • 1970-01-01
            • 1970-01-01
            • 2014-08-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多