一个平平无奇的周末就这样快乐起来了。
最近在学习libGDX游戏引擎的应用,想开发一个小小的Android游戏,其中对于中文显示比较多,这就不可能一条一条地制成图片再显示,终于找到了懒人的 解决方法,参考。
先看看效果:
不经历一次又一次的碰壁肯定难以理解我激动的心情(doge),不过到此为止了。
使用方法就简单啦,我的版本是1.10,如下:
import java.lang.reflect.Field;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeType;
import com.badlogic.gdx.graphics.g2d.freetype.FreeType.Face;
import com.badlogic.gdx.graphics.g2d.freetype.FreeType.SizeMetrics;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.GlyphAndBitmap;
import com.badlogic.gdx.utils.GdxRuntimeException;
public class LazyBitmapFont extends BitmapFont {
private FreeTypeFontGenerator generator;
private FreeTypeBitmapFontData data;
private FreeTypeFontParameter parameter;
private static FreeTypeFontGenerator GLOBAL_GEN = null;
public static void setGlobalGenerator(FreeTypeFontGenerator generator){
GLOBAL_GEN = generator;
}
public LazyBitmapFont(int fontSize){
this(GLOBAL_GEN,fontSize);
}
public LazyBitmapFont(FreeTypeFontGenerator generator, int fontSize) {
if(generator == null)
throw new GdxRuntimeException("lazyBitmapFont global generator must be not null to use this constructor.");
this.generator = generator;
FreeTypeFontParameter param = new FreeTypeFontParameter();
param.size = fontSize;
this.parameter = param;
this.data = new LazyBitmapFontData(generator, fontSize, this);
try {
Field f = getClass().getSuperclass().getDeclaredField("data");
f.setAccessible(true);
f.set(this, data);
} catch (Exception e) {
e.printStackTrace();
}
genrateData();
}
private void genrateData() {
Face face = null;
try {
Field field = generator.getClass().getDeclaredField("face");
field.setAccessible(true);
face = (Face) field.get(generator);
} catch (Exception e) {
e.printStackTrace();
return;
}
// set general font data
SizeMetrics fontMetrics = face.getSize().getMetrics();
// Set space glyph.
Glyph spaceGlyph = data.getGlyph(' ');
if (spaceGlyph == null) {
spaceGlyph = new Glyph();
spaceGlyph.xadvance = (int) data.scaleX;//spaceWidth
spaceGlyph.id = (int) ' ';
data.setGlyph(' ', spaceGlyph);
}
if (spaceGlyph.width == 0)
spaceGlyph.width = (int) (spaceGlyph.xadvance + data.padRight);
// set general font data
data.flipped = parameter.flip;
data.ascent = FreeType.toInt(fontMetrics.getAscender());
data.descent = FreeType.toInt(fontMetrics.getDescender());
data.lineHeight = FreeType.toInt(fontMetrics.getHeight());
// determine x-height
for (char xChar : data.xChars) {
if (!face.loadChar(xChar, FreeType.FT_LOAD_DEFAULT))
continue;
data.xHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
break;
}
if (data.xHeight == 0)
throw new GdxRuntimeException("No x-height character found in font");
for (char capChar : data.capChars) {
if (!face.loadChar(capChar, FreeType.FT_LOAD_DEFAULT))
continue;
data.capHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
break;
}
// determine cap height
if (data.capHeight == 1)
throw new GdxRuntimeException("No cap character found in font");
data.ascent = data.ascent - data.capHeight;
data.down = -data.lineHeight;
if (parameter.flip) {
data.ascent = -data.ascent;
data.down = -data.down;
}
}
@Override
public void dispose() {
setOwnsTexture(true);
super.dispose();
data.dispose();
}
public static class LazyBitmapFontData extends FreeTypeBitmapFontData {
private FreeTypeFontGenerator generator;
private int fontSize;
private LazyBitmapFont font;
private int page = 1;
public LazyBitmapFontData(FreeTypeFontGenerator generator, int fontSize, LazyBitmapFont lbf) {
this.generator = generator;
this.fontSize = fontSize;
this.font = lbf;
}
public Glyph getGlyph(char ch) {
Glyph glyph = super.getGlyph(ch);
if (glyph == null && ch != 0)
glyph = generateGlyph(ch);
return glyph;
}
protected Glyph generateGlyph(char ch) {
GlyphAndBitmap gab = generator.generateGlyphAndBitmap(ch, fontSize, false);
if (gab == null || gab.bitmap == null)
return null;
Pixmap map = gab.bitmap.getPixmap(Format.RGBA8888, Color.WHITE,9);
TextureRegion rg = new TextureRegion(new Texture(map));
map.dispose();
font.getRegions().add(rg);
gab.glyph.page = page++;
super.setGlyph(ch, gab.glyph);
setGlyphRegion(gab.glyph, rg);
return gab.glyph;
}
}
}
调用就简单了,
//1
lazyBitmapFont = new LazyBitmapFont(FreeTypeFontGenerator generator, int fontSize);
//2
lazyBitmapFont.draw(batch, "我随便说一句话就能够组成十五字", 100, 200);
//3
lazyBitmapFont.dispose();
剩下的大家自己探索吧886!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)