Android:在 Nougat 中截取整个 WebView 的屏幕截图(Android 7) [英] Android: take screenshot of whole WebView in Nougat ( Android 7 )

查看:73
本文介绍了Android:在 Nougat 中截取整个 WebView 的屏幕截图(Android 7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码从 WebView 截取屏幕截图.它在 Android 6 和更低版本中运行良好,但在 Android 7 中它只需要 webview 的可见部分.

I use below code to take screenshot from a WebView. it works well in Android 6 and lower versions but in Android 7 it takes only the visible part of the 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);

但位图 b 不完整.如何在 Android Nougat 中截取整个 WebView 的屏幕截图?

but bitmap b is not complete. How can I take screenshot of whole WebView in Android Nougat?


我发现 webView.getContentHeight 效果不佳.我对整个 webview 高度进行了硬编码,并且效果很好.所以问题是:如何在 Android Nougat 中获得整个 WebView 内容高度?


I found out the webView.getContentHeight doesn't works well. I hard coded the whole webview height and it works well. So the Question is : How can I get the Whole WebView Content Height in Android Nougat?

推荐答案

使用以下方法代替 getMeasuredWidth() &getContentHeight() 方法:

Use the following methods instead of getMeasuredWidth() & getContentHeight() method:

computeHorizontalScrollRange(); -> for width 
computeVerticalScrollRange(); -> for height

这两种方法将返回整个可滚动的宽度/高度,而不是屏幕上 webview 的实际宽度/高度

these two methods will return the entire scrollable width/height rather than the actual widht/height of the webview on screen

要实现这一点,您需要使 WebViews getMeasuredWidth() &getContentHeight() 方法公开如下

To achieve this you need to make WebViews getMeasuredWidth() & getContentHeight() methods public like below

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();
    }

}

在其他工作中,您还可以使用视图树观察器来计算 webview 高度,如本 answer

in other work around you can also use a view tree observer to calculate the webview height as shown in this answer

这篇关于Android:在 Nougat 中截取整个 WebView 的屏幕截图(Android 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆