我正在尝试使用字体Latto-Reg将多行文本绘制到位图上,但StaticLayout似乎存在问题。
paint.setTextSize(label.fontSize);
paint.setTypeface(face);
StaticLayout textLayout = new StaticLayout(label.text, paint, (int)StaticLayout.getDesiredWidth(label.text, paint), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
Bitmap bitmapAux = Bitmap.createBitmap(textLayout.getEllipsizedWidth(), textLayout.getHeight(), Bitmap.Config.ALPHA_8);
canvas.setBitmap(bitmapAux);
canvas.save();
canvas.translate(0, textLayout.height());
textLayout.draw(canvas);
canvas.restore();
根据字体和大小,纹理上下有一定的内边距,虽然文本在位图中完美地适合,但却浪费了大量的内存空间,并且使布局杂乱无章。
我使用单行绘图进行测试,位图完美地适应了文本
paint.getTextBounds(label.text, 0, label.text.length(), rect);
Bitmap bitmapAux = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ALPHA_8);
canvas.drawText(label.text, -rect.left, -rect.bottom, paint);
我尝试从StaticLayout获取各种指标,但它们似乎都偏离了文本:第0行边界、第0行顶部、最后一行底部...导致相同的填充问题。
编辑: 我通过使用基于偏移量的单行绘画解决了这个问题。 仍然有几种不标准的字体,StaticLayout类绘制不正确,我想知道原因。
height()
方法。 - Paul Lammertsma