在libgdx UI中设置复选框图像的大小

时间:2013-03-19 20:02:14

标签: java checkbox libgdx game-engine

我无法弄清楚如何管理复选框图片大小。当然,可以在我的纹理图集中创建不同尺寸的图像并采取适当的图像,但我不想这样做。

这是我的代码:

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

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

提前致谢。

2 个答案:

答案 0 :(得分:8)

libgdx小部件正在使用drawable来绘制图像。 drawable会自动缩放以适合它所在的单元格。因此,为了更改图像大小,请更改单元格大小:

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

为了获得更好的效果,您应该为drawable使用九个补丁。

答案 1 :(得分:0)

您需要设置图像缩放类型。同样,方法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
相关问题