美文网首页
第十三章:图片资源打包器(TexturePacker)

第十三章:图片资源打包器(TexturePacker)

作者: xiaolei123 | 来源:发表于2018-08-03 09:39 被阅读115次

TexturePacker介绍

1.介绍:TexturePacker 是一款把若干张资源图片拼接为一张大图的工具。
2.特点:
- 1.提高载入速度
把小图拼接成一张大图,一次载入,减少IO,提高速度
- 2. 减少内存
OpenGL ES 纹理的宽高都是需要2次幂数,在加入内存后,使用TexturePacker 打包后,它会变成一张512*512的纹理,进而减少了内存的使用。
- 3. 加快渲染速度
OpenGl ES 渲染图片,是需要使用glDrawaArray ,一张纹理调用一次,将所有纹理使用TexturePacker打包后,会减少glDrawArray的使用,进而加快渲染速度。

TexturePacker 下载使用

下载地址:https://gitee.com/xcode_xiao/LibGdxDemos2/raw/master/gdx-texturepacker.jar

原素材:


原素材

打包后:


打包后

TextureAtlas

API定义:TexturePacker封装的一个类,用于加载TexturePacker打包的图片。

功能用途:

public TextureAtlas (FileHandle packFile, FileHandle imagesDir, boolean flip) 

第一个参数:传入pack文件
第二个参数:图片路径
第三个参数:是否翻转

常用方法:

    1. public Sprite createSprite (String name)
      此方法返回一个Sprite类型的方法,默认返回pack文件中打包的第一个Region,括号内部传入的是图片的名字。
    1. public Sprite createSprite (String name, int index)
      此方法返回一个指定索引的Sprite类型的方法,因为在pack中,不同的图片名称可以相同。
    1. public Array<Sprite> createSprites (String name)
      根据传入名称,获取指定名称的图片数组。
    1. public AtlasRegion findRegion (String name)
      根据传入名字,找到符合名字第一个图片的方法
    1. public AtlasRegion findRegion (String name, int index)
      根据传入名字,获取指定索引位置的图片
    1. public Array<AtlasRegion> findRegions (String name)
      根据传入名字,获取指定名字的图片数组

提示:

public class AtlasRegion extends TextureRegion 

pack文件格式

texture_pack.png // 对应图片
format: RGBA8888
filter: Nearest,Nearest
repeat: none
shujia
  rotate: false
  xy: 1, 129
  size: 508, 370
  orig: 508, 370
  offset: 0, 0
  index: -1 // 索引
success_bg//名称
  rotate: false
  xy: 511, 255
  size: 259, 244
  orig: 259, 244
  offset: 0, 0
  index: -1// 索引

运行图片:


运行图片

DEMO地址:https://gitee.com/xcode_xiao/LibGdxDemos2/tree/master/TexturePackerDemo

相关文章

网友评论

      本文标题:第十三章:图片资源打包器(TexturePacker)

      本文链接:https://www.haomeiwen.com/subject/sylrvftx.html