一、腾讯x5-webview保持长图
缺陷:H5高度太长,易引发OOM,导致webview转bitmap白屏
x5保存长图
/**
* @param context
* @param webView
* @param cutOutHeight 截取高度
* @return
*/
public static Bitmap captureX5WebViewUnsharp(Context context, WebView webView, int cutOutHeight) {
Bitmap bitmap = null;
try {
// DES: 方案二
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
return bitmap;
}
webView.buildLayer();
webView.buildDrawingCache();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
二、原生webview
SDK<7.0时,保存长图,网上通用方法可行,如下:
/**
* @param context
* @param webView
* @param cutOutHeight 截取高度
* @return
*/
public static Bitmap captureX5WebViewUnsharp(Context context, WebView webView, int cutOutHeight) {
Bitmap bitmap = null;
try {
// DES: 方案二
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
return bitmap;
}
webView.buildLayer();
webView.buildDrawingCache();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
当SDK>7.0时,webview上述获取宽高失效,应该使用新的方式获取宽度与高度
重新webview的此方法,对外开放获取宽高
/**
* 获取,webview内容高度,SDK升级7.0,保存长图使用
*
* @return
*/
public int getPageHeight() {
return computeVerticalScrollRange();
}
public int getPageWidth() {
return computeHorizontalScrollRange();
}
截取长图,由于H5网页长图太大,易引发OOM,故只保持固定几个屏幕高度,(注:但是在X5内核的情况,无法截取部分高度,大图OOM无法通过截取几屏缓解该问题)
扩散:根本原理,为什么需要替换方法,未知,此方案从git上获取而来,忘记处理了~,莫怪
/**
* @param context
* @param webView
* @param cutOutHeight 截取高度
* @return
*/
public static Bitmap captureWebViewUnsharp(Context context, WebView webView, int cutOutHeight) {
Bitmap bitmap = null;
try {
// DES: 方案三
try {
int width;
int height;
//设置截图最大高度
int maxHeight = 1024*3*5;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//可以通过获取缩放,然后设置值从而控制webview快照的高度
height = (int) (((BaseWebView) webView).getPageHeight());
} else {
height = ((BaseWebView) webView).getPageHeight();
}
width = ((BaseWebView) webView).getPageWidth();
if (height >= maxHeight) {
height = maxHeight;
}
bitmap = Bitmap.createBitmap(width, height - cutOutHeight, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
if (cutOutHeight > 0) {
canvas.translate(0, -cutOutHeight);
}
webView.draw(canvas);
} catch (Exception e) {
e.printStackTrace();
// DES: 方案一
float scale = webView.getScale();
int targetHeight = (int) (webView.getContentHeight() * scale + 0.5);
//设置截图最大高度
int maxHeight = getMaxLongImageHeight();
if (targetHeight >= maxHeight) {
targetHeight = maxHeight;
}
bitmap = Bitmap.createBitmap(webView.getWidth(), targetHeight - cutOutHeight, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
if (cutOutHeight > 0) {
canvas.translate(0, -cutOutHeight);
}
webView.draw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}