android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用

转自:http://www.apkbus.com/forum.php?mod=viewthread&tid=19769&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26orderby%3Ddateline

 

libgdx的UI改进很大,原来各种稀奇古怪的问题都已经解决了,而且UI的类型也基本上完全了。推荐大家下载最近的版本使用。

UI的使用我觉得唯一复杂的就是各种样式的制定,比如TextButton:

  1. public TextButtonStyle (NinePatch down, NinePatch up, NinePatch checked, float pressedOffsetX, float pressedOffsetY,float unpressedOffsetX, float unpressedOffsetY, BitmapFont font, Color fontColor, Color downFontColor,Color checkedFontColor)
复制代码

再看看List:

  1. public ListStyle (BitmapFont font, Color fontColorSelected, Color fontColorUnselected, NinePatch selectedPatch)
复制代码
每次使用都需要实例化若干个Texture,NinePatch什么的还是有点麻烦,还好libgdx给出了一个解决方案:Skin。


 

Skin保存了UI的样式和相关的资源,定义使用的是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文件以便支持中文20120110152058414.jpg

2012-1-17 21:45 上传
下载附件(70.67 KB)



保持为chinese.fnt和chinese.png。再做张图:
201201101521005723.jpg

2012-1-17 21:45 上传
下载附件(1.77 KB)



全部拷贝到项目文件中(我是新建了一个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. final Label label = new Label("FPS:", skin.getStyle("default",LabelStyle.class), "fpsLabel"); //获取Label样式
复制代码
效果:
20120110152106602.jpg
2012-1-17 21:45 上传
下载附件(15.87 KB)
然后再来试试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. }
复制代码

调用:

  1. final TextButton textButton = new TextButton("确认", skin.getStyle("default", TextButtonStyle.class), "okButton");
复制代码
效果:
201201101521102681.jpg
2012-1-17 21:45 上传
下载附件(18.91 KB)
按下去的时候会变黄(没截起图)…


 

Skin真的用着很方便,特别是你大量使用了libgdx的UI的时候。


 

写在最后:
1.Skin支不支持xml?
说实话我也很想知道,因为gdx-tests里面skin 的配置文件有两个,一个是json格式,另外一个是xml格式。但是我试了一下直接加载xml会报错。
其实我比较推荐用xml格式,因为Json格式书写时候容易写错(少个逗号或者括号什么的),而且eclipse的自动缩进没有发挥作用(难道是配置问题?)。
2.Skin支持不一个配置文件多个图片文件,这个情况推荐将图片合并或者干脆用多个Skin就行了。
3.Skin的图片定义的原点是左上角。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值