TextureView和界面View截图整合

业务需求:最近做TV业务,使用 TextureView 播放视频,底部有滚动字幕。这里截图需要全部截取到。如果root的话可以用指令screencap -p 去截图。这里没有Root的情况。

/**
     * 获取和保存当前屏幕的截图 返回真实路径
     * <p>
     * 这里TextureView 实现 textureview + 整个ViewGroup 截图 =叠加 Canva绘制
     */
    public String cropVideoImage(TextureView textureView, View layoutView, WindowManager windowManager) {
        if (textureView == null || layoutView == null) {
            return null;
        }

        File imgFile = new File(dirFile, UUID.randomUUID() + ".png");
        Bitmap content = textureView.getBitmap();
        layoutView.setDrawingCacheEnabled(true);
//        Bitmap layout = layoutView.getDrawingCache();
        Bitmap layout = convertViewToBitmap(layoutView);
        Bitmap screenshot = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_8888);

        Display defaultDisplay = windowManager.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        defaultDisplay.getMetrics(metrics);

        //拼接
        Canvas canvas = new Canvas(screenshot);
        canvas.drawBitmap(content, (layout.getWidth() - content.getWidth()) / 2, (layout.getHeight() - content.getHeight()) / 2, new Paint());
        canvas.drawBitmap(layout, 0, 0, new Paint());
        canvas.save();
        canvas.restore();

        OutputStream fout = null;
        try {
            fout = new FileOutputStream(imgFile);
            screenshot.compress(Bitmap.CompressFormat.PNG, 70, fout);
            fout.flush();
            fout.close();

            return imgFile.getAbsolutePath();
        } catch (FileNotFoundException e) {
            Logger.d(TAG, "FileNotFoundException");
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            Logger.d(TAG, "IOException");
            e.printStackTrace();
            return null;
        }
    }

    public Bitmap convertViewToBitmap(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,240评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 远房亲戚家的表妹,产假即将休完,但接到单位通知,要她不用急于回来上班,工资照发,社保照交,可以继续休一段。 她刚开...
    沫Jenny阅读 3,890评论 0 0
  • 那女子决绝的转身而去,踉踉跄跄的走了几步,回过身来。俏脸已不似平日的桃李花色,苍白无比,泪珠在脸上慢慢划落。四月的...
    祝游阅读 1,300评论 0 0
  • 从早期的少年科学俱乐部开始鹤田谦二就一直是sf漫画爱好者。作为漫画家画力已然已经是艺术领域了。斩获了好几届星云赏。...
    伊蒂雅阅读 5,371评论 4 9