Mne*_*oee 5 android screenshot webview android-7.0-nougat
我使用下面的代码从a截取屏幕截图WebView.它在Android 6及更低版本中运行良好,但在Android 7中它只占用webview的可见部分.
// before setContentView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
...
// before set webview url
webView.setDrawingCacheEnabled(true);
// after html load complete
final Bitmap b = Bitmap.createBitmap(webView.getMeasuredWidth(),
webView.getContentHeight(), Bitmap.Config.ARGB_8888);
Canvas bitmapHolder = new Canvas(b);
webView.draw(bitmapHolder);
Run Code Online (Sandbox Code Playgroud)
但位图b不完整.如何WebView在Android Nougat中截取整个屏幕截图?
编辑:
我发现webView.getContentHeight不行.我对整个webview高度进行了硬编码,效果很好.问题是:如何WebView在Android Nougat中获得整体内容高度?
使用以下方法代替 getMeasuredWidth() 和 getContentHeight() 方法:
computeHorizontalScrollRange(); -> for width
computeVerticalScrollRange(); -> for height
Run Code Online (Sandbox Code Playgroud)
这两个方法将返回整个可滚动宽度/高度,而不是屏幕上 webview 的实际宽度/高度
要实现此目的,您需要将 WebViews getMeasuredWidth() 和 getContentHeight() 方法公开,如下所示
public class MyWebView extends WebView
{
public MyWebView(Context context)
{
super(context);
}
public MyWebView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyWebView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public int computeVerticalScrollRange()
{
return super.computeVerticalScrollRange();
}
}
Run Code Online (Sandbox Code Playgroud)
在其他工作中,您还可以使用视图树观察器来计算 webview 高度,如本答案所示
| 归档时间: |
|
| 查看次数: |
575 次 |
| 最近记录: |