帶有 9patch 的 LibGdx 標籤背景 (LibGdx label background with 9patch)


問題描述

帶有 9patch 的 LibGdx 標籤背景 (LibGdx label background with 9patch)

所以我遇到了這個我根本無法解決的問題。我正在 LibGdx 的幫助下製作遊戲,並嘗試創建聊天氣泡功能。問題是,當我嘗試將標籤樣式的背景更改為 9patch 可繪製對象時,它不能很好地縮放它,或者根本不能縮放?

public class ChatBubble
{
    private Label textLabel;
    private BitmapFont font;
    private Label.LabelStyle lStyle;
    private int scaledWidth = 0;
    private int scaledHeight = 0;
    private Timer.Task currentTask;
    private Texture bkg;

    public ChatBubble()
    {
        font = new BitmapFont();
        font.setColor(Color.BLACK);
        bkg = new Texture("data/ui/chatb.9.png");

        NinePatch np = new NinePatch(bkg,11,11,9,10);
        NinePatchDrawable npd = new NinePatchDrawable(np);
        lStyle = new Label.LabelStyle(font,font.getColor());
        lStyle.background = npd;

        textLabel = new Label("",lStyle);
        textLabel.setVisible(false);
        textLabel.setAlignment(Align.center);
        currentTask = new Timer.Task() {
            @Override
            public void run() {
                textLabel.setVisible(false);
            }};
    }

    public void show(String text, float duration)
    {
        if(currentTask.isScheduled())currentTask.cancel();
        textLabel.setText(text);
        textLabel.setVisible(true);
        scaledHeight = (int)textLabel.getPrefHeight();
        scaledWidth = (int)textLabel.getWidth()/2;
        Timer.schedule(currentTask,duration);
    }

    public void show(String text)
    {
        if(currentTask.isScheduled())currentTask.cancel();
        textLabel.setText(text);
        textLabel.setVisible(true);
        scaledHeight = (int)textLabel.getPrefHeight();
        scaledWidth = (int)textLabel.getWidth()/2;
        Timer.schedule(currentTask,(float)(text.length()*0.1));
    }

    public void draw(SpriteBatch batch, float x, float y)
    {
        if(!textLabel.isVisible())return;
        textLabel.setPosition(x ‑ scaledWidth, y + scaledHeight);
        batch.begin();
        textLabel.draw(batch, 1);
        batch.end();
    }
}

它看起來如何:遊戲中的樣子

9batch 的樣子:9patch

任何幫助將不勝感激!

更新:我發現我的 9patch 可以縮放,問題在於標籤未更新調用 setText() 時的大小,


參考解法

方法 1:

Call .pack() on the label after .setText() to tell it to size itself to its text (plus whatever padding there is in the background drawable). You don't need to call layout() since that's handled automatically.

I'm not sure the exact reason you have to manually call pack(), but this is generally the case with Widgets that you are not children of a WidgetGroup subclass (i.e. Table, VerticalGroup, etc.).

(by Alen ZarkovicTenfour04)

參考文件

  1. LibGdx label background with 9patch (CC BY‑SA 2.5/3.0/4.0)

#libgdx #java #Android #nine-patch #label






相關問題

為什麼原生 libmpg123 在帶有 libgdx 的 android 上花費這麼長時間? (Why is native libmpg123 taking so long on android with libgdx?)

睡眠後重新加載應用程序 (Re-load the app after sleep)

獲取實際觸摸位置 LibGDX (Get actual touch position LibGDX)

LibGDX - 只有可拖動的運動 (LibGDX - only draggable movement)

無法解析符號“android” - 在使用 libgdx 在 Android 應用程序中實現 Google Analytics 時 (Cannot resolve symbol 'android' - While implementing Google Analytics in Android App with libgdx)

如何從矩形數組中刪除隨機矩形? (How to remove random rectangle from rectangle array?)

球在傾斜平面上滾動 java libgdx (Ball rolling on an incline plane java libgdx)

libgdx - Intellij 類未找到異常? (libgdx - Intellij class not found exception?)

GL_COLOR_BUFFER_BIT 再生哪個內存? (GL_COLOR_BUFFER_BIT regenerating which memory?)

libGDX - 更改屏幕後的黑色按鈕和文本 (libGDX - Black buttons and text after changing screen)

帶有 9patch 的 LibGdx 標籤背景 (LibGdx label background with 9patch)

如何從平鋪中刪除對象? (How do I remove an object from tiled?)







留言討論