-----------截屏方法
- private Bitmap shot() {
- View views = getWindow().getDecorView();
- views.buildDrawingCache();
-
- // 獲取狀態(tài)欄高度
- 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、高度的行數(shù)
- //Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache(), 0, statusBarHeights,widths, heights - statusBarHeights);
- return bmp;
- }
------------保存到SD卡方法
- try {
- String status = Environment.getExternalStorageState();
- // 判斷SD卡是否存在
- if (status.equals(Environment.MEDIA_MOUNTED)) {
- File destDir = new File("文件夾名");
-
- if (!destDir.exists()) {
- // 創(chuàng)建文件夾
- 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轉(zhuǎn)為Drawable 放進imageView中
- //Bitmap-->Drawable
- BitmapDrawable bd=new BitmapDrawable(shot());
- imageView.setBackgroundDrawable(bd);
- imageView.setImageBitmap(shot());
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。