(libgdx小结)常见UI的使用

以下主要介绍的是Label、Image、Button的使用,用法比较固定


直接贴代码


Label

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;

public class MyGame implements ApplicationListener {

	Stage stage;
	
	@Override
	public void create() {
		stage = new Stage();
		
		//创建一个label
		BitmapFont font = new BitmapFont(Gdx.files.internal("jd.fnt"),Gdx.files.internal("jd.png"),false);
		LabelStyle style = new LabelStyle(font,font.getColor());
		Label label = new Label("Hello Libgdx ", style);//第一个参数为label中的字体,第二个为label的style
		
		label.setPosition(50 , 150);
		label.setColor(Color.GREEN);
		
		stage.addActor(label);
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void dispose() {

	}

	@Override
	public void pause() {

	}

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

	@Override
	public void resize(int arg0, int arg1) {

	}

	@Override
	public void resume() {

	}

}


Image

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
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.Image;

public class MyGame implements ApplicationListener {

	Stage stage;
	
	Image image;
	TextureRegion region;
	Texture texture;
	
	@Override
	public void create() {
		stage = new Stage();
		texture = new Texture(Gdx.files.internal("potato.jpg"));
		region = new TextureRegion(texture,512,0,512,512);
		image = new Image(region);
		
		image.setSize(480, 320);
		image.setColor(Color.GREEN);
		image.setPosition(0,0);
		image.setOrigin(0, 0);
		image.setRotation(45f);
		
		stage.addActor(image);
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

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

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}


Button

package com.example.groupactiontest;


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.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class MyGame implements ApplicationListener {
	Stage stage;
	
	Texture texture;
	TextureRegion buttonUp;
	TextureRegion buttonDown;
	TextureRegionDrawable up;
	TextureRegionDrawable down;
    Button button;
	
	@Override
	public void create() {
		stage = new Stage();
		
		texture = new Texture(Gdx.files.internal("button.png"));
		buttonUp = new TextureRegion(texture, 0, 0,120,120);
		buttonDown = new TextureRegion(texture,120,0,120,120);
		up = new TextureRegionDrawable(buttonUp);
		down = new TextureRegionDrawable(buttonDown);
		button = new ImageButton(up, down);
		
		stage.addActor(button);
		Gdx.input.setInputProcessor(stage);
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

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

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}


二、效果图








源码下载:

http://download.csdn.net/detail/caihongshijie6/7005985

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值