/** * @param view, the view want to screenshot, such as WebView, etc. * @param width, screenshot ' s width. * @param height, screenshot ' s height. * @param scroll, true if you want to start capture from currently scroll position. * @param config, the bitmap.config, such as argb_8888, etc. * @return The screenshot ' s Bitmap */public static Bitmap cap Ture (view view, float width, float height, boolean scroll, Bitmap.config Config) {if (!view.isdrawingcacheenabled ()) { View.setdrawingcacheenabled (TRUE); } Bitmap Bitmap = Bitmap.createbitmap ((int) width, (int) height, config); Bitmap.erasecolor (Color.White); Canvas canvas = new canvas (bitmap); int left = View.getleft (); int top = View.gettop (); if (scroll) {left = View.getscrollx (); top = view.getscrolly (); } int status = Canvas.save (); Canvas.translate (-left,-top); Float scale = width/view.getwidth (); Canvas.scale (scale, scale, left, top); View.draw (canvas); Canvas.reStoretocount (status); Paint Alphapaint = new paint (); Alphapaint.setcolor (color.transparent); Canvas.drawrect (0f, 0f, 1f, height, alphapaint); Canvas.drawrect (width-1f, 0f, width, height, alphapaint); Canvas.drawrect (0f, 0f, width, 1f, alphapaint); Canvas.drawrect (0f, height-1f, width, height, alphapaint); Canvas.setbitmap (NULL); return bitmap;}
public static String screenshot (context context, Bitmap Bitmap, String name) {if (Bitmap = = null) {RET Urn null; } File dir = environment.getexternalstoragepublicdirectory (environment.directory_pictures); if (name = = NULL | | Name.trim (). IsEmpty ()) {name = String.valueof (System.currenttimemillis ()); } name = Name.trim (); int count = 0; File File = new file (dir, name + ". png"); while (File.exists ()) {count++; File = new file (dir, name + "." + Count + ". png"); } try {FileOutputStream stream = new FileOutputStream (file); Bitmap.compress (Bitmap.CompressFormat.PNG, N, Stream); Stream.flush (); Stream.Close (); Context.sendbroadcast (New Intent (Intent.action_media_scanner_scan_file, Uri.fromfile (FILE)); return File.getabsolutepath (); } catch (Exception e) {return null; } }
Android Webview Save as Picture