libgdx学习记录7——Ui
libgdx中的UI设计主要通过其对应的Style类进行实现,也可以通过skin实现。如果没有编辑好的skin文件,可以创建一个默认的skin,再添加已经设计好的style类即可,然后在需要使用的地方直接调用skin会更加方便。
本文主要简单介绍Label和TextButton这两种比较常见的UI组件,代码如下。
1 package com.fxb.newtest; 2 3 import com.badlogic.gdx.ApplicationListener; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.graphics.Color; 6 import com.badlogic.gdx.graphics.GL10; 7 import com.badlogic.gdx.graphics.Pixmap; 8 import com.badlogic.gdx.graphics.Pixmap.Format; 9 import com.badlogic.gdx.graphics.Texture; 10 import com.badlogic.gdx.graphics.g2d.BitmapFont; 11 import com.badlogic.gdx.graphics.g2d.TextureRegion; 12 import com.badlogic.gdx.scenes.scene2d.Stage; 13 import com.badlogic.gdx.scenes.scene2d.ui.Button; 14 import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle; 15 import com.badlogic.gdx.scenes.scene2d.ui.Label; 16 import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; 17 import com.badlogic.gdx.scenes.scene2d.ui.Skin; 18 import com.badlogic.gdx.scenes.scene2d.ui.TextButton; 19 import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; 20 import com.badlogic.gdx.scenes.scene2d.utils.Align; 21 import com.badlogic.gdx.scenes.scene2d.utils.Drawable; 22 import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; 23 24 public class Lib002_Ui implements ApplicationListener{ 25 26 Stage stage; 27 LabelStyle labelStyle; 28 Label label, label2; 29 BitmapFont font; 30 ButtonStyle buttonStyle; 31 TextButtonStyle textbuttonStyle; 32 Button button; 33 TextButton textbutton, textbutton2; 34 35 Skin skin; 36 37 @Override 38 public void create() { 39 // TODO Auto-generated method stub 40 41 stage = new Stage(); 42 skin = new Skin(); 43 44 Pixmap pixmap = new Pixmap( 1, 1, Format.RGBA8888 ); 45 pixmap.setColor( Color.DARK_GRAY ); 46 pixmap.fill(); 47 Texture texture = new Texture( pixmap ); 48 Drawable draw1 = new TextureRegionDrawable( new TextureRegion( texture ) ); 49 50 pixmap.setColor( Color.GRAY ); 51 pixmap.fill(); 52 texture = new Texture( pixmap ); 53 Drawable draw2 = new TextureRegionDrawable( new TextureRegion( texture ) ); 54 55 font = new BitmapFont(); 56 labelStyle = new LabelStyle( font, Color.GREEN ); 57 labelStyle.background = draw1; 58 59 label = new Label( "Very Good!", labelStyle ); 60 label.setAlignment( Align.right ); 61 //label.setSize( 100, 50 ); 62 label.setBounds( 10, 0, 100, 50 ); 63 64 skin.add( "gray", texture, Texture.class ); 65 skin.add( "gray", labelStyle, LabelStyle.class ); 66 67 label2 = new Label( "Label2", skin, "gray" ); 68 label2.setSize( 100, 50 ); 69 label2.setPosition( 120, 0 ); 70 71 buttonStyle = new ButtonStyle( draw1, draw2, null ); 72 textbuttonStyle = new TextButtonStyle( draw1, draw2, null, font ); 73 textbuttonStyle.fontColor = Color.CYAN; 74 75 textbutton = new TextButton( "TextButton", textbuttonStyle ); 76 textbutton.setBounds( 0, 100, 100, 50 ); 77 78 skin.add( "gray", textbuttonStyle, TextButtonStyle.class ); 79 textbutton2 = new TextButton( "TextButton2", skin, "gray" ); 80 textbutton2.setBounds( 150, 100, 100, 50 ); 81 82 stage.addActor( label ); 83 stage.addActor( label2 ); 84 stage.addActor( textbutton ); 85 stage.addActor( textbutton2 ); 86 Gdx.input.setInputProcessor( stage ); 87 } 88 89 @Override 90 public void resize(int width, int height) { 91 // TODO Auto-generated method stub 92 93 } 94 95 @Override 96 public void render() { 97 // TODO Auto-generated method stub 98 Gdx.gl.glClearColor( 1, 1, 1, 1 ); 99 Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); 100 101 stage.act(); 102 stage.draw(); 103 } 104 105 @Override 106 public void pause() { 107 // TODO Auto-generated method stub 108 109 } 110 111 @Override 112 public void resume() { 113 // TODO Auto-generated method stub 114 115 } 116 117 @Override 118 public void dispose() { 119 // TODO Auto-generated method stub 120 stage.dispose(); 121 skin.dispose(); 122 } 123 124 }
运行效果: