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 2014 新特性——内存数据库(转载)
目录 SQL Server 2014 新特性——内存数据库 简介: 设计目的和原因: 专业名词 In-Memory OLTP不同之处 内存优化表 内存优化表的索引 并发能力的提升 和竞争对手相比几点 ...
- PowerDesigner Code和Name设置大写tablespace设置,PK设置
1,PowerDesigner Code和Name设置大写 tool>MODEL OPTIONS 2.从oracle数据库导出的表结构默认包含了tablespace 删除tablespace方法 ...
- 如何在EF Core 使用存储过程
使用EF Core框架能快速的帮助我们进行常规的数据处理和项目开发,但是ORM虽然好用,但是在许多复杂逻辑的数据处理时,我个人还是偏向用SQL和存储过程的方式去处理,但是研究了一下目前最新版本的EF ...
- search文件中的config
config文件中出现这句话时,代表该部分search应写成文件中封装好的search部分.即: var search = null; this.search = search; se ...
- MSCRM2016 取消邮箱强制SSL
在新建电子邮件服务器配置文件时Advanced中的Use SSL for Incoming/Outgoing Connection默认都是启用的而且无法编辑,启用SSL当然是为了安全的考虑,但当客户的 ...
- DLL动态链接库导出函数方法 -- 动态导出(.def文件导出)
简介 动态链接库最大的优势在于可以提供给其他应用程序共享的资源,最小化应用程序代码的复杂度,其中一个十分重要的功能就是dll可以导出封装函数的功能.导出函数有两种主要方式,分别是静态导入和动态导入,本 ...
- 【8】python文件的读写方法
(1).读文件的步骤: (1)打开文件 open(path,flag,encoding,[errors]) path:打开路径 flag:打开方式 r(只读) rb(二进制格式) r+(可以读写) w ...
- LeetCode 休闲计划
老年退役选手的 LeetCode 休闲之旅 前言 不知不觉两年多的大学时光悄然流逝,浑浑噩噩的状态似乎从来没有离开过自己. 这两年刷题似乎一直是常态.在退役之后的现在,深感有些东西一段时间没有接触,很 ...
- Django商城项目笔记No.3用户部分-用户模型类
Django商城项目笔记No.3用户部分-用户模型类 Django提供了认证系统,文档资料https://yiyibooks.cn/xx/Django_1.11.6/topics/auth/index ...
- Docker技术入门与实战 第二版-学习笔记-4-Dockerfile外其他生成镜像的方法
其它生成镜像的方法 即除了标准地使用Dockerfile来生成镜像外,还有一些其他的方法 1)从 rootfs 压缩包导入 格式:docker import [选项] <文件>|<U ...