【问题标题】:Drawing shapes and images in libgdx dialog在 libgdx 对话框中绘制形状和图像
【发布时间】:2014-12-21 01:20:37
【问题描述】:

我目前正在使用 libgdx 为我的 Android 游戏创建一个对话框窗口。此对话窗口包含标签和按钮的集合,但还应包含图像。图像代表“剩余生命值指示器”,即带有指示玩家生命​​值符号的空指示器。该指标必须用代表剩余生命值的彩色矩形填充(见下面的屏幕截图)。

为了在 libgdx 的对话框上呈现它,我必须绘制一个图像和一个彩色矩形(红色矩形表示实际的剩余生命值)。我知道对话框支持图像的渲染,但我不知道如何先绘制矩形。

这是我到目前为止的代码:

public FuelFacilityDialog(GameWorld world, GuiComponent gui) {
    super("Health check", gui.defaultSkin);

    this.world = world;
    this.gui = gui;

    setModal(true);
    setMovable(false);
    setResizable(false);

    Image healthIndicator = new Image();
    Button button1   = new TextButton("heal", gui.defaultSkin);
    Button button4   = new TextButton("Exit", gui.defaultSkin);

    healthIndicator.setDrawable(new TextureRegionDrawable(AssetLoader.healthIndicatorV));
    healthIndicator.setScaling(Scaling.fit);
    setObject(button1, true);
    setObject(button4, false);

    Table imageTable = new Table();
    Table buttonTable = new Table();

    imageTable.add(healthIndicator).width(100).height(200);
    buttonTable.add(button1).width(100).height(50).expandY();

    this.getContentTable().padTop(20);
    this.getContentTable().padBottom(20);
    this.getContentTable().add(imageTable);
    this.getContentTable().add(buttonTable).height(200);
    getButtonTable().add(button2);
}

【问题讨论】:

标签: java android user-interface libgdx


【解决方案1】:

我找到了问题的答案。为了绘制图像和形状,可以使用像素图。可以先创建一个像素图,在其上绘制一个矩形。然后,您创建另一个在其上绘制图像的像素图。然后可以通过将一个像素图绘制到另一个像素图上来组合两个像素图。

这是我用来构建一个图像的代码,该图像包含一个指示实际健康水平的矩形并在其顶部绘制指示图像。

private Image getHealthIndicatorImage() {
        Image indicatorImage = new Image();
        Pixmap healthIndicator = new Pixmap(AssetLoader.healthIndicatorV);
        Pixmap healthLevel = new Pixmap(healthIndicator.getWidth(), healthIndicator.getHeight(), Format.RGBA8888);

        healthLevel.setColor(Config.InstrumentPanel.healthInstrumentColor1);
        healthLevel.fillRectangle(0, 0, 50, 50);
        healthLevel.drawPixmap(healthIndicator, 0, 0);

        indicatorImage.setDrawable(new TextureRegionDrawable(new TextureRegion(new Texture(healthLevel))));
        indicatorImage.setScaling(Scaling.fit);

        return indicatorImage;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多