【问题标题】:TrueType Fonts in libGDXlibGDX 中的 TrueType 字体
【发布时间】:2012-03-18 04:34:42
【问题描述】:

有谁知道我如何在 libGDX 中使用 TTF 字体?我环顾四周,看到了有关 StbTrueTypeFont 的信息,但它似乎不在最新版本中。

编辑:我找到了 StbTrueType 字体的东西,jar 文件位于扩展目录中。我已将其添加到我的项目中。现在我只需要弄清楚如何使用它。有什么例子吗?

【问题讨论】:

  • 我在哪里可以得到这个库文件?我到处找。我发现的唯一地方(没用)是:code.google.com/p/libgdx/source/browse/trunk/extensions/…code.google.com/p/libgdx/source/browse/trunk/extensions/…
  • 转到此处:libgdx.badlogicgames.com/nightlies 并下载“libgdx-nightly-latest.zip”,您将在扩展目录中找到这些 jar。
  • 我需要使用哪些罐子?我试图添加“gdx-stb-truetype.jar”,但它一直在给我例外,例如“javax.media.opengl.GLException: java.lang.NoClass DefFoundError: com/badlogic/gdx/utils /SharedLibraryLoader ..."
  • 您需要gdx-stb-truetype-natives.jargdx-stb-truetype.jar。将它们都添加到您的主项目中,并确保将它们添加到您的构建路径中。
  • 仍然有同样的错误。我什至尝试使用桌面项目想要的其他最新库,但它不起作用(没有错误,但我看不到文本和其他一些东西):gdx-backend-jogl-natives.jar gdx -backend-jogl.jar gdx-natives.jar gdx-sources.jar gdx-stb-truetype-natives.jar gdx-stb-truetype.jar gdx.jar

标签: java android fonts truetype libgdx


【解决方案1】:

是的,您肯定需要将 gdx-stb-truetype jar 添加到您的项目中,正如您在编辑中所述。以下是您将如何使用它,非常简单......

首先您需要声明您的BitmapFont 以及您将使用的字符...

BitmapFont font;
public static final String FONT_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789][_!$%#@|\\/?-+=()*&.;,{}\"´`'<>";

那么你需要创建字体...

font = TrueTypeFontFactory.createBitmapFont(Gdx.files.internal("font.ttf"), FONT_CHARACTERS, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
font.setColor(1f, 0f, 0f, 1f);

您可以使用传递给createBitmapFont() 的参数,您将看到它们的作用。

然后像平常一样渲染字体...

batch.begin();
font.draw(batch, "This is some text", 10, 10);
batch.end();

【讨论】:

  • 如何将此字体与com.badlogic.gdx.scenes.scene2d.ui.Label 等ui 元素一起使用?还是不可能?
  • @uni 与任何BitmapFont 一样,您可以通过在Label 构造函数中传递LabelStyle 来申请Label。在LabelStyle 中可以指定字体。 libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/…
【解决方案2】:

使用 gdx-freetype 扩展:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
BitmapFont font15 = generator.generateData(15);
BitmapFont font22 = generator.generateData(22);
generator.dispose();

"要使用 gdx-freetype,获取最新的 nightlies,将 gdx-freetype.jargdx-freetype-natives.jar 链接到您的桌面项目,将 gdx-freetype.jar 链接到您的 Android 项目,然后将 armeabi/libgdx-freetype.soarmeabi-v7a/libgdx-freetype.so 文件复制到您的Android 项目的 libs/ 文件夹,就像 libgdx.so 文件一样。”

来源:http://www.badlogicgames.com/wordpress/?p=2300

【讨论】:

【解决方案3】:

做了很多研究,发现了这个工作方法:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("myfont.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12; // font size
BitmapFont font12 = generator.generateFont(parameter);
generator.dispose(); // avoid memory leaks, important

如果您在上面尝试过的答案失败,请使用此选项, libGDX Freetype Wiki供参考。

【讨论】:

    【解决方案4】:

    这适用于 S4 克隆手机: Pinewood 是下载的字体,位于 assets 文件夹中。请参阅下面的文件夹结构。

    import android.util.Log;
    
    import com.badlogic.gdx.ApplicationListener;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.files.FileHandle;
    import com.badlogic.gdx.graphics.GL10;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.BitmapFont;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
    
    public class Game implements ApplicationListener {
        private SpriteBatch mSpriteBatch;
        private Texture textureBackground;
        private BitmapFont mBitmapFont;
    
        public void create() {
    
            Gdx.graphics.setContinuousRendering(false);
            Gdx.graphics.requestRendering();
    
            mSpriteBatch = new SpriteBatch();
    
            Texture.setEnforcePotImages(false);
            textureBackground = new Texture(Gdx.files.internal("background.jpg"));
    
            FileHandle fontFile = Gdx.files.internal("Pinewood.ttf");
            FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
    
            mBitmapFont = generator.generateFont(150);
            generator.dispose();
    
            mBitmapFont.setColor(0.9f, 0.5f, 0.5f, 1); 
            Log.e("Game", "mBitmapFont.getScaleX() : "+mBitmapFont.getScaleX() + ", mBitmapFont.getScaleY() "+mBitmapFont.getScaleY());
    
        }
    
        public void render() {
            Log.e("Game", "render()");
    
            Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
            mSpriteBatch.begin();
            // Drawing goes here!
            mSpriteBatch.draw(textureBackground, 0, 0);
            mBitmapFont.draw(mSpriteBatch, "FPS:"+Gdx.graphics.getFramesPerSecond(), 110, 260);
            mSpriteBatch.end();     
        }
    
        public void resize(int width, int height) {
        }
    
        public void pause() {
        }
    
        public void resume() {
        }
    
        public void dispose() {
        }
    
    }
    

    【讨论】:

      【解决方案5】:

      您必须使用 FreeTypeFont 库。 github

      1.正如 github 页面所说,对于使用 gradle 的项目,请添加以下依赖项:

      核心依赖:

      compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
      

      桌面依赖:

      compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
      

      Android 依赖:

      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"
      

      更多详情和平台见this页面

      如果您不使用 gradle,请参阅 this

      2.将库添加到您的项目后,您可以像这样使用它:

      FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("myfont.ttf"));
      FreeTypeFontParameter parameter = new FreeTypeFontParameter();
      parameter.size = 12;
      BitmapFont font12 = generator.generateFont(parameter); // font size 12 pixels
      generator.dispose(); // don't forget to dispose to avoid memory leaks!
      

      并将 myfont.ttf 放入 core assets 文件夹

      【讨论】:

        猜你喜欢
        • 2013-05-06
        • 1970-01-01
        • 2015-02-01
        • 2016-05-30
        • 1970-01-01
        • 2011-03-11
        • 1970-01-01
        • 2019-12-10
        • 2012-11-26
        相关资源
        最近更新 更多