2013-03-12 16 views
4

我試圖使用Nine Patch作爲Libgdx Scene2d UI按鈕的背景。它正在加載,但它真的很醜。我可以看到「元數據」的像素,並且如果它只是一個普通的圖像被拉伸(按鈕上的文字是「繼續」):將9-patch圖像作爲Libgdx Scene2d加載按鈕背景看起來很糟糕

ugly image of a nine-patch button

我加載了.9.png通過(libgdx)文件直接導入(libgdx)NinePatchDrawableNinePatch這樣的:

this.dialogButtonUp = new NinePatchDrawable(
    new NinePatch(new Texture(Gdx.files.internal("data/button-round.9.png")))); 
this.dialogButtonDown = new NinePatchDrawable(
    new NinePatch(new Texture(Gdx.files.internal("data/button-round-down.9.png")))); 

然後我做一個TextButtonStyle描述按鈕,並引用兩個NinePatch繪項目:

TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle(); 
buttonStyle.font = aValidFontReally; 
buttonStyle.fontColor = Color.BLACK; 
buttonStyle.up = this.dialogButtonUp; 
buttonStyle.down = this.dialogButtonDown; 
buttonStyle.pressedOffsetX = -2; 

我間接地建立按鈕,通過Dialog箱:

new Dialog(...).button("Continue", null, buttonStyle); 

我已檢查.9.png文件,以確保:

  • 該資產的文件是在刷新Eclipse
  • 元數據邊界像素是完全不可見的或完全可見的黑色
  • 表示Android draw9patch工具可以加載圖像並驗證它們

有關要檢查或更改什麼的其他建議?

+0

嘗試使用NinePatch.draw()與SpriteBatch一起繪製每個NinePatch作爲至少會隔離問題的出處,即如果問題出現,那麼問題不在於NinePatch。 – 2013-03-12 12:41:51

+0

好點!我只是試了一下,並用直接的'NinePatch.draw(...)'看到了同樣的醜陋。所以'對話框'和'按鈕'不是在拼湊它。 – 2013-03-12 19:09:22

+2

看看NinePatch的源代碼(https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/NinePatch.java)我不認爲它的確如此你使用帶有一個Texture參數的構造函數。嘗試使用這個構造函數:public NinePatch(Texture texture,int left,int right,int top,int bottom) – 2013-03-12 20:12:01

回答

10

感謝來自@RodHyde的一些指針,它看起來像libgdx NinePatch類被設計爲接受一個「後處理」的九個補丁紋理(即用單獨的整數值描述如何將單個紋理剪切成補丁) 。這種「處理」通常是將「.9.png」文件打包到TextureAtlas(參見https://github.com/libgdx/libgdx/wiki/Texture-packer#ninePatches)的副作用。紋理地圖集是一個非常好的主意(特別是當你的用戶界面包含一堆不同的紋理元素時),所以這很有意義,但是在開發和嘗試運行某些東西時有點令人驚訝。

要變通此,所以我可以直接包含「.9.png」文件我寫了這個:

private static NinePatch processNinePatchFile(String fname) { 
    final Texture t = new Texture(Gdx.files.internal(fname)); 
    final int width = t.getWidth() - 2; 
    final int height = t.getHeight() - 2; 
    return new NinePatch(new TextureRegion(t, 1, 1, width, height), 3, 3, 3, 3); 
} 

這將加載紋理,創建一個子區域是剪掉了1個像素元 - 數據邊界,然後只是猜測9個邊界元素是3個像素寬/高。 (通過在紋理數據中進行正確的計算似乎是可行的,但不值得花費 - 只需將紋理放在圖集中即可)

+2

NinePatch javadoc得到了改進,使之更清晰:https://github.com/libgdx/libgdx/commit/5ce8dc04153234c760b3753fe3a6b4e6fe1653ea#gdx/src/ COM/badlogic/GDX /圖形/ G2D/NinePatch.java – 2013-03-13 22:29:10

相關問題