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的更多相关文章

  1. libgdx学习记录11——平铺地图TiledMap

    地图对于游戏场景十分重要,很多游戏都需要对地图进行编辑,可使用TileMap进行编辑并生成对应的tmx格式地图文件. 编辑好后,可通过TmxMapLoader来读取地图文件.可通过一个正交相机Otho ...

  2. libgdx学习记录6——动作Action

    libgdx中的Action类能够有效的帮助我们实现位移.旋转.缩放.淡入淡出等效果,对游戏的设计很有用. Action是一个抽象类,本身不可以实例化.一般使用的它的继承类,常用的有 MoveToAc ...

  3. libgdx学习记录5——演员Actor

    Actor也是libgdx中非常重要的一个元素,一般与stage配合一起使用.Actor能够设置大小,位置,旋转和动画等. 我们自定义的Actor一般需要继承于Actor,并且重写其中的act和dra ...

  4. libgdx学习记录4——舞台Stage

    libgdx总的来说是一个框架,而不是一个成熟的游戏引擎.Stage是其中一个比较好的封装,里面自带Camera.SpriteBatch等常用渲染绘图工具. 下面是一个简单的添加图片,并让镜头左右上下 ...

  5. libgdx学习记录2——文字显示BitmapFont

    libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...

  6. libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...

  7. libgdx学习记录26——Polygon多边形碰撞检测

    libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...

  8. libgdx学习记录23——图片移动选择

    模拟移动选择图片,采用相机实现. package com.fxb.newtest; import com.badlogic.gdx.ApplicationAdapter; import com.bad ...

  9. libgdx学习记录22——3d物体创建

    libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...

随机推荐

  1. UI第二组与数据库对接时遇到的问题记录

    此为组内某一位做UI的同学的随笔. 之前的app由于没有加入数据库,所以每次重新启动里面的东西都会回到初始状态,即不能保存内容.我们的数据库小组已经很棒地基本完成了数据库的工作,于是我就准备加入数据库 ...

  2. sklearn——数据集调用及应用

    忙了许久,总算是又想起这边还没写完呢. 那今天就写写sklearn库的一部分简单内容吧,包括数据集调用,聚类,轮廓系数等等.   自带数据集API 数据集函数 中文翻译 任务类型 数据规模 load_ ...

  3. 阿里云rds实例恢复到本地

    摘要: 前提: 1,阿里云数据库备份实例,恢复数据的时候需要将数据恢复到本地数据库,是不能直接恢复到RDS上的. 2,需要在本地服务器上下载一个数据库,尽量和RDS数据库版本保持一致.(我现在用的是5 ...

  4. Jmeter中自动重定向与跟随重定向的区别

    一.重定向就是通过各种方法将各种网络请求重新定个方向转到其它位置. 二.我们在网站建设中,时常会遇到需要网页重定向的情况: 1.网站调整(如改变网页目录结构): 2.网页被移到一个新地址: 3.网页扩 ...

  5. 【Redis数据库】命令学习笔记——发布订阅、事务、脚本、连接等命令汇总

    本篇基于redis 4.0.11版本,学习发布订阅.事务.脚本.连接的相关命令. Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 序号 ...

  6. ngrep命令用法

    ngrep 是grep(在文本中搜索字符串的工具)的网络版,他力求更多的grep特征,用于搜寻指定的数据包.正由于安装ngrep需用到libpcap库, 所以支持大量的操作系统和网络协议.能识别TCP ...

  7. Web Services 根据wsdl生成代理类

    生成代理类步骤: 一:找到Visual Studio 的工具文件夹 二:用管理员方式打开本机工具命令提示 三:输入要执行的脚本 wsdl /language:C# /n:xxxx.HermesMobi ...

  8. linux 设备驱动加载的先后顺序

    Linux驱动先注册总线,总线上可以先挂device,也可以先挂driver,那么究竟怎么控制先后的顺序呢. 1.初始化宏 Linux系统使用两种方式去加载系统中的模块:动态和静态. 静态加载:将所有 ...

  9. 大话存储 1 - 走进计算机IO世界

    组成计算机的三大件:CPU,内存和IO. 1 总线 总线就是一条或者多条物理上的导线,每个部件都接到这些导线上,同一时刻只能有一个部件在接收或者发送. 仲裁总线:所有部件按照另一条总线,也就是仲裁总线 ...

  10. python基础学习15----异常处理

    异常处理,是编程语言或计算机硬件里的一种机制,用于处理软件或信息系统中出现的异常状况(即超出程序正常执行流程的某些特殊条件). 1.异常的类型 异常的类型多种多样,常见的异常有: AttributeE ...