美文网首页
webView 截图

webView 截图

作者: 捡贝壳的小男孩_839c | 来源:发表于2018-06-07 15:42 被阅读26次

最近项目有个 webView 里面截图的需求。记录一下。

        // 获取根视图
        View dView = context.getWindow().getDecorView();
        // 启用绘图缓存
        dView.setDrawingCacheEnabled(true);
        // 调用缓存
        dView.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
        // 获取内置SD卡路径
        String sdCardPath = Environment.getExternalStorageDirectory().getPath();
        // 图片文件路径
        Calendar calendar = Calendar.getInstance();
        String creatTime = calendar.get(Calendar.YEAR) + "-" +
                calendar.get(Calendar.MONTH) + "-"
                + calendar.get(Calendar.DAY_OF_MONTH) + " "
                + calendar.get(Calendar.HOUR_OF_DAY) + ":"
                + calendar.get(Calendar.MINUTE);
        String filePath = sdCardPath + File.separator + "shot_" + creatTime + ".png";
        if (bitmap != null) {
            try {
                File file = new File(filePath);
                FileOutputStream os = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                os.flush();
                os.close();
                XjjLogManagerUtil.d(TAG, "存储完成");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

通过上述代码截图后,发现一个问题。webView 里面截图的一些图片是没有显示的。在网上查了一下,需要在 webView 的 xml 布局文件里面加上一行属性设置
android:layerType="software"

        <WebView
            android:id="@+id/webView"
            android:layerType="software"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

这样子就能够正常截图了。webView 中的图片也能正常在截图中显示出来了。
然而又发现了一个问题,就是每次在同一个 webView 中截图,保存的图片永远是第一张图片,初步猜测是因为缓存问题,于是换了种截图方式

        //获取webview缩放率
        float scale = webView.getScale();
        //得到缩放后webview内容的高度
        int webViewHeight = (int) (webView.getContentHeight()*scale);
        Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(),webViewHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        //绘制
        webView.draw(canvas);

发现从 webView 获取的截图,有时候会有一大段空白。所以又改成了从 decorView 中获取当前屏幕。

        View decorView = getContext().getWindow().getDecorView();
        Bitmap bitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        //绘制
        decorView.draw(canvas);

这样子截图功能最终就做好了。缺点就是把标题栏也给带进去了。但能保证截图的是 webView 当前显示的全部内容。 所以最终代码是

        View decorView = getContext().getWindow().getDecorView();
        Bitmap bitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        //绘制
        decorView.draw(canvas);
        // 获取内置SD卡路径
        String sdCardPath = Environment.getExternalStorageDirectory().getPath();
        // 图片文件路径
        Calendar calendar = Calendar.getInstance();
        String creatTime = calendar.get(Calendar.YEAR) + "-" +
                calendar.get(Calendar.MONTH) + "-"
                + calendar.get(Calendar.DAY_OF_MONTH) + " "
                + calendar.get(Calendar.HOUR_OF_DAY) + ":"
                + calendar.get(Calendar.MINUTE) + ":"
                + calendar.get(Calendar.SECOND);
        String filePath = sdCardPath + File.separator + "shot_" + creatTime + ".png";
        if (bitmap != null) {
            try {
                File file = new File(filePath);
                if (file.exists()) {
                    file.delete();
                }
                FileOutputStream os = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                os.flush();
                os.close();
                XjjLogManagerUtil.d(TAG, "存储完成");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

相关文章

  • webView 截图

    最近项目有个 webView 里面截图的需求。记录一下。 通过上述代码截图后,发现一个问题。webView 里面截...

  • WebView截图

    初始化webview之前,不然截图只有当前显示的部分,其余为空白 截图调用

  • WebView 长截图

    WebView本来自带capturePicture截图方法,但是在L后该方法被标记为废弃,虽然还能用,官方建议用o...

  • iOS webView 截图

    pragma mark --------------------------------------------...

  • Andorid截图

    前言 最近项目里有用到截图,总结一些用法。 截图 获取View在屏幕可见区域的截图 webview截图 webvi...

  • iOS-webView截图

    开发中如何截图,webView为例,用法如下: (一)截取当前屏幕显示的区域(设备的全屏) 代码如下: (二)截取...

  • ios Webview截图总结

    对当前view可见区域截屏 关键api详细解 UIGraphicsBeginImageContextWithOpt...

  • Electron截图功能(webview)

    使用html2canvas遇到webview容易有跨域问题,所以使用原生capturePage方法

  • Flutter中对webview截图

    最近碰到个需求,在flutter中对webview进行截图,一想这还不简单,直接用Stack在webview上面放...

  • webView及ScrollView长截图

    前言 之前的文章已经讲了X5内核加入到项目中的方式,现在写一下如何在该内核下使用长截图,Android自带的web...

网友评论

      本文标题:webView 截图

      本文链接:https://www.haomeiwen.com/subject/ilpgsftx.html