首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >笔画汉字

笔画汉字
EN

Stack Overflow用户
提问于 2014-03-19 22:33:49
回答 2查看 4K关注 0票数 9

我喜欢在我的申请中打印中文文本。

1.当我尝试这个时,屏幕将是空的。控制台上没有错误。

创建方法:

代码语言:javascript
运行
复制
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40, "好", false);

渲染方法:

代码语言:javascript
运行
复制
spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "好", 10, 100);
spriteBatch.end();

2.当我尝试这个,三个不同的汉字出现在屏幕上,但我不知道为什么这些字符在哪里画。asd与这三个字符之间没有任何联系。

创建方法:

代码语言:javascript
运行
复制
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40);

渲染方法:

代码语言:javascript
运行
复制
spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "asd", 10, 100);
spriteBatch.end();

有谁知道如何在libgdx中画正确的汉字(我使用的是libgdx的当前版本)?例如:你好吗?-你好吗?-你好吗?

问候

编辑:这是一个完整的例子,它将显示屏幕上预期的汉字。我已经从这里下载了字体:http://www.study-area.org/apt/firefly-font/

代码语言:javascript
运行
复制
package com.mytest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
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.scenes.scene2d.Stage;

public class ChineseFontTest implements ApplicationListener {

private Stage stage;
private SpriteBatch spriteBatch;
public BitmapFont font;

@Override
public void create() {
    stage = new Stage(800, 800);
    spriteBatch = new SpriteBatch();

    FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/fireflysung.ttf"));
    font = gen.generateFont(40, "好你吗", false);

}

@Override
public void dispose() {
    stage.dispose();
}

@Override
public void render() {      
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    spriteBatch.setColor(1, 1, 1, 1);
    spriteBatch.begin();
    font.draw(spriteBatch, "你好吗", 10, 100);
    spriteBatch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-19 23:31:00

转到http://www.angelcode.com/products/bmfont/并获取BMFont并使用它查看您的字体。您还可以使用它预生成libgdx位图字体文件。问题是您的字体不是unicode字体。

当您键入Java代码时,"好“符号将被转换为值好。您使用的字体ttf文件是一种老式的非unicode字体.这些旧字体文件的工作方式是用不同的图片替换"a“这样的字母。"a“是U+61。因此,在您的程序中,字母"a“被转换为U+61,它映射到一个汉字符号,但是值为U+597D的"好”却没有。

您可以继续使用字体并查找每个字符的位置,以便使用正确的数字。不要使用"a“,而要使用类似(char)0x61之类的词,这样就不会让人感到困惑了。或者..。

只需在U+597D处获得包含"好“符号的有效unicode字体即可。

票数 2
EN

Stack Overflow用户

发布于 2017-09-29 15:56:28

这是我的回答:

代码语言:javascript
运行
复制
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();
}

}

这是程序的结果。在这里输入图像描述

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22519584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档