我有一个问题。当我获得捕获webview时,我的平板电脑中没有捕获画布区域...但内容很好。我不明白...... 但奇怪的是......
如果我提供选项' webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE,null);'保存后出现笔数据(画布),但绘图真的很慢......
这是我的来源..
@SuppressLint("SetJavaScriptEnabled")
private void setWebviewInit(WebView webview) {
webview.setInitialScale(1);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setDisplayZoomControls(false);
webview.setWebViewClient(webviewclient);
webview.setWebChromeClient(new WebviewAlert());
webview.setWebChromeClient(new ChromeClient(this));
webview.addJavascriptInterface(new JIFace(), "ezandroid");
}
//捕获
WebView webview = arrWebView.get(0);
webview.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
webview.layout(0, 0, webview.getMeasuredWidth(), webview.getMeasuredHeight());
webview.setDrawingCacheEnabled(true);
webview.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(webview.getMeasuredWidth(), webview.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
int height = bitmap.getHeight();
canvas.drawBitmap(bitmap, 0, height, paint);
webview.draw(canvas);
if(penBitmap != null){
bitmap = combineBitmaps(bitmap, penBitmap, OVERLAY);
}
int fullHeight = webview.getMeasuredHeight();
int pageHeight = (int) (1122*WEBVIEW_SCALE_RATE);
int croppedStart = 0;
int cnt;
if(fullHeight == pageHeight){
cnt =1;
}else{
cnt = (fullHeight / pageHeight) + 1;
}
ArrayList<String> localFilePath = new ArrayList<String>();
for (int i = 0; i < cnt + 1; i++) { // 0: full Img, else: cropped
Bitmap croppedBitmap = null;
if (i!=cnt) {
if ((croppedStart + pageHeight) > webview.getMeasuredHeight()) {
int lastHeight = webview.getMeasuredHeight() - croppedStart;
croppedBitmap = Bitmap.createBitmap(bitmap, 0, croppedStart, webview.getMeasuredWidth(),lastHeight-1);
} else {
try{
croppedBitmap = Bitmap.createBitmap(bitmap, 0, croppedStart, webview.getMeasuredWidth(), pageHeight);
}catch(Exception e){
e.printStackTrace();
}
}
croppedStart += pageHeight;
}
if (bitmap != null) {
try {
String filePath = Environment.getExternalStorageDirectory().toString();
OutputStream out = null;
File file = new File(filePath, "test");
if(!file.exists()){
file.mkdir();
}
out = new FileOutputStream(filePath + "/test/consentImg" + i + ".jpeg");
localFilePath.add(filePath + "/test/consentImg" + i + ".jpeg");
if(i==cnt){
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
}else{
croppedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
}
if(croppedBitmap != null){
croppedBitmap.recycle();
croppedBitmap = null;
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
bitmap.recycle();
答案 0 :(得分:1)
您需要在创建任何WebView之前调用WebView.enableSlowWholeDocumentDraw()。
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
...
setContentView(layout);
...
...
}
答案 1 :(得分:1)
仅仅添加WebView.enableSlowWholeDocumentDraw()是不够的,我仍然将空白图像保存到我的SD卡中。经过大量的谷歌搜索后,我找到了解决方案。您需要为webview禁用硬件加速,如下所示:
mWebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
答案 2 :(得分:0)
解决。 在保存时,我制作了一张图片,然后我把它放在webview的背景上......
答案 3 :(得分:0)
问题在于硬件加速。
将其关闭应该可以。
<application android:hardwareAccelerated="false">