android x5webview截长图
生活随笔
收集整理的這篇文章主要介紹了
android x5webview截长图
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
普通的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實(shí)際沒(méi)有意義,可以通過(guò)降低圖片質(zhì)量(RGB_565),也可以截取截圖高度,防止圖片過(guò)高(下面x5webview中截取一半)等方法來(lái)防止oom
x5webview:
private Bitmap captureWebView(X5WebView webView) {int wholeWidth = webView.computeHorizontalScrollRange();int wholeHeight = webView.computeVerticalScrollRange();wholeHeight=wholeHeight/2;//高度截取一半,防止oom,后面可以指定高度,縮放進(jìn)行換算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之后的業(yè)務(wù)邏輯,(如果不在線程里操作,return 拿到的x5bitmap,不穩(wěn)定,實(shí)際加載bitmap的時(shí)候時(shí)有時(shí)無(wú),但是在線程里操作就沒(méi)這個(gè)問(wèn)題)}});return x5bitmap; }注:重點(diǎn)是這個(gè)線程,因?yàn)榫W(wǎng)上大部分的方法在使用snapshotWholePage的時(shí)候都是用的上面三個(gè)參數(shù)的方法(網(wǎng)上和tx都沒(méi)有snapshotWholePage相關(guān)的api)。
總結(jié)
以上是生活随笔為你收集整理的android x5webview截长图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 格拉布斯检验法matlab,格拉布斯检验
- 下一篇: java保留小数点后两位_JAVA中小数