android Tencent x5webview截長圖

開發中遇到一個問題,截取屏幕作爲一張圖片保存下來,測試的時候用的是大屏手機,一屏就能把內容全部顯示出來,但是用戶使用的是小屏手機,保存圖片之後發現屏幕外的內容截取不到,然後就是一頓百度
參考鏈接:WebView截取長圖
這種方式雖然可以截取長圖,但是保存之後的圖片會變得模糊,不建議使用

還有另外一種方式也可以做參考:webView及ScrollView長截圖
但是這是方式適合原生的webview截圖,不適合x5內核的webview截圖

不囉嗦了,直接上解決方式(源碼)

private Bitmap captureWebView(WebView webView) {

int wholeWidth = webView.computeHorizontalScrollRange();
int wholeHeight = webView.computeVerticalScrollRange();
Bitmap x5bitmap = Bitmap.createBitmap(wholeWidth , wholeHeight, Bitmap.Config.ARGB_8888);

Canvas x5canvas = new Canvas(x5bitmap);
x5canvas.scale((float) wholeWidth / (float) webView.getContentWidth(), (float) wholeHeight / (float)webView.getContentHeight());
if ( webView.getX5WebViewExtension() == null) {
return null;
}

webView.getX5WebViewExtension().snapshotWholePage(x5canvas, false, false);
return x5bitmap;
//保存自己實現吧
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章