【问题标题】:how to make a png image from dynamic text in Android?如何从Android中的动态文本制作png图像?
【发布时间】:2012-08-21 07:26:23
【问题描述】:
byte[] GetImageFromText(string text, float fontSize)
{
   //do make png image
   //and returns byte array
}

我想得到一个类似上面的方法。

【问题讨论】:

  • 您的 png 存储在哪里?
  • 一个 png 图像存储在内存中。然后通过网络传输。
  • 图片转换成Base64格式了吗??

标签: android png


【解决方案1】:

感谢卢米斯~

这是我的最终解决方案

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();

【讨论】:

  • 你不需要 canvas.save() 和 canvas.restore() 因为你没有对画布进行任何转换。
【解决方案2】:

您可以先将文本视图绘制到位图中,然后您必须将其以 PNG 格式保存到私有应用程序内存或 SD 卡中并发送。以下是如何将文本转换为位图的示例:How to draw a TextView into a Bitmap (without ever beeing drawn on the display)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 2021-05-09
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 2012-09-25
    相关资源
    最近更新 更多