(libgdx小结)TTF字库的使用(优美字体的绘制)

本文详细介绍了如何利用TTF字符库生成个性化字体,并通过编程实例展示了其在游戏开发中的应用。包括初始化方法、API使用、效果展示及源码下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里再次声明,本系列博客参考了奋斗的小土豆的系列博文。

TTF字符库的使用是的你可以使用一些你自己定义的优美字体。


一、初始化方法及常见的API


FreeTypeFontGenerator generator;//TTF字体发生器。负责保存和解析.ttf文件
	FreeTypeBitmapFontData fontData;//负责处理FreeTypeFontGenerator中的数据
	BitmapFont font;

generator = new FreeTypeFontGenerator(Gdx.files.internal("potato.ttf"));//FreeTypeFontGenerator的初始化
		
		/**
		 * FreeTypeBitmapFontData的初始化
		 * 这3个参数的含义为:
		 * 字号大小、中文字符串、是否翻转
		 */
		fontData = generator.generateData(35, generator.DEFAULT_CHARS + "长的帅人告白才叫,丑男那性骚扰。奋斗小土豆手机工作游戏室", false);
		font = new BitmapFont(fontData, fontData.getTextureRegion(), false);
		
		font.setColor(Color.GREEN);



二、应用举例


1、导jar包

在原有jar包的基础上再到extension包下的gdx-freetype包下找到armeabi、armeabi-v7a、gdx-freetype.jar,

并将其导入工程


2、编程

package com.example.groupactiontest;

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 MyGame implements ApplicationListener {

	FreeTypeFontGenerator generator;//TTF字体发生器。负责保存和解析.ttf文件
	FreeTypeBitmapFontData fontData;//负责处理FreeTypeFontGenerator中的数据
	BitmapFont font;
	
	SpriteBatch batch;
	
	@Override
	public void create() {
		generator = new FreeTypeFontGenerator(Gdx.files.internal("potato.ttf"));//FreeTypeFontGenerator的初始化
		
		/**
		 * FreeTypeBitmapFontData的初始化
		 * 这3个参数的含义为:
		 * 字号大小、中文字符串、是否翻转
		 */
		fontData = generator.generateData(35, generator.DEFAULT_CHARS + "长的帅人告白才叫,丑男那性骚扰。奋斗小土豆手机工作游戏室", false);
		font = new BitmapFont(fontData, fontData.getTextureRegion(), false);
		
		font.setColor(Color.GREEN);
		
		batch = new SpriteBatch();
		
	}

	@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.draw(batch, "长的帅人告白才叫,丑男那性骚扰。奋斗小土豆手机工作游戏室",0,  Gdx.graphics.getHeight());
		font.drawMultiLine(batch, "长的帅人告白才叫,丑男那性骚扰。\n 奋斗小土豆手机工作游戏室", 0,  Gdx.graphics.getHeight()/2);
		batch.end();
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}


三、效果图




四、源码下载

http://download.youkuaiyun.com/detail/caihongshijie6/7004581


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

帅气的东哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值