我想在不编辑字体文件的情况下,用字母之间的自定义间隙绘制文本。我可以用这种方式在pc机上执行。
public void makeTextImageWithFont(String message, Font font) {
AffineTransform affine = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affine, true, false);
//Get width of generated image
double w = generateWidth(message, font, frc);
//Get height of generated image with descent/ascent info
double[] h = generateHeight(message, font, frc, affine);
BufferedImage img = new BufferedImage(w, h[0] + h[1], BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIASING_ON);
double pad = 0.0
for(int i = 0; i < message.length(); i++) {
String str = Character.toString(message.charAt(i));
if(str.equals(" ")) {
pad += 30; //custom space
continue;
}
//Get Glyph vector data of this letter
Shape oueline = font.createGlyphVecotr(frc, str).getGlyphOutline(0);
//Define (x,y) location of Path2D
double[] offset = decideOffet(pad, h[0]+h[1], h[1]);
//There is space even in glyph vector data too. To remove this space, I have to get minimum x coord from vector data
double left = getLeftPoint(outline.getPathIterator(affine));
//Handle stroke width, and apply left to offset
offset[0] -= left - strokeWidth - 5;
offset[1] += strokeWidth;
//Get pure vector path with PathIterator and offset data
Path2D path = generatePath2D(offset, outline.getPathIterator(affine));
//Draw path
drawPathWithStroke(path, g, strokeWidth * 2);
//Next letter now has to be drawn further to the right
//4 is custom gap
pad += generateLetterWidth(str, frc) + 4;
}
}
使用这种方法,我可以生成任何字体和任何自定义间距的文本图像。现在我正尝试在android中执行同样的操作。我看得见 Font
或者 Path
,但我找不到其他的选择 FontRenderContext
. 所以,我要问几个问题。
有没有什么方法可以像这样在android中获取glyph向量数据?
我真的要把字体作为安卓的资产吗?因为字体文件大约是20mb,我不想让我的应用程序的大小得到增加。所以我们能产生 Font
从设备中的外部文件?我知道我可以 Font
从android文件,但这需要androidapi级别29,而我的应用程序可以运行在api至少24。
暂无答案!
目前还没有任何答案,快来回答吧!