webView 截图

最近项目有个 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();
            }
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,305评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,946评论 0 17
  • 开发中如何截图,webView为例,用法如下: (一)截取当前屏幕显示的区域(设备的全屏) 代码如下: (二)截取...
    Freedom_fly阅读 7,816评论 0 2
  • 一、异常定义:中断了正常指令流的事件class Test {public static void main (St...
    tah阅读 1,522评论 0 0
  • 初五,懒懒地起了床,发现微信圈里都在接财神爷了。我知道这已经不是从前意义上的封建迷信了,而是人们新年里的一...
    东观阅读 3,777评论 0 1

友情链接更多精彩内容