2011-07-15 4 views
2

J'essaie de faire un Bitmap d'une vue dans Android ainsi:Faire Bitmap du problème WebView dans Android

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); 

et il fonctionne de la même que pour les autres vues telles que ImageView, ....

Mais quand je veux créer un bitmap à partir de WebView, il est créé mais le contenu n'apparaît pas. J'ai également essayé d'utiliser WebViewClient pour détecter quand le contenu a déjà été chargé, mais le résultat est le même. Ci-dessous est le code que j'ai utilisé:

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 est créé avec un fond vert mais le contenu ne s'affiche pas. Des suggestions pour lesquelles le contenu n'est pas dessiné dans le WebView ou à quel moment il peut être prêt pour le dessin? Merci d'avance pour toute aide !!!

Répondre

1

Pour moi, les travaux suivants.

 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; 
}