public static Bitmap viewToImage ( Context context , WebView viewToBeConverted ) { int extraSpace = 2000 ; //because getContentHeight doesn't always return the full screen height. int height = viewToBeConverted . getContentHeight ( ) + extraSpace ; Bitmap viewBitmap = Bitmap . createBitmap ( viewToBeConverted . getWidth ( ) , height , Bitmap . Config . ARGB_8888 ) ; Canvas canvas = new Canvas ( viewBitmap ) ; viewToBeConverted . draw ( canvas ) ; //If the view is scrolled, cut off the top part that is off the screen. try { int scrollY = viewToBeConverted . getScrollY ( ) ; if ( scrollY > 0 ) { viewBitmap = Bitmap . createBitmap ( viewBitmap , 0 , scrollY , viewToBeConverted . getWidth ( ) , height - scrollY ) ; } } catch ( Exception ex ) { Log . e ( "Caffeine" , "Could not remove top part of the webview image. ex=" + ex ) ; } return viewBitmap ; }
Convert view to an image . Can be used to make animations smoother .