日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

原生WebView长截图 和 Tencent x5webview截长图

發布時間:2023/12/31 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 原生WebView长截图 和 Tencent x5webview截长图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言: 之前項目用的是原生webview,最近使用公司新框架使用的是x5的webview;正好有需求需要做長截圖,踩坑之路開始。


1、原生WebView長截圖

Android5.0及以上版本,Android對WebView進行了優化,為了減少內存使用和提高性能,使用WebView加載網頁時只繪制顯示部分。如果我們不做處理,仍然使用上述代碼截圖的話,就會出現只截到屏幕內顯示的WebView內容,其它部分是空白的情況。
這時候,我們通過調用WebView.enableSlowWholeDocumentDraw()方法可以關閉這種優化,但要注意的是,該方法需要在WebView實例被創建前就要調用,否則沒有效果。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {android.webkit.WebView.enableSlowWholeDocumentDraw();} /*** 原生WebView長截圖** @param webView* @return*/public static Bitmap getWebViewBtpBase64Str(WebView webView) {Bitmap bitmap = null;try {if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {Picture snapShot = webView.capturePicture();bitmap = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.RGB_565);Canvas canvas = new Canvas(bitmap);snapShot.draw(canvas);} else {float scale = webView.getScale();//得到縮放后webview內容的高度int webViewHeight = (int) (webView.getContentHeight() * scale);bitmap = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Bitmap.Config.RGB_565);Canvas canvas = new Canvas(bitmap);//繪制webView.draw(canvas);}return bitmap;} catch (OutOfMemoryError e) {LogEx.e("ScreenUtils", e.getMessage(), e);} catch (Exception e) {LogEx.e("ScreenUtils", e.getMessage(), e);} finally {}return "";}

2、Tencent x5webview截長圖

/*** Tencent x5webview截長圖** @param webView* @return*/public static Bitmap getX5WebViewBtpBase64Str(WebView webView) {if (webView == null) {return null;}int wholeWidth = webView.getContentWidth();int wholeHeight = webView.getContentHeight();Bitmap x5bitmap = Bitmap.createBitmap(wholeWidth, wholeHeight, Bitmap.Config.RGB_565);Canvas x5canvas = new Canvas(x5bitmap); // x5canvas.scale((float) wholeWidth / (float) webView.getContentWidth(), (float) wholeHeight / (float)webView.getContentHeight());if (webView.getX5WebViewExtension() == null) {return null;}IX5WebViewExtension ix5WebViewExtension = webView.getX5WebViewExtension();ix5WebViewExtension.snapshotWholePage(x5canvas, false, false);Bitmap bitmap = Bitmap.createBitmap(x5bitmap);return bitmap;}

總結

以上是生活随笔為你收集整理的原生WebView长截图 和 Tencent x5webview截长图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。