Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

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

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 I use to save the image in the ImageView to the device SD Card. Now what I really want to do is Convert the elements (image and the text in the RelativeLayout) together to a PNG image when the Save button is clicked and Save it to the SD Card.

Have anyone tried a conversion like this before? It would be very helpful if someone can give me some hints or code snippets on how to go about doing this?

The Save functionality works fine but currently only saves the image in the imageview.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...