———-截屏方法
[java]
private Bitmap shot() {
View views = getWindow().getDecorView();
views.buildDrawingCache();
// 獲取狀態欄高度
Rect frames = new Rect();
views.getWindowVisibleDisplayFrame(frames);
int statusBarHeights = frames.top;
Display display = getWindowManager().getDefaultDisplay();
int widths = display.getWidth();
int heights = display.getHeight();
//第一種方式
views.layout(0, statusBarHeights,widths, heights – statusBarHeights);
views.setDrawingCacheEnabled(true);//允許當前窗口保存緩存信息 ,兩種方式都需要加上
Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache());
//第二種方式
// 1、source 位圖 2、X x坐標的第一個像素 3、Y y坐標的第一個像素 4、寬度的像素在每一行 5、高度的行數
//Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache(), 0, statusBarHeights,widths, heights – statusBarHeights);
return bmp;
}
————保存到SD卡方法
[java]
try {
String status = Environment.getExternalStorageState();
// 判斷SD卡是否存在
if (status.equals(Environment.MEDIA_MOUNTED)) {
File destDir = new File("文件夾名");
if (!destDir.exists()) {
// 創建文件夾
destDir.mkdirs();
}
File file = new File("圖片名");
// 判斷文件夾是否存在
if (file.exists()) {
String pic_path ="文件夾名" +"圖片名"+".png";
FileOutputStream out = new FileOutputStream(pic_path);
shot().compress(Bitmap.CompressFormat.PNG,100, out);
out.flush();
out.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
——–把Bitmap轉為Drawable 放進imageView中
[java]
//Bitmap–>Drawable
BitmapDrawable bd=new BitmapDrawable(shot());
imageView.setBackgroundDrawable(bd);
imageView.setImageBitmap(shot());