Libgdx中ImageButton的使用

ImageButton有个构造是
ImageButton(Drawable imageUp, Drawable imageDown)
TextRegionDrawable 是Drawable的子类
TextRegionDrawable有这样一个构造:
TextureRegionDrawable(TextureRegion region)
以下实现步骤:准备一张图
在这里插入图片描述
对该图片进行裁剪.而后将图片由textregion------>textregiondrawable
以下是代码
package com.me.mygdxgame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class MyGdxGame implements ApplicationListener {

private ImageButton imb;
private Texture tex;
private TextureRegionDrawable up;
private TextureRegionDrawable down;
private Stage stage;

@Override
public void create() {
	tex = new Texture(Gdx.files.internal("data/button.png"));
	TextureRegion[][] temp=TextureRegion.split(tex, 120, 120);
	TextureRegion trUp=temp[0][0];
	TextureRegion trDown=temp[0][1];
	up = new TextureRegionDrawable(trUp);
	down = new TextureRegionDrawable(trDown);
	imb = new ImageButton(up, down);
	stage = new Stage(480,320,false);
	stage.addActor(imb);
	Gdx.input.setInputProcessor(stage);
}

@Override
public void dispose() {
	
}

@Override
public void render() {		
	Gdx.gl.glClearColor(1, 1, 1, 1);
	Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	stage.act();
	stage.draw();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值