Java libgdx绘制汉字
我喜欢在我的应用程序中打印中文文本 1.当我尝试此操作时,屏幕将为空。控制台上没有错误 创建方法:Java libgdx绘制汉字,java,android,fonts,libgdx,chinese-locale,Java,Android,Fonts,Libgdx,Chinese Locale,我喜欢在我的应用程序中打印中文文本 1.当我尝试此操作时,屏幕将为空。控制台上没有错误 创建方法: FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF")); font = gen.generateFont(40, "好", false); FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40, "好", false);
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40);
渲染方法:
spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "好", 10, 100);
spriteBatch.end();
spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "asd", 10, 100);
spriteBatch.end();
2.当我试着这样做时,屏幕上会出现3个不同的汉字,但我不知道为什么这些汉字会出现在哪里。asd和这三个字符之间没有联系
创建方法:
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40, "好", false);
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40);
渲染方法:
spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "好", 10, 100);
spriteBatch.end();
spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "asd", 10, 100);
spriteBatch.end();
有人知道如何正确地在libgdx中绘制汉字吗(我使用的是libgdx的当前版本)?
例如:
你好吗倪浩马你 好 吗?
问候
编辑: 下面是一个完整的示例,它将在屏幕上显示预期的汉字。 我已经从这里下载了字体: 转到并获取BMFont,然后使用它查看您的字体。还可以使用它预生成libgdx位图字体文件。问题是您的字体不是unicode字体 当您键入Java代码时“好" 符号被转换为U+597D。您使用的字体ttf文件是一种老式的非unicode字体。这些旧字体文件的工作方式是用不同的图片替换像“a”这样的字母。“a”是U+61。因此,在您的程序中,字母“a”被转换为U+61,后者映射为汉字符号,但好“值为U+597D的情况下不存在 您可以继续使用字体并查找每个字符的位置,以便使用正确的数字。不要使用“a”,而是使用(char)0x61之类的字符,这样就不那么容易混淆了。或者
只需获取包含以下内容的有效unicode字体“好“U+597D处的符号。libGDX中的freetype加载程序实际上创建了一个
BitmapFont
。为此,它需要一个字符列表。默认列表仅为字母数字。要呈现unicode,您需要用所需的unicode字符替换/附加该列表。”
下面的代码应该有一些帮助(我正在使用AssetManager
):
我也发现了。这是我的答案:
package com.wang.libgdx.test.my;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.wang.libgdx.test.utils.GdxTest;
public class TtfFontTest extends GdxTest{
SpriteBatch spriteBatch;
BitmapFont font;
@Override
public void create() {
spriteBatch = new SpriteBatch();
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.characters=FreeTypeFontGenerator.DEFAULT_CHARS + "你好";
parameter.size = 24;
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/ui/fangzheng.ttf"));
font = generator.generateFont(parameter);
generator.dispose();
}
@Override
public void dispose() {
spriteBatch.dispose();
font.dispose();
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
font.draw(spriteBatch, "helloworld", 300,300);
font.draw(spriteBatch, "你好,世界!", 300,330);
spriteBatch.end();
}
}
这是程序的结果。
感谢您的解释和建议。我现在能够解决这个问题。我还添加了一个完整的示例,希望它能帮助将来的人。謝謝DroidSansFallback有中文字符。你可以从(或者你应该在你的android sdk文件夹中有一个副本)获得它。如果你使用这种字体,符号很有可能看起来非常接近手机上的其他android应用程序(如果这是一个目标).Ahh,谢谢你的链接。a会看一看。我使用的ttf大于12MB,但这些文件要小得多。