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


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

/**
     * view转bitmap
     *
     * @param view view
     * @return 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;
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值