android webview 截图快照

本文介绍了三种在Android中截取WebView屏幕快照的方法,包括使用capturePicture()、getDrawingCache()以及直接绘制到Bitmap上。方法二在平板竖屏时可能因尺寸限制导致截图失败,而方法三则具有较高的成功率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:    

Picture snapShot = view.capturePicture();


            if (snapShot.getWidth() > 0 && snapShot.getHeight() > 0) {
                Bitmap b = Bitmap.createBitmap(snapShot.getWidth(),
                        snapShot.getHeight(), Bitmap.Config.ARGB_8888);

                Canvas c = new Canvas(b);

                snapShot.draw(c);

               Bitmap newBitmap = zoomBitmap(b, 250, 250);
                historyModel.setSnapshot(newBitmap);


方法二(经过测试,平板竖屏是经常截图失败, 原因是root.getDrawingCache()尺寸超过限制):

View root = activity.getWindow().getDecorView();

    private Bitmap catchScreen(View root) {
        root.setDrawingCacheEnabled(true);
        root.setDrawingCacheBackgroundColor(0);
        root.buildDrawingCache(true);
        Bitmap b = root.getDrawingCache();
        return b;
    }



方法三(成功率高):

            // create snapshot for webview
            View cv = activity.getWindow().getDecorView();
            Bitmap b = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(),
                    Config.ARGB_4444);
            cv.draw(new Canvas(b));



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值