4
byte[] GetImageFromText(string text, float fontSize)
{
   //do make png image
   //and returns byte array
}

我想得到一种像上面那样的方法。

4

2 回答 2

3

谢谢卢米斯~

这是我的最终解决方案

float textSize = 30;
String text = "testing";
TextPaint tp = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
tp.setColor(Color.WHITE);
tp.setTextSize(textSize);
Rect bounds = new Rect();
tp.getTextBounds(text , 0, text.length(), bounds);
StaticLayout sl = new StaticLayout(text , tp, bounds.width()+5,
Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);

Bitmap bmp = Bitmap.createBitmap(bounds.width()+5, bounds.height()+5,
                        Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
sl.draw(canvas);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
于 2012-08-30T04:43:43.367 回答
1

您可以先将文本视图绘制到位图中,然后您必须将其保存为 PNG 到私有应用程序内存或 SD 卡中并发送。以下是如何将文本转换为位图的示例:如何将 TextView 绘制为位图(无需在显示器上绘制)

于 2012-08-21T08:23:54.110 回答