【发布时间】:2013-11-25 22:03:12
【问题描述】:
我正在尝试使用字体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 类绘制不正确,我想知道原因。
【问题讨论】: