android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
来源:互联网 发布:java文件夹选择器 编辑:程序博客网 时间:2024/05/16 16:55
libgdx的UI改进很大,原来各种稀奇古怪的问题都已经解决了,而且UI的类型也基本上完全了。推荐大家下载最近的版本使用。
UI的使用我觉得唯一复杂的就是各种样式的制定,比如TextButton:
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:
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
先看看基本格式:
{
resources: {
className: {
name: value,
...
},
...
},
styles: {
className: {
name: value,
...
},
...
}
}
由两个大块定义:资源和样式。先做个fnt文件以便支持中文resources: {
className: {
name: value,
...
},
...
},
styles: {
className: {
name: value,
...
},
...
}
}
图片分享:
com.badlogic.gdx.graphics.g2d.NinePatch: {
default-rect-red: [
{ width: 2, height: 1, x: 1, y: 43 },
{ width: 1, height: 1, x: 2, y: 43 },
{ width: 2, height: 1, x: 3, y: 43 },
{ width: 2, height: 1, x: 1, y: 45 },
{ width: 1, height: 1, x: 2, y: 45 },
{ width: 2, height: 1, x: 3, y: 45 },
{ width: 2, height: 1, x: 1, y: 43 },
{ width: 1, height: 1, x: 2, y: 43 },
{ width: 2, height: 1, x: 3, y: 43 }
],
default-rect-yellow: [
{ width: 2, height: 1, x: 1, y: 54 },
{ width: 1, height: 1, x: 2, y: 54 },
{ width: 2, height: 1, x: 3, y: 54 },
{ width: 2, height: 1, x: 1, y: 55 },
{ width: 1, height: 1, x: 2, y: 55 },
{ width: 2, height: 1, x: 3, y: 55 },
{ width: 2, height: 1, x: 1, y: 54 },
{ width: 1, height: 1, x: 2, y: 54 },
{ width: 2, height: 1, x: 3, y: 54 }
]
}
default-rect-red: [
{ width: 2, height: 1, x: 1, y: 43 },
{ width: 1, height: 1, x: 2, y: 43 },
{ width: 2, height: 1, x: 3, y: 43 },
{ width: 2, height: 1, x: 1, y: 45 },
{ width: 1, height: 1, x: 2, y: 45 },
{ width: 2, height: 1, x: 3, y: 45 },
{ width: 2, height: 1, x: 1, y: 43 },
{ width: 1, height: 1, x: 2, y: 43 },
{ width: 2, height: 1, x: 3, y: 43 }
],
default-rect-yellow: [
{ width: 2, height: 1, x: 1, y: 54 },
{ width: 1, height: 1, x: 2, y: 54 },
{ width: 2, height: 1, x: 3, y: 54 },
{ width: 2, height: 1, x: 1, y: 55 },
{ width: 1, height: 1, x: 2, y: 55 },
{ width: 2, height: 1, x: 3, y: 55 },
{ width: 2, height: 1, x: 1, y: 54 },
{ width: 1, height: 1, x: 2, y: 54 },
{ width: 2, height: 1, x: 3, y: 54 }
]
}
再定义一个白色:
com.badlogic.gdx.graphics.Color: {
white: { a: 1, b: 1, g: 1, r: 1 }
}
white: { a: 1, b: 1, g: 1, r: 1 }
}
然后我们的字体:
com.badlogic.gdx.graphics.g2d.BitmapFont: {
default-font: { file: chinese.fnt }
}
default-font: { file: chinese.fnt }
}
然后在样式中声明一个Label样式:
com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
default: { font: default-font, fontColor: white}
}
default: { font: default-font, fontColor: white}
}
使用时
Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"), Gdx.files.internal("ui/ui.jpg"));//加载样式
final Label label = new Label("FPS:", skin.getStyle("default",LabelStyle.class), "fpsLabel");//获取Label样式
final Label label = new Label("FPS:", skin.getStyle("default",LabelStyle.class), "fpsLabel");//获取Label样式
效果:
图片分享:
然后再来试试TextButton:
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: { down: default-rect-red, up: default-rect-yellow,fontColor: white, font: default-font}
}
default: { down: default-rect-red, up: default-rect-yellow,fontColor: white, font: default-font}
}
调用:
final TextButton textButton =new TextButton("确认", skin.getStyle("default", TextButtonStyle.class), "okButton");
效果:
图片分享:
按下去的时候会变黄(没截起图)…
Skin真的用着很方便,特别是你大量使用了libgdx的UI的时候。
写在最后:
1.Skin支不支持xml?
说实话我也很想知道,因为gdx-tests里面skin 的配置文件有两个,一个是json格式,另外一个是xml格式。但是我试了一下直接加载xml会报错。
其实我比较推荐用xml格式,因为Json格式书写时候容易写错(少个逗号或者括号什么的),而且eclipse的自动缩进没有发挥作用(难道是配置问题?)。
2.Skin支持不一个配置文件多个图片文件,这个情况推荐将图片合并或者干脆用多个Skin就行了。
3.Skin的图片定义的原点是左上角。
- android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
- android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
- Libgdx的使用(11)——Skin和UI配置文件的使用
- android游戏开发框架libgdx的使用(五)--舞台和常用UI类
- android游戏开发框架libgdx的使用(五)--舞台和常用UI类
- Android游戏开发框架libgdx的使用
- Android游戏开发框架libgdx的使用
- android游戏开发框架libgdx的使用(九)—在libgdx中使用Box2d
- android游戏开发框架libgdx的使用(九)—在libgdx中使用Box2d
- android游戏开发框架libgdx的使用(十三)—TiledMap中的角色和角色移动
- android游戏开发框架libgdx的使用(十三)—TiledMap中的角色和角色移动
- android游戏开发框架libgdx的使用(十三)—TiledMap中的角色和角色移动
- Libgdx Skin的使用
- android游戏开发框架libgdx的使用(十六)—使用TexturePacker工具加快开发速度
- android游戏开发框架libgdx的使用(十六)—使用TexturePacker工具加快开发速度
- android游戏开发框架libgdx的使用(十八)—简单的AVG游戏效果实现
- android游戏开发框架libgdx的使用(十二)—TiledMap地图的使用
- android游戏开发框架libgdx的使用(十八)—资源加载管理AssetManager的使用
- 上班不想干活的时候 。。。。
- 注意java 字符串split的坑哦
- Git 常用命令 和使用方法
- perl关于乱码问题!
- C# 画面中PictureBox 画面闪烁
- android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
- 实现一个函数,对一个正整数n,算得到1
- android游戏开发框架libgdx的使用(十二)—TiledMap地图的使用
- 软件工程之七——软件维护
- 一些Ipa plist收集
- Ubuntu12.04 root用户登录设置
- android游戏开发框架libgdx的使用(十三)—TiledMap中的角色和角色移动
- java下Class.forName的作用是什么,为什么要使用它
- Fcitx4.x安装问题总结