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

Android 获取屏幕截图(可视控件、布局) 获取控件截图(不可视控件、布局)

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

Android 获取屏幕截图(可视控件、布局) 获取控件截图(不可视控件、布局)

文章目录
  • 1、 获取可视控件、布局的截图
    • 1.1 方法:view.getDrawingCache()
    • 1.2 demo:获取屏幕截屏
  • 2、 获取不可视控件、布局的截图
    • 2.1 方法:Bitmap.createBitmap()
    • 2.2 demo:把一个xml布局文件转成bitmap
  • 3、view.getDrawingCache() 为 null 解决方案


1、 获取可视控件、布局的截图 1.1 方法:view.getDrawingCache() 1.2 demo:获取屏幕截屏
	
    public static Bitmap screenShotWithoutStatusBar(Activity activity) {
        //通过window的源码可以看出:检索顶层窗口的装饰视图,可以作为一个窗口添加到窗口管理器
        View view = activity.getWindow().getDecorView();
        //SYSTEM_UI_FLAG_FULLSCREEN表示全屏的意思,也就是会将状态栏隐藏
        //设置系统UI元素的可见性
        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
		
        //启用或禁用绘图缓存
        view.setDrawingCacheEnabled(true);
        //创建绘图缓存
        view.buildDrawingCache();
        //拿到绘图缓存
        Bitmap bitmap = view.getDrawingCache();
 
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayframe(frame);
        //状态栏高度
        int statusBarHeight = frame.top;
        int width = ScreenUtils.getScreenWidth();
        int height = ScreenUtils.getScreenHeight();
 
        Bitmap bp = null;
//        bp = Bitmap.createBitmap(bitmap, 0, 0, width, height - statusBarHeight);
        bp = Bitmap.createScaledBitmap(bitmap, width, height - statusBarHeight,true);
        view.destroyDrawingCache();
        view.setSystemUiVisibility(View.VISIBLE);
        return bp;
    }

2、 获取不可视控件、布局的截图 2.1 方法:Bitmap.createBitmap() 2.2 demo:把一个xml布局文件转成bitmap
    private Bitmap createBitmapByView() {
    	View view = LayoutInflater.from(activity).inflate(R.layout.img_qrcode, null, false);
        //计算设备分辨率
        WindowManager manager = activity.getWindowManager();
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
		int height = metrics.heightPixels;

        //测量使得view指定大小
        int measureWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measureHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);

        view.measure(measureWidth, measureHeight);
        //调用layout方法布局后,可以得到view的尺寸
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return bitmap;
    }

3、view.getDrawingCache() 为 null 解决方案
public static BitmapWithHeight getSimpleViewToBitmap(final View view, int width) throws OutOfMemoryError {
        if (view.getWidth() <= 0 || view.getHeight() <= 0) {
            view.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        }
        view.destroyDrawingCache();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap map = view.getDrawingCache();
        if (view.getHeight() > 0 && map == null)
            map = getViewBitmap(view);
        return new BitmapWithHeight(map, view.getMeasuredWidth(), view.getMeasuredHeight());
    }


    public static Bitmap getViewBitmap(View view) {
        Bitmap bitmap;
        if (view.getWidth() > 0 && view.getHeight() > 0)
            bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        else if (view.getMeasuredWidth() > 0 && view.getMeasuredHeight() > 0)
            bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        else
            bitmap = Bitmap.createBitmap(com.blankj.utilcode.utils.ScreenUtils.getScreenWidth(), com.blankj.utilcode.utils.ScreenUtils.getScreenHeight() * 2, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591992.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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