【问题标题】:How do I convert a RelativeLayout with an Imageview and TextView to a PNG image?如何将具有 Imageview 和 TextView 的 RelativeLayout 转换为 PNG 图像?
【发布时间】:2011-05-10 11:29:27
【问题描述】:

在我的 Android App Activity 中,我有一个带有一个 ImageView 和几个 TextView 在运行时填充的 RelativeLayout。 我在活动中还有一个保存按钮,用于将 ImageView 中的图像保存到设备 SD 卡。 现在我真正想做的是在单击“保存”按钮时将元素(RelativeLayout 中的图像和文本)一起转换为 PNG 图像并将其保存到 SD 卡。

以前有没有人尝试过这样的转换?如果有人可以给我一些提示或代码 sn-ps 来告诉我如何去做这将非常有帮助?

保存功能工作正常,但目前仅将图像保存在图像视图中。

提前致谢。

【问题讨论】:

    标签: java android png android-layout


    【解决方案1】:

    RelativeLayoutView 的子类,以下内容适用于任何视图:

    final View v; // The view that you want to save as an image
    Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    v.draw(c);
    File outputFile; // Where to save it
    FileOutputStream out = new FileOutputStream(imageFile);
    boolean success = bitmap.compress(CompressFormat.PNG, 100, out);
    out.close();
    

    在您闲暇时添加异常处理。 ;)

    【讨论】:

    • 好的,我会试试这个并发布它是如何工作的......非常感谢!
    • 非常感谢,效果很好!!想提一下,如果文件名和/或路径长度超过 200 个字符,则 FileOutputStream 报告了 Sun 的错误。
    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 2012-01-29
    • 2018-02-15
    • 1970-01-01
    • 2021-04-22
    • 2013-01-08
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多