自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android游戲引擎libgdx使用教程11:Skin和UI配置文件

移動開發(fā) Android
ibgdx的UI改進很大,原來各種稀奇古怪的問題都已經(jīng)解決了,而且UI的類型也基本上完全了。推薦大家下載最近的版本使用。

ibgdx的UI改進很大,原來各種稀奇古怪的問題都已經(jīng)解決了,而且UI的類型也基本上完全了。推薦大家下載最近的版本使用。

UI的使用我覺得唯一復雜的就是各種樣式的制定,比如TextButton:

  1. public TextButtonStyle (NinePatch down, NinePatch up, NinePatch checked, float pressedOffsetX, 
  2. float pressedOffsetY,float unpressedOffsetX, float unpressedOffsetY, 
  3. BitmapFont font, Color fontColor, Color downFontColor,Color checkedFontColor)   

再看看List:

public ListStyle (BitmapFont font, Color fontColorSelected, Color fontColorUnselected, NinePatch selectedPatch)

每次使用都需要實例化若干個Texture,NinePatch什么的還是有點麻煩,還好libgdx給出了一個解決方案:Skin。

Skin保存了UI的樣式和相關(guān)的資源,定義使用的是Json或者Json-like。API地 址:http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/scenes/scene2d/ui /Skin.html

先看看基本格式:

  1. {     
  2. resources: {     
  3. className: {     
  4. name: value,     
  5. ...     
  6. },     
  7. ...     
  8. },     
  9. styles: {     
  10. className: {     
  11. name: value,     
  12. ...     
  13. },     
  14. ...     
  15. }     
  16. }     

由兩個大塊定義:資源和樣式。先做個fnt文件以便支持中文。

Android游戲引擎libgdx使用教程11:如何使用Skin和UI配置文件

保持為chinese.fnt和chinese.png。再做張圖:

Android游戲引擎libgdx使用教程11:如何使用Skin和UI配置文件

全部拷貝到項目文件中(我是新建了一個UI文件夾)。我們先寫個Label試試。定義需要的NinePath:

  1. com.badlogic.gdx.graphics.g2d.NinePatch: {    
  2. default-rect-red: [    
  3. { width: 2, height: 1, x: 1, y: 43 },    
  4. { width: 1, height: 1, x: 2, y: 43 },    
  5. { width: 2, height: 1, x: 3, y: 43 },    
  6. { width: 2, height: 1, x: 1, y: 45 },    
  7. { width: 1, height: 1, x: 2, y: 45 },    
  8. { width: 2, height: 1, x: 3, y: 45 },    
  9. { width: 2, height: 1, x: 1, y: 43 },    
  10. { width: 1, height: 1, x: 2, y: 43 },    
  11. { width: 2, height: 1, x: 3, y: 43 }    
  12. ],    
  13. default-rect-yellow: [    
  14. { width: 2, height: 1, x: 1, y: 54 },    
  15. { width: 1, height: 1, x: 2, y: 54 },    
  16. { width: 2, height: 1, x: 3, y: 54 },    
  17. { width: 2, height: 1, x: 1, y: 55 },    
  18. { width: 1, height: 1, x: 2, y: 55 },    
  19. { width: 2, height: 1, x: 3, y: 55 },    
  20. { width: 2, height: 1, x: 1, y: 54 },    
  21. { width: 1, height: 1, x: 2, y: 54 },    
  22. { width: 2, height: 1, x: 3, y: 54 }    
  23. ]    
  24. }   

再定義一個白色:

  1. com.badlogic.gdx.graphics.Color: {    
  2. white: { a: 1, b: 1, g: 1, r: 1 }    
  3. }   

然后我們的字體:

  1. com.badlogic.gdx.graphics.g2d.BitmapFont: {    
  2. default-font: { file: chinese.fnt }    
  3. }   

然后在樣式中聲明一個Label樣式:

  1. com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {    
  2. default: { font: default-font, fontColor: white}    
  3. }   

使用時:

  1. Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"), Gdx.files.internal("ui/ui.jpg")); //加載樣式   
  2.    
  3. final Label label = new Label("FPS:", skin.getStyle("default",LabelStyle.class), "fpsLabel"); //獲取Label樣式   

效果:

Android游戲引擎libgdx使用教程11:如何使用Skin和UI配置文件

然后再來試試TextButton:

  1. com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {    
  2. default: { down: default-rect-red, up: default-rect-yellow,fontColor: white, font: default-font}    
  3. }   

 調(diào)用:

  1. final TextButton textButton = new TextButton("確認", skin.getStyle("default", TextButtonStyle.class), "okButton");   

效果:

Android游戲引擎libgdx使用教程11:如何使用Skin和UI配置文件

 

按下去的時候會變黃(沒截起圖)…

Skin真的用著很方便,特別是你大量使用了libgdx的UI的時候。

寫在最后:

1、Skin支不支持xml?

說實話我也很想知道,因為gdx-tests里面skin 的配置文件有兩個,一個是json格式,另外一個是xml格式。但是我試了一下直接加載xml會報錯。

其實我比較推薦用xml格式,因為Json格式書寫時候容易寫錯(少個逗號或者括號什么的),而且eclipse的自動縮進沒有發(fā)揮作用(難道是配置問題?)。

2、Skin支持不一個配置文件多個圖片文件,這個情況推薦將圖片合并或者干脆用多個Skin就行了。

3、Skin的圖片定義的原點是左上角。

責任編輯:閆佳明 來源: jizhuomi
相關(guān)推薦

2013-12-04 13:30:45

Android游戲引擎libgdx教程

2013-12-04 17:14:57

Android游戲引擎libgdx教程

2013-12-06 10:12:49

Android游戲引擎libgdx教程

2013-12-06 10:31:14

Android游戲引擎libgdx教程

2013-12-06 09:59:53

Android游戲引擎libgdx教程

2013-12-04 16:28:29

Android游戲引擎libgdx教程

2013-12-04 17:27:10

Android游戲引擎libgdx教程

2013-12-06 10:22:42

Android游戲引擎libgdx教程

2013-12-04 16:07:27

Android游戲引擎libgdx教程

2013-12-04 16:21:02

Android游戲引擎libgdx教程

2010-03-25 18:31:03

Nginx配置文件

2011-07-18 11:07:12

iPhone 游戲 引擎

2011-07-18 10:53:09

2011-07-18 12:29:10

2011-07-18 11:23:29

iPhone 游戲 動畫

2011-07-18 11:39:58

iPhone 游戲 引擎

2010-12-27 14:59:31

Outlook 配置文

2022-11-10 09:05:18

Lua配置文件

2021-07-05 12:09:58

Python編程語言

2009-06-24 14:17:00

BackingBeanJSF配置文件
點贊
收藏

51CTO技術(shù)棧公眾號