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() {
}
}