【问题标题】:Custom, clickable font自定义,可点击字体
【发布时间】: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


【解决方案1】:

生成 200 像素的字体会用相当大的位图字体字形页面填满您的视频内存。如果您使用许多不同的字体类型和/或比例(或仅一种大比例字体),您可能需要考虑实现距离字段字体。看看这个答案:How to draw smooth text in libgdx?

另一种选择是创建图像并使用 Scene2d ImageButton 代替可点击的文本。

【讨论】:

  • 你认为用 Table,Actor,Textbutton 制作菜单屏幕会好吗?
  • 这就是我制作菜单和其他 UI 工具的方式,所以是的!我认为 Scene2d 对于制作 UI/菜单非常有用
  • 谢谢。而对于制作游戏画面,我应该使用什么?例如。显示左上角的分数,我想动态改变分数,所以如果分数增加,我必须更新分数的文本。
  • 当然我使用屏幕,但不知道我应该使用什么来绘制点(文本,还是什么?)
猜你喜欢
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多