How do I convert a RelativeLayout with an Imageview and TextView to a PNG image?

后端 未结 1 1729
孤独总比滥情好
孤独总比滥情好 2020-12-17 07:37

In my Android App Activity, I have a RelativeLayout with one ImageView and a couple of TextViews being populated at runtime. I also have a Save button in the activity that

相关标签:
1条回答
  • 2020-12-17 07:58

    RelativeLayout is a subclass of View, and the following should work for any view:

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

    Add exception handling at your leisure. ;)

    0 讨论(0)
提交回复
热议问题