渲染Android的web视图为位图,HTML5 javascript的回调问题位图、视图、回调、问题

2023-09-13 23:46:36 作者:愛你再深ゞ終為過客

我有一个按钮,图像和web视图的活动。 我的目标是加载一个页面,然后执行的JavaScript就可以使用绘制图形HTML5等。

I have an activity with a button and an image and a webview. My goal is to load a page, then execute javascript on it to draw graphics using HTML5 etc.

由HTML5绘制的图像的高度是未知的/可变的,但该宽度应该总是相同的手机的宽度/或浏览器窗口。

The height of the image drawn by the html5 is unknown/variable , but the width should always be the same as the phone's width/or browser window.

我有一个JavaScript接口来获取包含的信息,它已经完成了调用绘图功能的回调,这是给我的高度。

I have a javascript interface to get a callback containing info that it has now finished calling the drawing function, and it is giving me the height.

下面是web的视图的设置

Here is the setup of the web-view

mWeb = (WebView) findViewById(R.id.webView1);
mWeb.clearCache(true);
mWeb.addJavascriptInterface(new IJavascriptHandler(), "cpjs");

mWeb.getSettings().setJavaScriptEnabled(true);

mWeb.loadUrl(" http://theURL.com ");
mWeb.setInitialScale(100);
mWeb.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);


mWeb.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url){
                mWidth = mWeb.getWidth();
                mHeight = mWeb.getHeight();

                view.loadUrl("javascript: Tools.adjustWidth(" + mWidth + " )");

                String javaCouponRender = "renderCoupon(" + kupongJson + ", 0); void(0)";
                view.loadUrl("javascript: " + javaCouponRender);

            }
             });



        }

和按钮我有我的看法,我已经习惯了启动使我的WebView与此功能的质地:

And the button I have in my view, I have used to initiate render my webview to a texture with this function:

void renderCoupon(int width,int height){


    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas c =new Canvas(bitmap);
    mWeb.draw(c);
    imgView.setMinimumWidth(width);
    imgView.setMinimumHeight(height);
    imgView.setImageBitmap(bitmap);
    imgView.setVisibility(View.VISIBLE);
}

这工作。 pressing按钮,呈现绘制的内容纹理和ImageView的有一个正确的结果。唯一的cavaet是我必须等待,直到页面加载/ $ P $之前绘制pssing按钮。

This works. Pressing the button, renders the drawn content to the texture and the imageview has a correct result. The only cavaet is I have to wait until the page has loaded/drawn before pressing the button.

现在,我已经实现了我的回调从JavaScript得到这样的:

Now, I have implemented the callback I get from javascript like this:

final class IJavascriptHandler {
       IJavascriptHandler() {
       }

       public void couponRenderedCallback(final int height){

           mHasGotRenderCallback = true;
           mHeight = height;
              Toast t = Toast.makeText(getApplicationContext(), "see if it helps sleeping", 2000);
              t.show();
              try{
                Thread.sleep(5000);
            }
            catch(InterruptedException ex){

            }
              runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Toast t2 = Toast.makeText(getApplicationContext(), "and now we render to texture", 2000);
                        t2.show();       
                        renderCoupon(mWidth, mHeight);
                    }
                });

       }

    }

而每一次,这个回调函数呈现一个白色的页面。有趣的是,如果我身体上,只要我看到这个回调称为preSS渲染到纹理按钮,它显示了预期的结果。难道runOnUiThread的失败不知何故?

And every time, this callback function renders a white page. The funny thing is that if I physically press the render-to-texture button as soon as I see this callback is called, it shows the expected result. Is it runOnUiThread that is failing somehow?

睡眠是我加看看是否有某种web视图渲染和回调从我得到的JavaScript搞笑的延迟。

The sleep is something I added to see if there was some sort of funny delay of the webview rendering and the callback I get from javascript.

我也试着动睡眠进入runOnUiThread,这也没有帮助。

I have also tried moving the sleep into the runOnUiThread , which also did not help.

我希望其他人知道的,我应该看什么方向来解决这个问题。任何帮助是AP preciated。

I hope someone knows in what direction I should look to solve this problem. Any help is appreciated.

推荐答案

找到了解决办法,这是对renderCoupon改成这样:

Found a solution, which was to change the renderCoupon to this:

void renderCoupon(final int width,final int height){

    mWeb.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            final Canvas c =new Canvas(bitmap);
            mWeb.draw(c);

            imgView.setMinimumWidth(width);
            imgView.setMinimumHeight(height);
            imgView.setImageBitmap(bitmap);
            imgView.setVisibility(View.VISIBLE);
        }
    }, 100);
}

当然,

和我在我的回调除去睡眠的东西。感谢我的collegue谁放倒了我这件事。 :)

And of course I removed the Sleep stuff in my callback. Thanks to my collegue who tipped me about this. :)