(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

	}

}


二、效果图

(libgdx小结)常见UI的使用_第1张图片




(libgdx小结)常见UI的使用_第2张图片



源码下载:

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

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