安卓截屏分享功能实现
应产品需求,需要在项目中添加一个截屏分享功能,操作流程如下:
这里有两种截屏方式,一是用系统截屏,二是点击应用控件截屏。由于安卓机型太多太杂,各系统截屏都有分享功能,没必要去重复造轮子,浪费时间,故这里只做了点击控件截屏实现分享功能,具体效果图如下:
部分代码分享如下:
private void screenshot2() {
// 获取屏幕
View dView = getWindow().getDecorView();
// 允许当前窗口保存缓存信息
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bmp = dView.getDrawingCache();
mCameraSound.play(MediaActionSound.SHUTTER_CLICK);
if (bmp != null) {
try {
// 获取状态栏高度
Rect rect = new Rect();
dView.getWindowVisibleDisplayFrame(rect);
int statusBarHeights = rect.top;
Display display = getWindowManager().getDefaultDisplay();
int widths = display.getWidth();
int heights = display.getHeight();
// 去掉状态栏
saveBitmap = Bitmap.createBitmap(dView.getDrawingCache(), 0, statusBarHeights,
widths, heights - statusBarHeights);
saveToCamara(dView);
startAnim();
} catch (Exception e) {
e.printStackTrace();
}
} else {
// initHandler.sendMessage(Message.obtain());
}
}