【问题标题】:Set size for checkbox image in libgdx UI在 libgdx UI 中设置复选框图像的大小
【发布时间】:2013-03-08 16:58:45
【问题描述】:

我不知道如何管理复选框图像大小。当然,可以在我的纹理图集中创建不同大小的图像并获取合适的图像,但我不想这样做。

这是我的代码:

AtlasRegion checkboxOn = AssetsHelper.textures.findRegion("checked");
AtlasRegion checkboxOff = AssetsHelper.textures.findRegion("unchecked");
CheckBoxStyle checkBoxStyle = new CheckBoxStyle();
checkBoxStyle.font = AssetsHelper.font66yellow;
checkBoxStyle.checkboxOff = checkboxOff;
checkBoxStyle.checkboxOn = checkboxOn;
CheckBox cbSound = new CheckBox(" Sound", checkBoxStyle);

cbSound 对象没有这样的方法来调整复选框的图像,但是有方法 getImage(),但似乎也不起作用。 这不起作用:

cbSound.getImage().width = 120;
cbSound.getImage().height = 120;

仅供参考:例如,当我想绘制图像时,我就是这样做的:

batch.draw(textureRegion, 0, 0, widthIwant, heightIwant);

但在 CheckBox 类中只有这个被覆盖(不设置宽度和高度):

public void draw (SpriteBatch batch, float parentAlpha) {
    image.setRegion(isChecked ? style.checkboxOn : style.checkboxOff);
    super.draw(batch, parentAlpha);
}

问题:如何更改复选框图像的宽度和高度?

提前致谢。

【问题讨论】:

    标签: java checkbox libgdx game-engine


    【解决方案1】:

    libgdx 小部件使用可绘制对象来绘制图像。可绘制对象会自动缩放以适合其所在的单元格。因此,要更改图像大小,请更改单元格大小:

    cbSound.getCells().get(0).size(widht, height);
    

    为了获得更好的效果,您应该为可绘制对象使用九个补丁。

    【讨论】:

    • 非常感谢,但还需要为演员cbSound.height = height 设置高度,因为如果不是这样,可点击区域就会很大(作为实际区域高度)。
    【解决方案2】:

    您需要设置图像缩放类型。方法 getImageCell 也比方法 getCells().get(0) 更正确。默认为无。

    CheckBox soundCB = new CheckBox("Sound", uiskin);
    soundCB.getImage().setScaling(Scaling.fill);
    soundCB.getImageCell().size(GameConfig.WIDTH/6);
    soundCB.left().pad(PAD);
    soundCB.getLabelCell().pad(PAD);
    //...
    content.add(soundCB).width(GameConfig.WIDTH/1.5f).row(); //add to table
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 2016-02-11
      • 1970-01-01
      • 2015-05-21
      相关资源
      最近更新 更多