【问题标题】:Write text on image and save the image with text in the SD card without drawing it on the screen在图像上写入文本并将图像与文本一起保存在 SD 卡中,而不在屏幕上绘制
【发布时间】:2012-09-30 08:05:05
【问题描述】:

我已经为这个问题苦苦挣扎了很长时间。 我想从资源文件夹中获取图像并写入一些文本,作为进程的一部分动态生成,然后将带有文本的最终图像保存在 SD 卡中。我知道如何编写文件在 SD 卡中。我无法在图像上写文字。

我用 imageview 和 textview 创建了一个 RelativeLayout 并将其保存到 SD 卡,但后来意识到我不必绘制视图。所以,不能这样。

再次强调一点,我的应用程序不需要在当前屏幕上绘制位图。

有人可以在这里提供任何解决方案吗?

谢谢! :)

【问题讨论】:

    标签: android canvas view bitmap android-custom-view


    【解决方案1】:

    不要为此使用图像视图。

    使用画布!

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
    Bitmap alteredBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
    Canvas canvas = new Canvas(alteredBitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(bm, 0, 0, paint);
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 
    canvas.drawText("Some Text", 10, 25, paint); 
    

    然后将“alteredBitmap”保存到 SD 卡

    【讨论】:

    • 谢谢理查德!如此简单的解决方案。 :) 非常感谢!
    • 请阅读我在stackoverflow.com/questions/6159186/… 中对 MKJParekh 的回答的评论
    • 你能告诉我上面的示例代码“如何添加多行文本”吗?
    猜你喜欢
    • 2011-11-18
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多