Android third-party app screenshot

It is very easy to generate the corresponding image from the View, so the screenshot in the app can be used in this way.

view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap screenshot = view.getDrawingCache();
//Execute destroyDrawingCache after using the picture, because destroyDrawingCache will call bitmap.recycle() internally
view.destroyDrawingCache();
view.setDrawingCacheEnabled(false);

After getting the bitmap, it needs to be processed, which is divided into the following parts/situations:

1. Status bar

2. Navigation bar

3. The smartbar of Meizu flyme

 

For the status bar, the app cannot take a screenshot. The screenshot is only the background color of the status bar, and the information such as the notification, time and power above is not available, so the status bar is generally removed.

For the navigation bar, this can be intercepted, but the content has nothing to do with the app, just 3 buttons, which may not look good, so it is generally removed.

In addition, Meizu also has a smartbar, which generally needs to be killed.

For the above 3 items, we only need to know its height, and then remove these parts (horizontal screen is not considered here)

//width is the width of the screenshot, height is the final image height, that is, the height after processing the status bar/navigation bar/smartbar
//top is the height of the status bar
Bitmap bg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
canvas.drawBitmap(screenshot, width, top, null);
screenshot.recycle();

 

The following is a special treatment for Meizu's flyme

Judge the flyme system

/**
 * Determine whether it is a Meizu system
 * @return
 */
public static boolean isFlyme() {
    try {
        // Invoke Build.hasSmartBar()
        final Method method = Build.class.getMethod("hasSmartBar");
        return method != null;
    } catch (final Exception e) {
        return false;
    }
}

Get smartbar height

//Get the height of the Meizu smartbar
public static int getSmartBarHeight(Context context) {
    try {
        Class clazz = Class.forName("com.android.internal.R$dimen");
        Object obj = clazz.newInstance();
        Field field = clazz.getField("mz_action_button_min_height");
        int height = Integer.parseInt(field.get(obj).toString());
        return context.getResources().getDimensionPixelSize(height);
    } catch (ClassNotFoundException e) {
        Log.w(TAG, e);
    } catch (InstantiationException e) {
        Log.w(TAG, e);
    } catch (IllegalAccessException e) {
        Log.w(TAG, e);
    } catch (NoSuchFieldException e) {
        Log.w(TAG, e);
    }
    return 0;
}

How to get the height of the status bar/navigation bar will not be repeated here

 

 

The above is the general case, let's talk about some special cases

1. How to intercept the dialog

If you directly get the DecorView in the window in the activity, the captured picture actually has no dialog part, only the page below the dialog. To get a screenshot of the dialog, we can first get the dialog reference, and then get

dialog.getWindow().getDecorView()

2. When the page does not fill the screen

For example, sometimes we will make the page into a half-window form, and we can still see the content of the previous page at the top of the screen. At this time, the activity only has the part at the bottom of the screen, so the part of the status bar cannot be subtracted when processing the screenshot, because there is no status bar on the screenshot at this time.

3. webview screenshot

On the 4.3 system, using the getDecorView().getDrawingCache() method sometimes cannot accurately capture the content displayed on the webview. When a page in the form of a tab is displayed in the webview, sometimes no matter which tab it is currently in, the screenshot is the content of the first tab.

However, if you use webview.getDrawingCache(), you can get the correct picture, so I take screenshots in a page containing a webview by traversing the views in the page, and then piecing the pictures together

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326150333&siteId=291194637