截屏

作者: 霁逸lei | 来源:发表于2022-09-09 17:32 被阅读0次
    public static Bitmap takeScreenShot(Activity activity) {
            // View是你需要截图的View
            View view = activity.getWindow().getDecorView();
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            Bitmap bitmap = view.getDrawingCache();
    
            // 获取状态栏高度
            Rect frame = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            int statusBarHeight = frame.top;
    
            Bitmap bmp1 = Bitmap.createBitmap(bitmap, 0, statusBarHeight, view.getMeasuredWidth(), frame.height());
            view.setDrawingCacheEnabled(false);
            view.destroyDrawingCache();
            return bmp1;
        }
    
    
        public static void savePic(Bitmap bmp, String strFileName) {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(strFileName);
                if (null != fos) {
                    bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
                    fos.flush();
                    fos.close();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    相关文章

      网友评论

          本文标题:截屏

          本文链接:https://www.haomeiwen.com/subject/znwprrtx.html