【发布时间】:2015-03-10 07:45:48
【问题描述】:
我正在尝试为我的游戏制作一种自定义的、可点击的字体(对于菜单,例如:PLAY -> 进入播放屏幕),我尝试过使用 FreeType,但是当我用它制作新文本时,它使用android上有很多内存。你能建议我一种从 .ttf 文件创建可点击文本的正确方法吗?我是这样做的:
我有一个 Box 类:
public class Box {
protected float x;
protected float y;
protected float width;
protected float height;
public boolean contains(float x, float y) {
return x > this.x - width / 2 &&
x < this.x + width / 2 &&
y > this.y - height / 2 &&
y < this.y + height / 2;
}
}
然后是 Texts 类及其扩展框,所以当我创建一个新文本时,我可以调用 .contains(x,y) 以便我可以使文本可点击:
public class Texts extends Box{
private String text;
private int size;
private float density;
private int dpSize;
private BitmapFont font;
private FreeTypeFontGenerator generator;
private FreeTypeFontParameter parameter;
public Texts(String text, int size, float x ,float y){
this.text = text;
this.size = size;
generator = new FreeTypeFontGenerator(Gdx.files.internal("myfont.ttf"));
parameter = new FreeTypeFontParameter();
parameter.size = size;
dpSize = parameter.size;
font = generator.generateFont(parameter);
generator.dispose();
this.x = x;
this.y = y;
this.width = font.getBounds(text).width;
this.height = font.getBounds(text).height;
}
public void render(SpriteBatch sb){
font.draw(sb, text, x - width / 2 , y + height /2);
}
public void renderNoCenter(SpriteBatch sb){
font.draw(sb, text, x , y);
}
public float getWidth(){
return this.width;
}
public float getHeight(){
return this.height;
}
public void setText(String text){
this.text = text;
}
public void setXY(float x, float y){
this.x = x;
this.y = y;
}
public void update(float dt){
}
public int getDpSize(){
return dpSize;
}
public void dispose(){
font.dispose();
}
}
但是当我创建这样的新文本时,应用程序会消耗 + 12mb RAM / 文本:
Texts play = new Texts("PLAY", 200, 200, 80);
Texts options= new Texts("OPTIONS", 200, 200, 20);
这就是我的问题,感谢您的帮助!
【问题讨论】:
-
如果你只是想画一些盒子,你可以使用paint.extends textview以获得更好的性能
-
我不明白,对不起。我是 libgdx 的新手。
标签: java android fonts libgdx freetype