2017-06-15 70 views
2

我想要在Android中的Web視圖截圖。然而,屏幕截圖太快,因此我得到一個空白的截圖。我嘗試實施一個webviewclient和onPageFinished在拍攝之前聽取webview加載,但它沒有奏效。在拍攝截圖之前,如何確保視圖已加載?等待webview加載HTML之前採取屏幕截圖

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

您可以添加setWebChromeClient看到網頁視圖的過程。

webview.getSettings().setJavaScriptEnabled(true); 
    webview.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, final int progress) { 
      progressBar.setProgress(progress); 
      if (progress == 100) { 
       grabScreen(); 
      } 
     } 
    }); 
+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.  
    }  
} 
+0

看起來很有希望。但它說onNewPicture已被棄用:https://developer.android.com/reference/android/webkit/WebView.PictureListener.html#onNewPicture – cashmoneyscience

相關問題