libgdx——常见UI的使用












label

package com.doodle.uitest;

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 FirstGame implements ApplicationListener {

	private Stage stage;
	private BitmapFont font;
	private LabelStyle style;
	private Label label;
	
	@Override
	public void create() {
		font = new BitmapFont(Gdx.files.internal("hhjd.fnt"),Gdx.files.internal("hhjd.png"),false);//file,image,flip(是否翻转)
		
		style = new LabelStyle(font,font.getColor());//所使用的字符库,和字符的颜色
		label = new Label("Hello potato",style);//两个参数分别为:要显示的内容,labelstyle
		stage = new Stage(400,320,false);
		
		label.setPosition(50, 150);
		label.setFontScale(2);
		label.setColor(Color.GREEN);
		
		stage.addActor(label);
		
		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

	}

}


iamge

package com.doodle.uitest;

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.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;

public class FirstGame implements ApplicationListener {

	private Stage stage;
	private Image image;
	private Texture texture;
	private TextureRegion region;
	
	@Override
	public void create() {
		stage = new Stage(480,320,false);
		Gdx.input.setInputProcessor(stage);
		
		texture = new Texture(Gdx.files.internal("image2.jpg"));
		region = new TextureRegion(texture,0,0,512,512);
		image = new Image(region);
		
		image.setColor(Color.PINK);//设置图片的颜色
		image.setScale(0.5f);//设置图片的缩放大小
		image.setPosition(230, 40);//设置图片的位置
		image.setOrigin(0, 0);//设置旋转中心
		image.setRotation(45);//设置旋转角度
		image.setSize(300, 300);//设置大小
		
		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.doodle.uitest;

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.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class FirstGame implements ApplicationListener {

	private Stage stage;
	
	private TextureRegionDrawable up;
	private TextureRegionDrawable down;
	private TextureRegion buttonUp;
	private TextureRegion buttonDown;
	private Texture text;
	private ImageButton button;
	
	
	@Override
	public void create() {
		stage = new Stage(480,320,false);
		Gdx.input.setInputProcessor(stage);
		
		text = new Texture(Gdx.files.internal("image2.jpg"));
		TextureRegion[][] temp = TextureRegion.split(text, 120, 120);
		
		buttonUp = temp[0][0];
		buttonDown = temp[0][1];
		
		up = new TextureRegionDrawable(buttonUp);
		down = new TextureRegionDrawable(buttonDown);
		
		button = new ImageButton(up, down);
		
		stage.addActor(button);
		
	}

	@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

	}

}

本系列demo的源码下载链接:http://download.csdn.net/detail/caihongshijie6/6986861


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

帅气的东哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值