libgdx学习记录7——Ui
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的更多相关文章
- libgdx学习记录11——平铺地图TiledMap
地图对于游戏场景十分重要,很多游戏都需要对地图进行编辑,可使用TileMap进行编辑并生成对应的tmx格式地图文件. 编辑好后,可通过TmxMapLoader来读取地图文件.可通过一个正交相机Otho ...
- libgdx学习记录6——动作Action
libgdx中的Action类能够有效的帮助我们实现位移.旋转.缩放.淡入淡出等效果,对游戏的设计很有用. Action是一个抽象类,本身不可以实例化.一般使用的它的继承类,常用的有 MoveToAc ...
- libgdx学习记录5——演员Actor
Actor也是libgdx中非常重要的一个元素,一般与stage配合一起使用.Actor能够设置大小,位置,旋转和动画等. 我们自定义的Actor一般需要继承于Actor,并且重写其中的act和dra ...
- libgdx学习记录4——舞台Stage
libgdx总的来说是一个框架,而不是一个成熟的游戏引擎.Stage是其中一个比较好的封装,里面自带Camera.SpriteBatch等常用渲染绘图工具. 下面是一个简单的添加图片,并让镜头左右上下 ...
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- libgdx学习记录3——动画Animation
libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...
- libgdx学习记录26——Polygon多边形碰撞检测
libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...
- libgdx学习记录23——图片移动选择
模拟移动选择图片,采用相机实现. package com.fxb.newtest; import com.badlogic.gdx.ApplicationAdapter; import com.bad ...
- libgdx学习记录22——3d物体创建
libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...
随机推荐
- Python 3前言
Python具有简单.易学.免费.开源.可移植.可扩展.可嵌入.面向对象等优点,它的面向对象甚至比java和C#.net更彻底. 作为一种通用语言,Python几乎可以用在任何领域和场合,角色几乎是无 ...
- CSS揭秘(二)背景与边框
Chapter2 背景与边框 1. 半透明边框 基础:了解 RGBA & HSLA 颜色(色调 0~360.饱和度.亮度 (0%黑色~100%白色).透明度) 默认情况下,背景在边框的下层,容 ...
- 使用 PowerShell 创建 Azure VM 的自定义映像
自定义映像类似于应用商店映像,不同的是自定义映像的创建者是你自己. 自定义映像可用于启动配置,例如预加载应用程序.应用程序配置和其他 OS 配置. 在本教程中,你将创建自己的 Azure 虚拟机自定义 ...
- Oracle EBS AR应收核销取值
AR_RECEIVABLE_APPLICATIONS APP, AR_CASH_RECEIPTS CR, AR_PAYMENT_SCHEDULES PS_INV, HZ_CUST_ACCOUNTS C ...
- web.config中的InProc模式 与 StateServer模式[转]
开发asp.net应用时,修改web.config中的SessionState节点. <sessionState mode="StateServer" stateConnec ...
- ASP.NET动态引用样式表(css)和脚本(js)文件
// 引入js文件 HtmlGenericControl scriptControl = new HtmlGenericControl("script"); scriptContr ...
- python基础学习5----字典
字典由大括号和键值对组成,特点为无序,键唯一 1.字典的创建 #直接创建字典 dic1={'name':'a','age':20} #通过dict创建字典,输出都为{'name': 'a', 'age ...
- OpenResty 安装配置
0. 说明 1. Windows 下安装 下载软件包 openresty-1.13.6.1-win32.zip ,解压即可食用. [开启] 直接运行 nginx.exe 在 Windows 的命令窗口 ...
- CentOS7 配置静态 ip
1. 为 CentOS7 配置静态 ip 1.1 修改文件/etc/sysconfig/network-scripts/ifcfg-ens33 sudo vi /etc/sysconfig/netwo ...
- 【转】Java学习---volatile 关键字
[原文]https://www.toutiao.com/i6591422029323305480/ 前言 不管是在面试还是实际开发中 volatile 都是一个应该掌握的技能. 首先来看看为什么会出现 ...