Summary of implementation methods of android screenshot function [including background screenshot]

Preface

For Android to implement the screenshot function, briefly describe the feasible methods and their pros and cons.



Use canvas

View v = getWindow().getDecorView();
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap);
v.draw(canvas);

ImageView image = findViewById(R.id.image);
image.setImageBitmap(bitmap);

The above is the simplest form. You can change the current page into a canvas bitmapand then load it directly into ImageViewit to see the effect.
advantage:

  1. Simple to implement
  2. No permission required

shortcoming:

  1. Only application pages can be captured, the status bar will not be captured, and the requirement for full-screen screenshots does not apply.
  2. Because getWindow()it is a method in Activity, it can only be called in Activity (cannot take screenshots in the background)


MediaProjectionManager

MediaProjectionManagerIt is a screen recording API. We can get a screenshot by getting one of the frames. There are many tutorials on this method online, you can search them by yourself.
Advantages:
No advantages, just one more screenshot solution

shortcoming:

  1. The code is more complicated
  2. Before each screenshot, a pop-up window will pop up to obtain permission, asking whether to allow screen recording, which is fatal to the user experience.
  3. Unable to take screenshots in the background because onActivityResult()methods in Activity need to be called


UseSurfaceControl.screenshot

SurfaceControl.screenshotIt is the system's screenshot API and is not restricted by Activity, so it can be used to implement functions such as background screenshots, full-screen screenshots, and designated area screenshots.
For specific usage, please refer to my article: Advantages of SurfaceControl.screenshot() usage
:

  1. Unlimited use, can be used in the background
  2. Powerful and easy to call
  3. There is no need to apply for permission from the user, and no pop-up windows are displayed.

shortcoming:

  1. System API must have system signature and system-level permissions. Ordinary apps cannot use this method.


Create a transparent activity in the service and call the screenshot method in the activity

This method has not been tested, but service can indeed create an activity in the background, but there are many restrictions (please refer to my article: Why starting an activity from the background is invalid ). At the same time, although the activity is transparent, the animation effect of page switching may expose the behavior of the app. In short, it is not an effective method.



Modify and compile the screencap class in the source code

Advantages:
The advantages are not big. It is not recommended to modify the system class. If the strength allows, using this method can avoid some convoluted calling methods and system restrictions.

Disadvantages:
Difficult, requires the ability to compile and modify source code

Guess you like

Origin blog.csdn.net/Guan_li_peng/article/details/128346206