libgdx: 打包图片.atlas文件的使用

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));
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值