截屏保存

作者: 简书_朱聪 | 来源:发表于2018-01-19 11:12 被阅读0次

// 获取指定Activity的截屏,保存到png文件

private static Bitmap takeScreenShot(Activity activity) {

// View是你需要截图的View

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

    view.setDrawingCacheEnabled(true);

    view.buildDrawingCache();

    Bitmapb1 =view.getDrawingCache();

    // 获取状态栏高度

    Rectframe =new Rect();

    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

    int statusBarHeight =frame.top;

    Log.i("TAG", "" +statusBarHeight);

    // 获取屏幕长和高

    int width = activity.getWindowManager().getDefaultDisplay().getWidth();

    int height = activity.getWindowManager().getDefaultDisplay().getHeight();

    // 去掉标题栏

// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);

    Bitmapb =Bitmap.createBitmap(b1, 0, statusBarHeight, width, height

            -statusBarHeight);

    view.destroyDrawingCache();

    return b;

}

// 保存指定位置

private static void savePic(Bitmap b, String strFileName) {

try {

FileOutputStream fos =new FileOutputStream(strFileName);

            b.compress(Bitmap.CompressFormat.PNG, 90, fos);

            fos.flush();

            fos.close();

    }catch (Exception e) {

e.printStackTrace();

    }

}

相关文章

网友评论

    本文标题:截屏保存

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