Android截屏關鍵代碼

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   os   ar   java   

[java] view plaincopy
  1. package com.wangzhen.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7.   
  8. import android.app.Activity;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.Rect;  
  11. import android.view.View;  
  12.   
  13. public class ScreenShot {  
  14.   
  15.     // 進行截屏  
  16.     private static Bitmap takeScreenShot(Activity activity) {  
  17.         // view是將要截屏的view  
  18.         View view = activity.getWindow().getDecorView();  
  19.         view.setDrawingCacheEnabled(true);  
  20.         view.buildDrawingCache();  
  21.         Bitmap bitmap = view.getDrawingCache();  
  22.   
  23.         // 擷取狀態列高度  
  24.         Rect rect = new Rect();  
  25.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);  
  26.         int statusBarHeight = rect.top;  
  27.   
  28.         // 擷取螢幕長和高  
  29.         int width = activity.getWindowManager().getDefaultDisplay().getWidth();  
  30.         int height = activity.getWindowManager().getDefaultDisplay()  
  31.                 .getHeight();  
  32.         // 去除標題列  
  33.         Bitmap bitmap_final = Bitmap.createBitmap(bitmap, 0, statusBarHeight,  
  34.                 width, height - statusBarHeight);  
  35.         view.destroyDrawingCache();  
  36.         return bitmap_final;  
  37.     }  
  38.   
  39.     // 儲存  
  40.     private static void savePic(Bitmap bitmap, File filepath) {  
  41.         FileOutputStream fileOutputStream = null;  
  42.         try {  
  43.             fileOutputStream = new FileOutputStream(filepath);  
  44.             if (null != fileOutputStream) {  
  45.                 bitmap.compress(Bitmap.CompressFormat.PNG, 100,  
  46.                         fileOutputStream);  
  47.                 fileOutputStream.flush();  
  48.                 fileOutputStream.close();  
  49.             }  
  50.         } catch (FileNotFoundException e) {  
  51.             // TODO Auto-generated catch block  
  52.             e.printStackTrace();  
  53.             System.out.println(e.getMessage());  
  54.         } catch (IOException e) {  
  55.             // TODO Auto-generated catch block  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.   
  60.     // 外部進行調用的方法  
  61.     public static void shoot(Activity activity, File filePath) {  
  62.         if (filePath == null) {  
  63.             return;  
  64.         }  
  65.         if (!filePath.exists()) {  
  66.             filePath.getParentFile().mkdir();  
  67.         }  
  68.         ScreenShot.savePic(ScreenShot.takeScreenShot(activity), filePath);  
  69.     }  

Android截屏關鍵代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.