简体   繁体   中英

How to get pixel bitmap of everything, including Dialogs and Toasts, in Android?

I've done some search in the internet. Some suggests use of PixelCopy API or Canvas. However, either way is limited to get the bitmap of the decor view. If there is a toast or AlertDialog on top of current activity, their images would not appear in the final bitmap.

So my question is, can I get bitmap of the whole activity, not only view, so the bitmap contains any Dialog or Toast on top of the activity?

So my question is, can I get bitmap of the whole activity, not only view, so the bitmap contains any Dialog or Toast on top of the activity?

Yes, we can use the window manager to get access to all the views in the hierarchy.

How to achieve this:

  1. Get window managger for the activity, which is responsible for managing an ordered list of windows and contains the info on how they are laid out on the screen. This gives Object holding the view hierarchy you needed.
  2. For the targeted class from the object, get the declared fields using the getDeclaredFields() method. This gives all the view fields declared for the given activity view. You can differentiate the view hierarchy with field labels mGlobal , mRoots , and mParams .
  3. This gives you access to all the view fields declared in the activity.

A thoughtful implementation of this is done here Falcon .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM