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 显示文字》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

矩阵计算

矩阵计算

Gene H. Golub、Charles F. Van Loan / 袁亚湘 / 人民邮电出版社 / 2009 / 89.00元

本书是国际上数值计算方面的权威著作,有“圣经”之称。被美国加州大学、斯坦福大学、华盛顿大学、芝加哥大学、中国科学院研究生院等很多世界知名学府用作相关课程的教材或主要参考书。 本书系统地介绍了矩阵计算的基本理论和方法。书中的许多算法都有现成的软件包实现,每节后还附有习题,并有注释和大量参考文献,非常有助于自学。一起来看看 《矩阵计算》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具