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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

android x5webview截长图

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

普通的webview:

public static Bitmap shotWebView( WebView webView, ImageView imageView) {try {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {// Android5.0以上float scale = webView.getScale();int width = webView.getWidth();int height = (int) (webView.getContentHeight() * scale + 0.5);final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);webView.draw(canvas);// 保存圖片Log.d("bitmap888--","45"+bitmap.getByteCount());savePicture(webView.getContext(), bitmap);return bitmap;} else {// Android5.0以下Picture picture = webView.capturePicture();int width = picture.getWidth();int height = picture.getHeight();if (width > 0 && height > 0) {Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);picture.draw(canvas);// 保存圖片savePicture(webView.getContext(), bitmap);return bitmap;}return null;}} catch (OutOfMemoryError oom) {return null;} }

注:捕捉oom實際沒有意義,可以通過降低圖片質量(RGB_565),也可以截取截圖高度,防止圖片過高(下面x5webview中截取一半)等方法來防止oom

x5webview:

private Bitmap captureWebView(X5WebView webView) {int wholeWidth = webView.computeHorizontalScrollRange();int wholeHeight = webView.computeVerticalScrollRange();wholeHeight=wholeHeight/2;//高度截取一半,防止oom,后面可以指定高度,縮放進行換算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()/2)));if ( webView.getX5WebViewExtension() == null) {return null;}webView.getX5WebViewExtension().snapshotWholePage(x5canvas, false, false, new Runnable() {@Overridepublic void run() {//拿到x5bitmap之后的業務邏輯,(如果不在線程里操作,return 拿到的x5bitmap,不穩定,實際加載bitmap的時候時有時無,但是在線程里操作就沒這個問題)}});return x5bitmap; }

注:重點是這個線程,因為網上大部分的方法在使用snapshotWholePage的時候都是用的上面三個參數的方法(網上和tx都沒有snapshotWholePage相關的api)。

總結

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

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