libgdx 学习笔记(四)中文支持

libgdx 源码  BitmapFont类本身是不支持中文的

public  BitmapFont () {
         this (Gdx.files.classpath( "com/badlogic/gdx/utils/arial-15.fnt" ),
             Gdx.files.classpath( "com/badlogic/gdx/utils/arial-15.png" ), false , true );
}
使用下面的代码可以实现中文显示,但必须要自定义一些字体和图片,用工具:Hiero 可以实现
bitmapFont = new  BitmapFont(Gdx.files.internal( "cf.fnt" ), Gdx.files.internal( "cf.png" ), false );
今天介绍的是用字库.ttf实现中文显示,代码如下:
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
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.stbtt.TrueTypeFontFactory;
/**
 * 显示中文
 * @author cui.li
 *
 */
public class FontDemo implements ApplicationListener {
// SpriteBatch是libgdx提供的opengl封装,可以在其中执行一些常规的图像渲染,
// 并且libgdx所提供的大多数图形功能也是围绕它建立的。
SpriteBatch spriteBatch;
// BitmapFont是libgdx提供的文字显示用类,内部将图片转化为可供opengl调用的
// 文字贴图(默认不支持中文)。
private BitmapFont font;
private Texture textTure;
public static final String FONT_CHARACTERS = "世界人民爱好的美好中文支持再看看";


@Override
public void create() {
font = TrueTypeFontFactory.createBitmapFont(
Gdx.files.internal("data/font/stxingka.ttf"), FONT_CHARACTERS, 12.5f,
7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
textTure = new Texture(Gdx.files.internal("data/xk.jpg"));  
spriteBatch = new SpriteBatch();
font.setColor(Color.RED);


}


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


@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(textTure, 0, 0);
        font.draw(spriteBatch, "中文支持达到法定", 80, 80);
        spriteBatch.end();


}


@Override
public void pause() {


}


@Override
public void resume() {


}


@Override
public void dispose() {
// 释放占用的资源
spriteBatch.dispose();
font.dispose();
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ada

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值