文不是直接翻译。。

本文在Libgdx的官方wiki的基础上,加上一些自己的理解。所以,难免会一些甚至是非常多的理解非常片面的东西。写的不好,还请见谅。。。。

事实上

事实上。在LibGDX的官方文档对Skin这个类的介绍中,其主要介绍了下面几个方面:

1、往Skin里面加入资源的两种方式。

2、从Skin中获取资源的方式。

第一种方式:以atlas为操作对象:

atlas = new TextureAtlas(Gdx.files.internal("meijia.atlas"), Gdx.files.internal(""));
skin = new Skin();//Skin求是就是一个资源管理类...
skin.addRegions(atlas);
region = skin.get("BG1", TextureRegion.class);

解析:对于skin.get(name,type)中的name。它与atlas文件里的资源的名字是一致的。

也就是说他与atlas.findRegion(name)中的name是一样的。

下面对skin的get(name,type)的源代码进行下面分析:

 public <T> T get (String name, Class<T> type) {//依据类型和名字返回对应的资源
//假设name(名字)或者是类型(type)为空,则抛出參数非法异常(IllegalArgumentException)和对应的提示信息
if (name == null) throw new IllegalArgumentException("name cannot be null.");
if (type == null) throw new IllegalArgumentException("type cannot be null."); //推断想要获取的类型是否是TDNS(TextureRegion、Drawable、NinePatch、Sprite)中的一种,假设是调用对应的getXXX()方法
if (type == Drawable.class) return (T)getDrawable(name);
if (type == TextureRegion.class) return (T)getRegion(name);
if (type == NinePatch.class) return (T)getPatch(name);
if (type == Sprite.class) return (T)getSprite(name); //假设不是上面的4种类型,则去资源中看一下是否有这样的类型(type)及名字(name)的资源存在.
ObjectMap<String, Object> typeResources = resources.get(type);
if (typeResources == null) throw new GdxRuntimeException("No " + type.getName() + " registered with name: " + name);
Object resource = typeResources.get(name);
if (resource == null) throw new GdxRuntimeException("No " + type.getName() + " registered with name: " + name);
return (T)resource;

小结:也就是说skin.get(name,type)这个函数主要干了这么一件事:

1)先看一下參数是否符合,假设不符合则抛出对应的异常及提示信息。

2)假设參数没有什么错误的话。就去找找type是否指TDNS(关于这个是什么意思请看上面的源代码分析)类型,假设是,则直接返回。

3)否则再去resource中找是否有类型为type,名字为name的这么一种资源。

假设没有则抛出对应的异常及提示信息

另外一种方式:以普通对象作为操作的基本对象

skin.add("color", Color.RED);
Color color = skin.getColor("color");

解释:

对于上面的“Color color = skin.getColor("color");”也许写成Color color1 = skin.get("green", Color.class);会比較符合刚開始学习的人的心态。嗯嗯。事实上getColor(name)函数实现的时候会去调get(name,Color.class)这个函数。

也就是说

Libgdx官方wiki中所提到的“Convinience methods(便利方法)”getXXX(name)函数底层都是去调对应的get(name,XXX)函数。事实胜于雄辩,源代码面前无秘密。

下面贴出libgdx中的Skin类中关于这几个函数的实现。

public Color getColor (String name) {
return get(name, Color.class);
}

public BitmapFont getFont (String name) {
return get(name, BitmapFont.class);
}

这种方法事实上完毕了这么一件事:事实上这里面是先取name为XXX的TextureRegion,假如取不到。就尝试去取name为XXX的Texture,假设能娶到。再通过取到的Texture生成TextureRegion,并存进Skin中。治愈一下的getSprite什么的,逻辑都相似

/** Returns a registered texture region. If no region is found but a texture exists with the name, a region is created from the
* texture and stored in the skin. */
public TextureRegion getRegion (String name) {
TextureRegion region = optional(name, TextureRegion.class);
if (region != null) return region; Texture texture = optional(name, Texture.class);
if (texture == null) throw new GdxRuntimeException("No TextureRegion or Texture registered with name: " + name);
region = new TextureRegion(texture);
add(name, region, Texture.class);
return region;
}

/** Returns a registered tiled drawable. If no tiled drawable is found but a region exists with the name, a tiled drawable is
* created from the region and stored in the skin. */
public TiledDrawable getTiledDrawable (String name) {
TiledDrawable tiled = optional(name, TiledDrawable.class);
if (tiled != null) return tiled; Drawable drawable = optional(name, Drawable.class);
if (tiled != null) {
if (!(drawable instanceof TiledDrawable)) {
throw new GdxRuntimeException("Drawable found but is not a TiledDrawable: " + name + ", "
+ drawable.getClass().getName());
}
return tiled;
} tiled = new TiledDrawable(getRegion(name));
add(name, tiled, TiledDrawable.class);
return tiled;
}

/** Returns a registered ninepatch. If no ninepatch is found but a region exists with the name, a ninepatch is created from the
* region and stored in the skin. If the region is an {@link AtlasRegion} then the {@link AtlasRegion#splits} are used,
* otherwise the ninepatch will have the region as the center patch. */
public NinePatch getPatch (String name) {
NinePatch patch = optional(name, NinePatch.class);
if (patch != null) return patch; try {
TextureRegion region = getRegion(name);
if (region instanceof AtlasRegion) {
int[] splits = ((AtlasRegion)region).splits;
if (splits != null) {
patch = new NinePatch(region, splits[0], splits[1], splits[2], splits[3]);
int[] pads = ((AtlasRegion)region).pads;
if (pads != null) patch.setPadding(pads[0], pads[1], pads[2], pads[3]);
}
}
if (patch == null) patch = new NinePatch(region);
add(name, patch, NinePatch.class);
return patch;
} catch (GdxRuntimeException ex) {
throw new GdxRuntimeException("No NinePatch, TextureRegion, or Texture registered with name: " + name);
}
}
/** Returns a registered sprite. If no sprite is found but a region exists with the name, a sprite is created from the region
* and stored in the skin. If the region is an {@link AtlasRegion} then an {@link AtlasSprite} is used if the region has been
* whitespace stripped or packed rotated 90 degrees. */
public Sprite getSprite (String name) {
Sprite sprite = optional(name, Sprite.class);
if (sprite != null) return sprite; try {
TextureRegion textureRegion = getRegion(name);
if (textureRegion instanceof AtlasRegion) {
AtlasRegion region = (AtlasRegion)textureRegion;
if (region.rotate || region.packedWidth != region.originalWidth || region.packedHeight != region.originalHeight)
sprite = new AtlasSprite(region);
}
if (sprite == null) sprite = new Sprite(textureRegion);
add(name, sprite, NinePatch.class);
return sprite;
} catch (GdxRuntimeException ex) {
throw new GdxRuntimeException("No NinePatch, TextureRegion, or Texture registered with name: " + name);
}
}

/** Returns a registered drawable. If no drawable is found but a region, ninepatch, or sprite exists with the name, then the
* appropriate drawable is created and stored in the skin. */
public Drawable getDrawable (String name) {
Drawable drawable = optional(name, Drawable.class);
if (drawable != null) return drawable; drawable = optional(name, TiledDrawable.class);
if (drawable != null) return drawable; // Use texture or texture region. If it has splits, use ninepatch. If it has rotation or whitespace stripping, use sprite.
try {
TextureRegion textureRegion = getRegion(name);
if (textureRegion instanceof AtlasRegion) {
AtlasRegion region = (AtlasRegion)textureRegion;
if (region.splits != null)
drawable = new NinePatchDrawable(getPatch(name));
else if (region.rotate || region.packedWidth != region.originalWidth || region.packedHeight != region.originalHeight)
drawable = new SpriteDrawable(getSprite(name));
}
if (drawable == null) drawable = new TextureRegionDrawable(textureRegion);
} catch (GdxRuntimeException ignored) {
} // Check for explicit registration of ninepatch, sprite, or tiled drawable.
if (drawable == null) {
NinePatch patch = optional(name, NinePatch.class);
if (patch != null)
drawable = new NinePatchDrawable(patch);
else {
Sprite sprite = optional(name, Sprite.class);
if (sprite != null)
drawable = new SpriteDrawable(sprite);
else
throw new GdxRuntimeException("No Drawable, NinePatch, TextureRegion, Texture, or Sprite registered with name: "
+ name);
}
} add(name, drawable, Drawable.class);
return drawable;
}

3、Skin在创建UI控件中的使用。

第一种方式:

TextButtonStyle buttonStyle = skin.get("bigButton", TextButtonStyle.class);
TextButton button = new TextButton("Click me!", buttonStyle);

另外一种方式:

TextButton button = new TextButton("Click me!", skin, "bigButton");

当没有第三个參数的时候j将默认去skin中找名为default的资源

4、拷贝Skin里面的资源

官方原话:

Resources obtained from the skin are not new instances, the same object is returned each time. If the object is modified, the changes will be reflected throughout the application. If this is not desired, a copy of the object should be made.

The newDrawable method copies a drawable. The new drawable's size information can be changed without affecting the original. The method can also tint a drawable.

也就是说,Skin里面的资源都是单例的,假设你在某一处改动了它,那么它在其它地方也会改变。

假设这不是所期望的,那么能够使用newDrawable(name,type)这个函数来拷贝Skin里面的资源。

使用方法:

//改动资源..拷贝(copy)skin里面的资源
Drawable pinkDrawable = skin.newDrawable("BG1",Color.PINK);
image = new Image(pinkDrawable);

下面是官方wiki中的部分文字的部分翻译:事实上看懂上面的东西即可了。由于官方wiki基本讲的就是这些东西。。

Overview 综述

The Skin class stores resources for UI widgets to use. It is a convenient container for texture regions, ninepatches, fonts, colors, etc. Skin also provides convenient conversions, such as retrieving a texture region as a ninepatch, sprite, or drawable.

Skin这个类主要是为UI 组件存储一些资源(Resource).它也提供了一些方便的转换。

如存进去的时候是Texture类型,取出来的时候能够直接当成TextureRegion类型去出来(事实上这里面是先取name为XXX的TextureRegion,假如取不到。就尝试去取name为XXX的Texture,假设能娶到。再通过取到的Texture生成TextureRegion,并存进Skin中)。

Skin files from the libgdx tests can be used as a starting point. You will need: uiskin.png, uiskin.atlas, uiskin.json, and default.fnt. This enables you to quickly get started using scene2d.ui and replace the skin assets later.

libgdx tests这个项目中(也就是官方提供的Gdx-tests的这个样例)的skin 的相关文件来作为学习Skin这个类的起点。

你将须要 对应的.png

.fnt    、atlas、json文件来作为学习Skin这个类的起点。他们能帮助你非常快的学习Skin。

Resources in a skin typically come from a texture atlas, widget styles and other objects defined using JSON, and objects added to the skin via code. Even when JSON is not used, it is still recommended to use Skin with a texture atlas and objects added via code. This is much more convenient to obtain instances of drawables and serves as a central place to obtain UI resources.

下面是我自己做的一个Demo:

http://download.csdn.net/detail/caihongshijie6/7610309

版权声明:本文博主原创文章,博客,未经同意不得转载。

深入了解Libgdx中间Skin分类的更多相关文章

  1. Libgdx教程目录

    Libgdx教程 Note:本教程用的Libgdx 1.9.2版本,在教程更新完毕之前应该不会更新版本.之前的博客中也发表过Libgdx的内容,不过当时都从别人那里拷贝的,因此现在想做一个Libgdx ...

  2. 【开源java游戏框架libgdx专题】-14-系统控件-Skin类

    Skin类主要用于存储用户界面的资源,该资源主要用于窗口部件.这些资源也包括纹理图片.位图画笔.颜色等内容.方便创建游戏组件,同时使用Skin也可以批量的粗略处理一些窗口部件. test.json { ...

  3. magento的robots文件编写和判断是否是一个导航分类页面

    magento是网店系统,我们突出的是我们的产品,所以,有很多路径我们不想让搜索引擎索引到,所以我们需要用robots文件进行限制 下面是麦神magento的robots.txt里面的内容,因为很多u ...

  4. 日入过百优质消除手游数据分享—萌萌哒包子脸爱消除(游戏开发引擎:libgdx)

    从2014年开始,消除游戏异常火爆,从消除小星星到腾讯的天天消除都赢得了海量用户.目前,各大市场上开心消消乐等游戏依旧火爆.消除游戏一直持续保持着女性和孩子的主流游戏地位.虽然市场上消除游戏种类很多, ...

  5. 10、Libgdx的内存管理

    (官网:www.libgdx.cn) 游戏是非常耗资源的应用.图片和音效可能耗费大量的内存,另一方面来说,这些资源没有被Java垃圾回收,让一个垃圾处理来决定将显存中的5M的图片进行释放也不是一个明知 ...

  6. CSS规范—分类方法(NEC规范学习笔记)

    一.CSS文件的分类和引用顺序 Css按照性质和用途,将Css文件分成“公共型样式”.“特殊型样式”.“皮肤型样式”,并以此顺序引用,有需要可以添加版本号 1.公共型样式:包含以下几个部分 标签的重置 ...

  7. CSS规范 - 分类方法

    CSS文件的分类和引用顺序 通常,一个项目我们只引用一个CSS,但是对于较大的项目,我们需要把CSS文件进行分类. 我们按照CSS的性质和用途,将CSS文件分成“公共型样式”.“特殊型样式”.“皮肤型 ...

  8. libgdx学习记录7——Ui

    libgdx中的UI设计主要通过其对应的Style类进行实现,也可以通过skin实现.如果没有编辑好的skin文件,可以创建一个默认的skin,再添加已经设计好的style类即可,然后在需要使用的地方 ...

  9. libgdx自制简易Flappy Bird

    Flappy Bird,好吧,无需多说.今天年初不知咋的,一下子就火了,而且直接跃居榜首,在ios和android平台都是如此,实在难以理解.传说其作者每天收入能达到5w刀,着实碉堡了... 好吧,咱 ...

随机推荐

  1. svn创建并应用补丁

    有时,我们不能提交临时局部变化,而我们需要回滚到svn最新的版本号.然,这些变化,我们要保持.提交或发送给其他同事的未来review. 怎么做?非常easy,只需要两个步骤: (1)创建一个补丁文件并 ...

  2. js+html+css简单的互动功能页面(2015知道几乎尖笔试题)http://v.youku.com/v_show/id_XMTI0ODQ5NTAyOA==.html?from=y1.7-1.2

    js+html+css实现简单页面交互功能(2015知乎前端笔试题) http://v.youku.com/v_show/id_XMTI0ODQ5NTAyOA==.html? from=y1.7-1. ...

  3. 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法

    原文:重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法 [源码下载] 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈 ...

  4. Leetcode:minimum_depth_of_binary_tree解决问题的方法

    一.     称号 并寻求最深的二元相似.给定的二进制树.求其最小深度. 最小深度是沿从根节点,到叶节点最短的路径. 二.     分析 当我看到这个题目时.我直接将最深二叉树的代码略微改了下,把ma ...

  5. 【Java GUI】Java GUI基金会

    AWT和Swing Swing是个AWT改进和扩展. 书写GUI规划.Swing和AWT曾效力.他们共存 Java基础类(JFC)于. 虽然AWT和Swing都提供了构造图形界面元素的类.但它们的虫药 ...

  6. Android - match_parent 和 fill_parent差异

    Android - match_parent 和 fill_parent差异 本文地址: http://blog.csdn.net/caroline_wendy match_parent 和 fill ...

  7. Directx11学习笔记【一】 最简单的windows程序HelloWin

    声明:本系列教程代码有部分来自dx11龙书及dx11游戏编程入门两本书,后面不再说明 首先,在vs2013中创建一个空的解决方案Dx11Demo,以后的工程都会放在这个解决方案下面.然后创建一个win ...

  8. 房费制 之 登录BUG

    声明:以下内容只有当你登录到一个username同时,学生不能申请多次登录.         说是BUG,事实上这也不是一个BUG,仅仅是想出一个办法,解决一个大家好多人都没有解决的问题.以下就给大家 ...

  9. Java知多少(4)J2SE、J2EE、J2ME的区别

    原文:Java知多少(4)J2SE.J2EE.J2ME的区别 1998年12月,SUN公司发布了Java 1.2,开始使用“Java 2” 这一名称,目前我们已经很少使用1.2之前的版本,所以通常所说 ...

  10. 11gR2 Database Services for &quot;Policy&quot; and &quot;Administrator&quot; Managed Databases (文件 ID 1481647.1)

    In this Document   _afrLoop=1459311711568804&id=1481647.1&displayIndex=6&_afrWindowMode= ...