2017-10-31 12 views
0

私はビットマップ上のテーブルでHTMLをレンダリングしようとしていますが、今は成功していません。Xamarin Android - ビットマップ上でHTMLテーブルをレンダリングする方法

WebViewでHTMLをロードしてからview.DrawまたはCapturePicture()を使用してスクリーンショットを取得しようとしましたが、OnPageFinished()が後で実行されるため空白のスクリーンショットが表示されます。

私のアプリケーションは、主な活動

のようなものです - >アダプタが

ViewPager活動クラスに

public class TreeCatalog 
{ 
    static TreePage[] treeBuiltInCatalog = { 
     new TreePage { 
      imageId = Resource.Drawable.larch, 
      caption = "Introduction", 
      bmp = BitmapFactory.DecodeFile(tempPath + "1.jpg") 
     }, 

     new TreePage { 
      imageId = Resource.Drawable.maple, 
      caption = "Highlights", 
      bmp = BitmapFactory.DecodeFile(tempPath + "2.jpg") 
     }, 

     new TreePage { 
      imageId = Resource.Drawable.maple, 
      caption = "Illustration", 
      bmp = BitmapFactory.DecodeFile(tempPath + "3.jpg") 
     },    
    };  

    private TreePage[] treePages; 
    public TreeCatalog() { treePages = treeBuiltInCatalog; } 
    public TreePage this[int i] { get { return treePages[i]; } } 
    public int NumTrees { get { return treePages.Length; } } 
} 

imageCreationClass.csを画像をキャプチャするために、次のコードを使用していますimageCreationClass.csと移入 - ViewPager活動>

public static void preparePageThree() 
    { 
     WebView web = new WebView(logic.context);       
     web.Layout(0, 0, 800, 800); 
     web.SetWebViewClient(new MyWebViewClient()); 
     web.LoadData(HtmlSource, "text/html", "utf-8"); 
    } 


public class MyWebViewClient : WebViewClient 
    { 
     public override bool ShouldOverrideUrlLoading(WebView view, string url) 
     { 
      view.LoadUrl(url); 
      return true; 
     } 

     public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon) 
     { 
      base.OnPageStarted(view, url, favicon); 
     } 

     public override void OnPageFinished(WebView view, string url) 
     { 
      base.OnPageFinished(view, url); 
      //Picture picture = view.CapturePicture(); 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.InMutable = true; 

      Bitmap.Config conf = Bitmap.Config.Argb8888; 
      bmp = Bitmap.CreateBitmap(200, 200, conf); 
      Canvas canvas = new Canvas(bmp); 
      //canvas.DrawColor(Color.Beige); 
      view.Draw(canvas); 
      //canvas.DrawPicture(picture); 

      if(File.Exists(tempPath + "3.jpg")) 
      { 
       File.Delete(tempPath + "3.jpg"); 
      } 

      using (var os = new System.IO.FileStream(logic.tempPath + "3.jpg", System.IO.FileMode.CreateNew)) 
      { 
       bmp.Compress(Bitmap.CompressFormat.Jpeg, 95, os); 
      }     
     } 

     public override void OnReceivedError(WebView view, ClientError errorCode, string description, string failingUrl) 
     { 
      base.OnReceivedError(view, errorCode, description, failingUrl); 
     } 
    } 

画像に直接HTMLをレンダリングするより良い方法があるかどうかを案内してください(ビットマップ)。

+0

あなたの 'ViewPager'アダプタのコードを投稿してください。アダプター用フラグメントを使用する場合は、フラグメントのコードも記入してください。 –

+0

この例から取得したものhttps://github.com/xamarin/monodroid-samples/tree/master/UserInterface/TreePager ページャのタブに画像を提供するだけです – seopower

答えて

0

WebViewでHTMLをロードしてからview.DrawまたはCapturePicture()を使用してスクリーンショットを取得しようとしましたが、OnPageFinished()が後で実行されるため空白のスクリーンショットが表示されます。

WebViewは非同期にHTMLを読み込み、読み込み時間はネットワークの条件によって大きく異なります。だから、それはあなたがコントロールできるものではありません。

私の提案は、現在のアクティビティに読み込みを行い、ロード中にすべてのビットマップをwebviewで準備することです。その後、用意されたビットマップでViewPagerアダプタを初期化します。

関連する問題