Android Libgdx 显示文字

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
复制代码

后面就是正常的步骤

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值