您的位置:首页 > 移动开发 > Android开发

Android 整个手机屏幕截图和去除状态栏截图

2017-09-13 10:31 369 查看
一、去除状态栏截图





//整个手机屏幕的视图
View view = getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();

Bitmap bitmap = view.getDrawingCache();

// 获取状态栏高度
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.i("TAG", "" + statusBarHeight);

// 获取屏幕长和高
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();

Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight);

//保存图片
FileOutputStream fout = null;
try {
fout = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
b.compress(Bitmap.CompressFormat.PNG, 100, fout);
//显示截图
mImageView.setImageBitmap(b);


二、整个手机屏幕截图,但状态栏是空白





View view = getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();

FileOutputStream fout = null;
try {
fout = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
mImageView.setImageBitmap(bitmap);


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  位图