加载TTF文件时出错 - Libgdx

时间:2017-06-24 16:04:01

标签: java android libgdx true-type-fonts

我用LibgdGdx创建了一个android项目,我创建了一个AssetManager类,在那里我加载了我需要的所有资源,但是当我运行项目时,我在加载ttf文件时出错。 AssetManager的代码:

 public AssetManager manager;

public AssetsManager(){

    manager = new AssetManager();
    loadAssets();
}


public void loadAssets(){
    loadTtf("assets/Birds.TTF"); 
}

void loadTtf(String path){
    FileHandleResolver resolver = new InternalFileHandleResolver();
    manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
    manager.setLoader(BitmapFont.class, "assets/Birds.TTF", new FreetypeFontLoader(resolver));

    FreetypeFontLoader.FreeTypeFontLoaderParameter font = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
    font.fontFileName = path;
    font.fontParameters.size = 20;
    manager.load(path , BitmapFont.class, font);
}

我尝试通过此代码加载ttf文件

BitmapFont font = manager.manager.get("assets/Birds.TTF",BitmapFont.class);

我遇到的部分错误:

com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: assets/Birds.TTF (Internal)
                                                               at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:579)
                                                               at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:380)
                                                               at com.poum.game.Main.render(Main.java:33)
                                                               at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
                                                               at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1649)
                                                               at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1354)

正如我所说,该项目是在Android上运行的

谢谢你的一切

1 个答案:

答案 0 :(得分:0)

String path="Birds.TTF";     //can be inside nested folder
String fileName = "Birds.TTF" ;   // it can be any name with extension, will use to load and retrieve

以这种方式加载:

manager=new AssetManager();
FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, ".TTF", new FreetypeFontLoader(resolver));

FreetypeFontLoader.FreeTypeFontLoaderParameter parms = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
parms.fontFileName = path;    // path of .TTF file where that exist 
parms.fontParameters.size = 20;
manager.load(fileName, BitmapFont.class, parms);   // fileName with extension, sameName will use to get from manager

manager.finishLoading();  //or use update() inside render() method

AssetManager

中检索字体
BitmapFont font=manager.get(fileName,BitmapFont.class);

修改

在屏幕截图中,我的文件名为Birds.ttf,而非Birds.TTF

所以改变

String path = "Birds.ttf";
String fileName = "Birds.ttf"

Android文件系统区分大小写。

桌面模块的运行配置应如下所示:

enter image description here