Android Libgdx 显示文字

栏目: Android · 发布时间: 7年前

内容简介:Libgdx有两种显示文字的方式:第一种:通过贴图的方式显示,使用BitmapFont和SpriteBatch组合来完成文字的绘制,构造BitmapFont时需要一个描述文字构成的fnt文件,和一个提供文字图片的png文件。具体的可以看看这个教程。

Libgdx有两种显示文字的方式:

第一种:

通过贴图的方式显示,使用BitmapFont和SpriteBatch组合来完成文字的绘制,构造BitmapFont时需要一个描述文字构成的fnt文件,和一个提供文字图片的png文件。具体的可以看看这个教程。

另一种:

直接使用ttf文件,就是FreeType方式。这里有个教程。但是这个教程的版本比较旧了,新版的libgdx1.9.8版本是使用gradle方式集成。具体的api也与上面的教程有所变化。变化后的使用方式如下。

首先,在项目的build.gradle文件中引入:

……
ext {
        ……
        gdxVersion = '1.9.8'
        ……
    }

project(":android") {
    apply plugin: "android"
    apply plugin: "kotlin-android"

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
……
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    }
}


project(":core") {
    apply plugin: "kotlin"


    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
……
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    }
}
复制代码

然后写代码:

private var font: BitmapFont? = null
        private var generator: FreeTypeFontGenerator? = null
    
        var freeTypeFontParameter = FreeTypeFontGenerator.FreeTypeFontParameter()
        freeTypeFontParameter.color = Color.BLACK
        freeTypeFontParameter.size = 40
        ……//还有一些其他的属性可以设置
        freeTypeFontParameter.characters = DEFAULT_CHARS + "你需要的文字,不能重复,都写在这里"
        font = generator!!.generateFont(freeTypeFontParameter)



        style.font = font
复制代码

后面就是正常的步骤


以上所述就是小编给大家介绍的《Android Libgdx 显示文字》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Django

Pro Django

Marty Alchin / Apress / 2008-11-24 / USD 49.99

Django is the leading Python web application development framework. Learn how to leverage the Django web framework to its full potential in this advanced tutorial and reference. Endorsed by Django, Pr......一起来看看 《Pro Django》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具