libgdx 打包图片.atlas文件的使用:
1、
public TextureAtlas allAtlas;
allAtlas = new TextureAtlas(Gdx.files.internal("atlasImage/fruit.atlas"), Gdx.files.internal("atlasImage"));
//packFile :.atlas文件
//imagesDir: 存放打包图片的目录
public TextureAtlas (FileHandle packFile, FileHandle imagesDir) {
this(packFile, imagesDir, false);
}
2、使用:
我们可以通过findRegion方法通过文件名获得对应 AtlasRegion 对象,这个的文件名
就是生成的.atlas文件中的对应的图片名字。如:
gaming_xianshilibao_bg
rotate: false
xy: 1, 273
size: 694, 749
orig: 694, 749
offset: 0, 0
index: -1
private AtlasRegion dialog_bg = Game.assets.allAtlas.findRegion("gaming_xianshilibao_bg");
3、
3.1、
因为AtlasRegion继承自TextureRegion,
static public class AtlasRegion extends TextureRegion
我们可以draw函数中使用。
public void draw(Batch batch, float parentAlpha) {
batch.draw(dialog_bg, 13, 290, 694, 749);
super.draw(batch, parentAlpha);
}
3.2、
同样我们也可以用于按钮:
Button的构造函数需要Drawable类型的对象,我们可以通过:
new TextureRegionDrawable(Game.assets.allAtlas.findRegion("novice_package_get_normal"));
方法把AtlasRegion对象转化为TextureRegionDrawable对象,而TextureRegionDrawable对象又继承自
Drawable,这样我们就可以用于按钮了。
public Button (Drawable up, Drawable down) {
this(new ButtonStyle(up, down, null));
}