在拍摄截图之前等待webview加载html

时间:2017-06-15 12:37:17

标签: java android webview screenshot

我正在尝试在Android中截取网页视图。然而,屏幕截图发射太快,因此我得到一个空白截图。我尝试实现webviewclient和onPageFinished来监听在拍摄之前加载的webview,但它没有用。如何在截取屏幕截图之前确保加载视图?

public void onSaveClicked(View reLayout){

    final WebView webview;
    setContentView(R.layout.webview);
    webview = (WebView) findViewById(R.id.webview);

    WriteJsJson();
    Activity context;

    context = _activity.get();
    Intent fire = new Intent(context, WebviewActivity.class);
    switch (_reportType) {
        case 1 :
            fire.putExtra("target", "daily"); // Parameter to tell the webview activity to open the right report.
        case 2 :
            fire.putExtra("target", "week");
        case 3 :
            fire.putExtra("target", "month");
    }
    startActivity(fire);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            grabScreen(); //method for taking screenshot and storing it...
        }
    });

2 个答案:

答案 0 :(得分:2)

onPageFinished通知主机应用程序页面已完成加载。仅为主框架调用此方法。调用onPageFinished()时,渲染图片可能尚未更新。要获取新图片的通知,请使用onNewPicture(WebView,Picture)。

示例代码

 mWebView.setPictureListener(new MyPictureListener());
//... and then later on....
class MyPictureListener implements PictureListener {

    @Override
    public void onNewPicture(WebView view, Picture arg1) {
      // put code here that needs to run when the page has finished loading and
      // a new "picture" is on the webview.      
    }    
} 

答案 1 :(得分:2)

您可以添加setWebChromeClient以查看webview的过程。

 webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, final int progress) {
            progressBar.setProgress(progress);
            if (progress == 100) {
                grabScreen();
            } 
        }
    });