admin 管理员组文章数量: 1184232
/**
*
* @param view 需要截取图片的view
* 传入线性或相对布局就截取里面的所有内容
* @return 截图
*/
private Bitmap getBitmap(View view) throws Exception {
View screenView = getWindow().getDecorView();
screenView.setDrawingCacheEnabled(true);
screenView.buildDrawingCache();
//获取屏幕整张图片
Bitmap bitmap = screenView.getDrawingCache();
if (bitmap != null) {
//需要截取的长和宽
int outWidth = view.getWidth();
int outHeight = view.getHeight();
//获取需要截图部分的在屏幕上的坐标(view的左上角坐标)
int[] viewLocationArray = new int[2];
view.getLocationOnScreen(viewLocationArray);
//从屏幕整张图片中截取指定区域
bitmap = Bitmap.createBitmap(bitmap, viewLocationArray[0], viewLocationArray[1], outWidth, outHeight);
Toast.makeText(context, "截图成功", Toast.LENGTH_SHORT).show();
view.setDrawingCacheEnabled(false); //禁用DrawingCahce否则会影响性能
}
return bitmap;
}
//保存图片到系统图库
private void onSaveBitmap(final Bitmap mBitmap, final Context context) {
//将Bitmap保存图片到指定的路径/sdcard/Boohee/下,文件名以当前系统时间命名,但是这种方法保存的图片没有加入到系统图库中
File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(context, "保存图片成功", Toast.LENGTH_SHORT).show();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file)));
}版权声明:本文标题:Android开发者必备:快速获取屏幕特定部分图片的实用指南 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1773355793a3561000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论