栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 视图截屏小结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 视图截屏小结

需求

把UI布局转换成图片保存到本地或者分享出去。

Activity 布局 测量过UI布局(已经绘制显示屏幕上)
public static Bitmap convertViewToBitmap(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();
        return bitmap;
    }
未测量过UI布局(未显示屏幕上)
public static Bitmap convertMeasureViewToBitmap(View view) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED));
        view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.scale(1, 1);
        view.draw(canvas);
        return bitmap;
    }
WebView 截图 第一种
//获取webview缩放率
float scale = wv_capture.getScale();
//得到缩放后webview内容的高度
int webViewHeight = (int) (wv_capture.getContentHeight()*scale);
Bitmap bitmap = Bitmap.createBitmap(wv_capture.getWidth(),webViewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//绘制
wv_capture.draw(canvas);
第二种

通过获取当前window的DecorView,然后绘制Bitmap对象。

View view  = context.getWindow().getDecorView();
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995860.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号