Xamarin Android - 如何在位图上呈现HTML表格

时间:2017-10-31 04:00:40

标签: android xamarin bitmap android-viewpager

我一直在尝试使用Bitmap上的表格呈现HTML,但直到现在都没有成功。

我尝试在WebView中加载HTML,然后使用view.Draw或CapturePicture()来获取屏幕截图,但每次我得到一个空白屏幕截图,因为OnPageFinished()稍后执行。

我的应用程序就像

主要活动 - > ViewPager活动 - >适配器使用imageCreationClass.cs填充,使用以下代码捕获图片

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

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(位图)。

1 个答案:

答案 0 :(得分:0)

  

我尝试在WebView中加载HTML,然后使用view.Draw或CapturePicture()来获取屏幕截图,但每次我得到一个空白屏幕截图,因为OnPageFinished()稍后执行。

WebView异步加载html,加载时间在很大程度上取决于网络状况。所以,这不是你能控制的。

我的建议是,对当前活动进行加载,并在加载期间使用webview准备所有位图。之后,使用准备好的位图初始化<select name="mac"> <?php $sql = mysqli_query($conn, "SELECT name, mac FROM wifi_mac"); while ($row = $sql->fetch_assoc()){ echo "<option value=\"" . $row['mac'] . "\">" . $row['name'] . "</option>"; } ?> </select> 适配器。