From: http://www.eoeandroid.com/thread-95111-1-1.html
Today, the screen is down, but the method is not very elegant. Please give me some advice.
The interface is simple:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/rootLayout" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><Buttonandroid:layout_width="fill_parent" android:layout_height="wrap_content" android:text="save" android:id="@+id/btn"/></LinearLayout>
Interface View:
The following code is used:
Package COM. babeeta. drawview; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import android. app. activity; import android. content. context; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. bitmap. config; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. widget. button; public class main extends activity {/** called when the activity is first created. */button BTN; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); BTN = (button) findviewbyid (R. id. BTN); BTN. setonclicklistener (New button. onclicklistener () {@ override public void onclick (view v) {// todo auto-generated method stub context = getapplicationcontext (); view rootview = findviewbyid (R. id. rootlayout); bitmap newb = bitmap. createbitmap (320,480, config. argb_8888); canvas = new canvas (NEWB); rootview. draw (canvas); file = new file (environment. getexternalstoragedirectory () + "/" + "1.png"); fileoutputstream F = NULL; try {f = new fileoutputstream (File);} catch (filenotfoundexception E) {// todo auto-generated Catch Block E. printstacktrace ();} Boolean B = newb. compress (bitmap. compressformat. PNG, 100, f); If (B) {// success }}});}}
Save it to the SD card as follows: