Public voidconvertviewtobitmap (view view) {//View view = Getlayoutinflater (). Inflate (r.layout.test_layout, NULL); View.setdrawingcacheenabled (true); View.measure (Measurespec.makemeasurespec (0, Measurespec.unspecified), Measurespec.makemeasurespec (0, measurespec.unspecified)); View.layout (0, 0, View.getmeasuredwidth (), View.getmeasuredheight ()); View.builddrawingcache (); Bitmap=View.getdrawingcache (); Date Date=NewDate (System.currenttimemillis ()); Storage.writebitmapfile ("/sdcard/dcim/uucamera/test" +date.tolocalestring () + ". JPEG", bitmap); //testbt.setvisibility (view.visible);View.destroydrawingcache (); }
Enter the layout corresponding to the view and then the operation, the save date is applied, so that each picture has a different save name.
It should be noted that View.measure and view.layout (0, 0, view.getmeasuredwidth (), View.getmeasuredheight ()), and the layout re-calculated height and width, So layout must be encapsulated, otherwise there will be shifts.
Take the following layout as an example
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" ><linearlayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/screen"Android:background= "@drawable/test_bg"android:gravity= "Center_horizontal"android:orientation= "Vertical" > <relativelayout Android:id= "@+id/uupic"Android:layout_width= "304DP"Android:layout_height= "380DP"Android:layout_margintop= "90DP"Android:background= "@drawable/test_photo_bg"android:gravity= "Center" > <ImageView Android:id= "@+id/myuupic"Android:layout_width= "264DP"Android:layout_height= "352DP"/> </RelativeLayout> <relativelayout Android:id= "@+id/lovepic"Android:layout_height= "116DP"Android:layout_width= "Match_parent" ><TextView Android:id= "@+id/agetext"Android:layout_width= "120DP"Android:layout_height= "116DP"android:gravity= "Center"Android:textcolor= "#ffffff"android:textsize= "@dimen/age_text_size"Android:background= "@drawable/test_age_bg"/><ImageView Android:id= "@+id/genderpic"Android:layout_width= "44DP"Android:layout_height= "44DP"Android:layout_torightof= "@+id/agetext"Android:layout_alignparentbottom= "true"Android:layout_marginleft= " -30DP"Android:scaletype= "Fitend"/><ImageView Android:id= "@+id/testimg"Android:layout_width= "222DP"Android:layout_height= "86DP"Android:layout_torightof= "@+id/agetext"Android:layout_alignparentbottom= "true"/></relativelayout></linearlayout><!--share and save layout-<relativelayout Android:id= "@+id/testbt"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparentbottom= "true"Android:layout_marginleft= "24DP"Android:layout_marginright= "24DP"Android:layout_marginbottom= "20DP" > <Button Android:id= "@+id/save"Android:layout_width= "148DP"Android:layout_height= "40DP"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:background= "@drawable/testbtstyle"Android:text= "Save"Android:textcolor= "#ff4c6c"android:textsize= "20sp"/> <Button Android:id= "@+id/share"Android:layout_width= "148DP"Android:layout_height= "40DP"Android:layout_alignparentright= "true"Android:layout_alignparenttop= "true"Android:background= "@drawable/testbtstyle"Android:text= "Share"Android:textcolor= "#ff4c6c"android:textsize= "20sp"/> </RelativeLayout></RelativeLayout>
Encapsulated in two-part top display, hidden underneath
When referencing
Mscreenshot =Mtestview.findviewbyid (r.id.screen); Public voidconvertviewtobitmap (view view) {//View view = Getlayoutinflater (). Inflate (r.layout.test_layout, NULL); View.setdrawingcacheenabled (true); View.measure (Measurespec.makemeasurespec (0, Measurespec.unspecified), Measurespec.makemeasurespec (0, measurespec.unspecified)); View.layout (0, 0, View.getmeasuredwidth (), View.getmeasuredheight ()); View.builddrawingcache (); Bitmap=View.getdrawingcache (); Date Date=NewDate (System.currenttimemillis ()); Storage.writebitmapfile ("/sdcard/dcim/uucamera/test" +date.tolocalestring () + ". JPEG", bitmap); //FileOutputStream fos = new FileOutputStream ("/sdcard/dcim/uucamera/test.png"); //compress the image data in the bitmap object into PNG-formatted image data and save the data in the Test.png file//bitmap.compress (compressformat.png, N, FOS); //close the file output stream//testbt.setvisibility (view.visible);View.destroydrawingcache (); if(!Ispicsave ()) { return; } } Caser.id.save:logman.e (TAG,"Save ..."); if(mscreenshot!=NULL){//testbt.setvisibility (view.invisible);Convertviewtobitmap (mscreenshot); }//testbt.setvisibility (view.visible); Break;
Android Footstep---Save layout with its output as a picture