2

I try to make a Bitmap from a view in Android in this way:

Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(myBitmap);

TextView myText = new TextView(myContext);      
newText.setText("This is the text that its going to appear");

myText.layout(0, 0, width, height);
myText.draw(myCanvas);

and it works the same as for other views such as ImageView,....

But when i want to create a Bitmap from WebView, it is created but content does not appear. I also tried to use WebViewClient to detect when content has been already loaded, but the result is the same. Bellow is code i've used:

Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas myCanvas = new Canvas(myBitmap);

String mWebViewContent = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";
 WebView lWebView = new WebView(myContext); 
 lWebView.layout(0, 0, width, height);      
 lWebView.setBackgroundColor(Color.GREEN);      
 lWebView.setWebViewClient(new WebViewClient(){     

            @Override
            public void onPageFinished(WebView view, String url) {
                view.draw(myCanvas);    
                    //content height is 0 at this point
                Log.d("info", "height: " + view.getContentHeight());                
                super.onPageFinished(view, url);
            }


        });
lWebView.loadData(mWebViewContent, "text/html", "utf-8");

Bitmap is created with green background but content does not apear. Any suggestions why the content is not drawn in the WebView or at what moment it can be ready for drawing? Thanks in advance for any help!!!

fox
  • 152
  • 5
  • 15

3 Answers3

4

You should check out the answer at:

Rendering Android webview to bitmap, html5 javascript , callback issue

The key factor here is the postDelayed() call. There is a small lag between when the WebClient.onPageFinished() is called and when the page is actually rendered. It would be nice if there was a onPageRendered() method.

Community
  • 1
  • 1
iHearGeoff
  • 141
  • 6
3

For me a following works.

        final WebView v = new WebView(getContext());
        v.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                addView(v, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                v.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bitmap = createBitmapFromView(v);
                        Drawable drawable = new BitmapDrawable(getResources(), bitmap);
                        imageView.setImageDrawable(drawable);
                        imageView.setVisibility(VISIBLE);
                        removeAllViews();
                        if (listener != null) listener.onComplete();
                    }
                }, 500);
            }
        });
        v.setBackgroundColor(Color.TRANSPARENT);
        //v.getSettings().setJavaScriptEnabled(true);
        v.loadDataWithBaseURL("", text, "text/html", "utf-8", "");
        v.setHorizontalScrollBarEnabled(false);
        v.setVerticalScrollBarEnabled(false);

public Bitmap createBitmapFromView(View view) {
    //Pre-measure the view so that height and width don't remain null.
    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

    //Assign a size and position to the view and all of its descendants
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    //Create the bitmap
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
            view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    //Create a canvas with the specified bitmap to draw into
    Canvas c = new Canvas(bitmap);

    //Render this view (and all of its children) to the given Canvas
    view.draw(c);
    return bitmap;
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

try

lWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

for disable hardware acceleration at a view level(webview) in case of HTML5 canvas is not displayed correctly with hardware acceleration

For more details, click this link.