一、騰訊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;
}