Android

截屏保存

2018-01-19  本文已影响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();

    }

}

上一篇 下一篇

猜你喜欢

热点阅读