libgdx中的UI设计主要通过其对应的Style类进行实现,也可以通过skin实现。如果没有编辑好的skin文件,可以创建一个默认的skin,再添加已经设计好的style类即可,然后在需要使用的地方直接调用skin会更加方便。

本文主要简单介绍Label和TextButton这两种比较常见的UI组件,代码如下。

 package com.fxb.newtest;

 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.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
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.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; public class Lib002_Ui implements ApplicationListener{ Stage stage;
LabelStyle labelStyle;
Label label, label2;
BitmapFont font;
ButtonStyle buttonStyle;
TextButtonStyle textbuttonStyle;
Button button;
TextButton textbutton, textbutton2; Skin skin; @Override
public void create() {
// TODO Auto-generated method stub stage = new Stage();
skin = new Skin(); Pixmap pixmap = new Pixmap( , , Format.RGBA8888 );
pixmap.setColor( Color.DARK_GRAY );
pixmap.fill();
Texture texture = new Texture( pixmap );
Drawable draw1 = new TextureRegionDrawable( new TextureRegion( texture ) ); pixmap.setColor( Color.GRAY );
pixmap.fill();
texture = new Texture( pixmap );
Drawable draw2 = new TextureRegionDrawable( new TextureRegion( texture ) ); font = new BitmapFont();
labelStyle = new LabelStyle( font, Color.GREEN );
labelStyle.background = draw1; label = new Label( "Very Good!", labelStyle );
label.setAlignment( Align.right );
//label.setSize( 100, 50 );
label.setBounds( , , , ); skin.add( "gray", texture, Texture.class );
skin.add( "gray", labelStyle, LabelStyle.class ); label2 = new Label( "Label2", skin, "gray" );
label2.setSize( , );
label2.setPosition( , ); buttonStyle = new ButtonStyle( draw1, draw2, null );
textbuttonStyle = new TextButtonStyle( draw1, draw2, null, font );
textbuttonStyle.fontColor = Color.CYAN; textbutton = new TextButton( "TextButton", textbuttonStyle );
textbutton.setBounds( , , , ); skin.add( "gray", textbuttonStyle, TextButtonStyle.class );
textbutton2 = new TextButton( "TextButton2", skin, "gray" );
textbutton2.setBounds( , , , ); stage.addActor( label );
stage.addActor( label2 );
stage.addActor( textbutton );
stage.addActor( textbutton2 );
Gdx.input.setInputProcessor( stage );
} @Override
public void resize(int width, int height) {
// TODO Auto-generated method stub } @Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( , , , );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); stage.act();
stage.draw();
} @Override
public void pause() {
// TODO Auto-generated method stub } @Override
public void resume() {
// TODO Auto-generated method stub } @Override
public void dispose() {
// TODO Auto-generated method stub
stage.dispose();
skin.dispose();
} }

运行效果:

libgdx学习记录7——Ui-LMLPHP

05-07 14:55