libgdx——常见UI的使用

libgdx——常见UI的使用_第1张图片




libgdx——常见UI的使用_第2张图片


libgdx——常见UI的使用_第3张图片




libgdx——常见UI的使用_第4张图片


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


你可能感兴趣的:(libgdx——常见UI的使用)