本文介绍了如何加载一个TrueTypeFont与不同的参数? LibGDX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,我一直在做的方式是复制相同的字体,保存在另一个名字,并加载不同的参数。我试图在LibGDX中开发一个应用程序,并保存字体,因为不同的文件需要更多的内存。是不是有一种方法加载1多次字体,每次都有不同的参数? ,我创建了具有不同大小的BitmapFont对象列表,只需更改 FreeTypeFontGenerator.FreeTypeFontParameter
的属性即可。SpriteBatch spriteBatch;
BitmapFont font [];
OrthographicCamera相机;
@Override
public void create(){
camera = new OrthographicCamera();
camera.setToOrtho(false,400,640);
spriteBatch = new SpriteBatch();
int size [] = new int [] {5,8,10,13,15,20,30,40,50,60};
font = new BitmapFont [size.length];
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(BUBBLEGUM.TTF));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.color = Color.WHITE;
parameter.magFilter = Texture.TextureFilter.Linear; //用于调整质量
parameter.minFilter = Texture.TextureFilter.Linear;
for(int i = 0; i< size.length; i ++){
parameter.size = size [i];
font [i] = generator.generateFont(parameter);
font [i] .getRegion()。getTexture()。setFilter(Texture.TextureFilter.Linear,Texture.TextureFilter.Linear);
font [i] .setColor(1.0f,0.0f,0.0f,1.0f);
}
generator.dispose(); //不要忘记处理
}
@Override
public void render(){
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT );
Gdx.gl.glClearColor(1,1,0,1);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
for(int i = 0; i< font.length; i ++)
font [i] .draw(spriteBatch,LIBGDX FONT,10,100 + i * 55);
spriteBatch.end();
}
@Override
public void resize(int width,int height){
camera.setToOrtho(false,width,height);
$ b $ @覆盖
public void dispose(){
spriteBatch.dispose();
for(BitmapFont bmfont:font)
bmfont.dispose();
$ b $ / code $ / pre
$ b
编辑
The way I have been doing it so far is copying the same font, saving under another name and loading it with different parameters. I am trying to develop an app in LibGDX and by saving the font as different files takes more memory than necessary. Isn't there a way of loading 1 font multiple times, each time with different parameters?
解决方案
Take a look of this example, I am creating a list of BitmapFont object having different size by simply changing attributes of FreeTypeFontGenerator.FreeTypeFontParameter
.
public class MainGame extends Game {
SpriteBatch spriteBatch;
BitmapFont font[];
OrthographicCamera camera;
@Override
public void create() {
camera=new OrthographicCamera();
camera.setToOrtho(false,400,640);
spriteBatch = new SpriteBatch();
int size[]=new int[]{5,8,10,13,15,20,30,40,50,60};
font=new BitmapFont[size.length];
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("BUBBLEGUM.TTF"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.color = Color.WHITE;
parameter.magFilter = Texture.TextureFilter.Linear; // used for resizing quality
parameter.minFilter = Texture.TextureFilter.Linear;
for(int i=0;i<size.length;i++){
parameter.size=size[i];
font[i]=generator.generateFont(parameter);
font[i].getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
font[i].setColor(1.0f, 0.0f, 0.0f, 1.0f);
}
generator.dispose(); // Don't forget to dispose
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(1,1,0,1);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
for (int i=0;i<font.length;i++)
font[i].draw(spriteBatch,"LIBGDX FONT",10,100+i*55);
spriteBatch.end();
}
@Override
public void resize(int width, int height) {
camera.setToOrtho(false,width,height);
}
@Override
public void dispose() {
spriteBatch.dispose();
for(BitmapFont bmfont:font)
bmfont.dispose();
}
}
EDIT
这篇关于如何加载一个TrueTypeFont与不同的参数? LibGDX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!