Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 截屏功能實作:使用 Drawing Cache 儲存畫面

編輯:關於 Android 程式設計

截屏的基本思路

Android 中可利用 View 的 Drawing Cache 取得畫面快照。做法是先開啟 drawing cache,再取得 Bitmap,最後壓縮成 PNG 或 JPEG 儲存。這種方式簡單快速,但通常不會包含狀態列。

實作步驟

首先在 AndroidManifest.xml 加入寫入外部儲存的權限。接著在需要截圖的頁面呼叫 getWindow().getDecorView(),啟用並建立 Drawing Cache,再將 Bitmap 寫入檔案。若需要分享或上傳,也可把檔案路徑交給其他元件處理。

注意事項

由於不同 Android 版本對儲存權限與檔案存取的限制不同,實作時要留意目標 SDK 與執行環境。若只是截取局部畫面,也可針對特定 View 建立 Drawing Cache,而不必抓取整個視窗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved