android x5webview截长图
生活随笔
收集整理的這篇文章主要介紹了
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截长图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 格拉布斯检验法matlab,格拉布斯检验
- 下一篇: java保留小数点后两位_JAVA中小数