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. 创建远景相机,角度一般设为 ...
随机推荐
- SQL SERVER中关于OR会导致索引扫描或全表扫描的浅析 (转载)
在SQL SERVER的查询语句中使用OR是否会导致不走索引查找(Index Seek)或索引失效(堆表走全表扫描 (Table Scan).聚集索引表走聚集索引扫描(Clustered Index ...
- 为什么Sql Server的查询有时候第一次执行很慢,第二次,第三次执行就变快了
老外提问: Hi, I have an sql query which takes 8 seconds in the first run. The next run there after takes ...
- Don't Dismiss Georgia Tech's $6,600 Online Master's Degree
https://www.pcmag.com/commentary/343924/dont-dismiss-georgia-techs-6-600-online-masters-degree Don't ...
- 从PFX文件中获取私钥、公钥证书、公钥
https://blog.csdn.net/ZuoYanYouYan/article/details/77868584 该类具体功能:根据pfx证书得到私钥.根据私钥字节数组获取私钥对象.根据公钥字节 ...
- Windows server 安装和配置zabbix agent
1.下载Windows 平台的zabbix agent 先到官网下载zabbix_agentd监控客户端软件安装包(windows操作系统客户端),客户端版本尽量与服务器版本一致,下载地址:http: ...
- 解决:Windows 强制升级为8.1之后 Mysql连接不上, VisualSVN Server无服务
1.mysql 连不上,只要将mysql重新加为windows服务即可.(我的是mysql-5.6.24-winx64 解压版) 方法:mysqld --install mysql --defa ...
- 开源作业调度框架 - Quartz.NET - Cron表达式测试
昨天简单写了一下如何使用Quzrtz.NET. 那么问题来了,我设置了Cron表达式之后如何知道是表达式是否按照预期的时间执行了呢? 我找到了些Cron表达式工具生成了表达式,确发现它们基本上没有进行 ...
- 微信小程序点击 navigator ,页面不跳转
1.navigator 对应的 url 必须配置在app.json的pages中: 2.navigator 对应的 url 不能配置在"tabBar"的"list&quo ...
- bat替换文件的指定内容
需求:替换文件my.ini中的1000 为10000,bat脚本如下: c:cd C:\Program Files\MySQL\MySQL Server 5.5copy my.ini my1126ba ...
- 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...