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

Android 應用內截屏實作:把 DecorView 畫進 Bitmap

編輯:關於 Android 程式設計

想在 App 內做「截圖分享」功能,最簡單的思路是把當前的視圖樹直接畫到一張 Bitmap 上。以下示範這個做法。

要說明的是,這個截屏並不完整——頂部的 status bar 沒有被擷取,呈現黑色。這是因為 getWindow().getDecorView() 雖然包含標題區,但系統 UI(狀態列、導覽列)屬於另一個視窗,畫不出來。

程式碼

package com.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ScreenPrinter extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_printer);

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                ImageView im = (ImageView) findViewById(R.id.img);
                Bitmap bmp = Bitmap.createBitmap(320, 480, Config.ARGB_8888);
                View cv = getWindow().getDecorView();
                cv.draw(new Canvas(bmp));
                im.setScaleType(ScaleType.FIT_XY);
                im.setImageBitmap(bmp);
            }
        });
    }
}

原理就是三行:createBitmap 開一張畫布、getDecorView() 拿到根視圖、draw(Canvas) 把整棵視圖樹畫上去。

版面檔

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/border">

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="截屏"/>

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="10dip"
        android:background="#ffffdd">
        <ImageView android:id="@+id/img"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</LinearLayout>

這個做法的限制與改進方向

  • 尺寸寫死了。範例中 Bitmap 固定 320×480,實務上應該動態取得:decorView.getWidth() 與 decorView.getHeight(),否則不同解析度的裝置上會變形。
  • 擷不到系統 UI。要包含狀態列,得改用其他途徑(見下方)。
  • SurfaceView / TextureView 內容畫不出來。這兩類元件的內容不在 View 層級,draw(Canvas) 會得到空白。視頻、相機預覽、遊戲畫面都屬於這種情況。
  • 效能與記憶體。全螢幕 ARGB_8888 的 Bitmap 相當吃記憶體(1080×1920 約 8 MB),用完記得 recycle(),並避免在主線程做太頻繁的截圖。

想截到完整螢幕(含狀態列)怎麼辦

Android 5.0 以上:用 MediaProjection + ImageReader 取得系統畫面。需要跳出權限確認視窗讓用戶授權,而且要在 VirtualDisplay 的 callback 中從 ImageReader.acquireLatestImage() 取出影像,記得呼叫 image.close() 釋放。

使用者操作型截圖:直接呼叫系統的截圖 Intent,讓用戶自行截圖再回來處理,是最省事的做法。

root 或系統簽名:可以讀取 /dev/graphics/fb0,但這不適用於一般上架 App。

更實用的做法:只截特定區域

如果目的只是「把某個 View 轉成圖片分享」,不必截整個螢幕,直接對目標 View 這樣做:

public static Bitmap getViewBitmap(View view) {
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    return bitmap;
}

這種做法記憶體占用小得多,也不受系統 UI 影響,做「分享海報」、「長圖產生」時更合適。

以上就是 App 內截屏的基本實作與各種限制說明,依需求選擇「視圖轉圖」或「MediaProjection 截螢幕」才是正確的判斷。

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