1. 程式人生 > >Android獲取當前螢幕截圖,簡單粗暴!

Android獲取當前螢幕截圖,簡單粗暴!

獲取當前螢幕截圖的Bitmap物件,然後就可以拿去做你想做的事情,程式碼很簡單。

    public static Bitmap captureScreen(Activity activity) {

        activity.getWindow().getDecorView().setDrawingCacheEnabled(true);

        Bitmap bmp=activity.getWindow().getDecorView().getDrawingCache();

        return bmp;

    }

通過API文件來解釋一下。

    public Window getWindow ()                      Added in API level 1
    Retrieve the current Window for the activity. This can be used to directly access parts of the Window API that are not available through Activity Scre    en.

    Returns
    Window The current window, or null if the activity is not visual.
               Activity的getWindow()方法,得到activity的當前視窗,返回一個Window物件
 public abstract View getDecorView ()                 Added in API level 1
 Retrieve the top-level window decor view (containing the standard window frame/decorations and the client's content inside of that), which can be added   as a window to the window manager.

 Note that calling this function for the first time "locks in" various window characteristics as described in setContentView(View, android.view.ViewGroup. LayoutParams).

 Returns the top-level window decor view.
Window的getDecorView()方法,得到當前Window的最頂層View,返回一個View物件
 public void setDrawingCacheEnabled (boolean enabled)               Added in API level 1
 Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the view in a bitmap. Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the d rawing cache by calling getDrawingCache() and draw it on screen if the returned bitmap is not null.

 Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the  drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitm ap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and  hardware layers.

 This API can be used to manually generate a bitmap copy of this view, by setting the flag to true and calling getDrawingCache().

 Parameters
 enabled	true to enable the drawing cache, false otherwise
View的setDrawingCacheEnabled()方法,設定為true,開啟了cache,意思就是generate a bitmap copy of this view,允許生成對當前view的一個bitmap形式的複製,這樣才可以呼叫getDrawingCache()方法。
 public Bitmap getDrawingCache ()                                 Added in API level 1
 Calling this method is equivalent to calling getDrawingCache(false).

 Returns
 A non-scaled bitmap representing this view or null if cache is disabled.
View的getDrawingCache()方法,在設定setDrawingCacheEnabled(true)之後,會對這個View進行儲存生成bitmap,這個方法會將這個bitmap返回。

這樣就完成了對當前螢幕的截圖操作,拿到bitmap之後,我們就可以進行顯示或者儲存等操作了。

有任何疑問,歡迎加群討論:261386924