注意:除了FirstGame以外的代码都是一样的。。一下只贴FirstGame的代码
1、导包
要使用TTF字库,这时候除了导arm...、arm...v7a、gdx.jar和gdk-bakcend....jar以外,还需要到extension这个包里面把这个包里面的arm...、和arm...v7a和gdx-freetype.jar也倒进去
2、FirstGame(MainActivity的写法都是一样的)
package com.doodle.fonttest1;
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.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.FreeTypeBitmapFontData;
public class FirstGame implements ApplicationListener {
private FreeTypeFontGenerator generator;//TTF字体发生器
private FreeTypeBitmapFontData fontData;//负责处理FreeTypeFontGenerator的数据.可以简单地理解成为一个加工好的字符库
private BitmapFont font;//要现实的内容
private SpriteBatch batch;
@Override
public void create() {
/**
* 以下是进行初始化
*/
generator = new FreeTypeFontGenerator(Gdx.files.internal("hjd.ttf"));
fontData = generator.generateData(35,generator.DEFAULT_CHARS + "中国智造惠及全球",false);
font = new BitmapFont(fontData, fontData.getTextureRegion(), false);
batch = new SpriteBatch();
font.setColor(Color.GREEN);
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//清屏
batch.begin();
/**
* font.drawMultiLine()显示多行字体
* \n将理解成换行符
*/
font.drawMultiLine(batch, "hello libgdx \n my name is huangjundong .\n 中国智造 惠及全球", 0, Gdx.graphics.getHeight()/2);
/**
* font.draw()显示单行字体
* \n 将不起作用(即被忽略..)
*
*/
font.draw(batch, "helloworld , \n i love zhangzetian", 0, 100);
batch.end();
}
@Override
public void resize(int arg0, int arg1) {
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}
本demo的下载链接:http://download.csdn.net/detail/caihongshijie6/6985479