Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Screenshot of rectangular area in Android

Shulou Source: shulou.com Published: 2022-05-31 23:49:22 11月15日 Update

< 0) mRect.left = 0; if (mRect.right < 0) mRect.right = 0; if (mRect.top < 0) mRect.top = 0; if (mRect.bottom < 0) mRect.bottom = 0; int cut_width = Math.abs(mRect.left - mRect.right); int cut_height = Math.abs(mRect.top - mRect.bottom); if (cut_width >

0 & & cut_height > 0) {Bitmap cutBitmap = Bitmap.createBitmap (bitmap, mRect.left, mRect.top, cut_width, cut_height);}

It should be noted that when calling the system screenshot feature, if the phone has NavigationBar (virtual navigation bar), the value of windowHeight does not include the height of NavigationBar. If it is not adjusted, it will cause the screenshot to be compressed. How to get the true height of the screen, you can refer to Android how to determine whether the NavigationBar is displayed (get the true height of the screen).

In addition, NavigationBar will also cause a border in the result of the screenshot, and the color of the frame is transparent. This is due to the rowPaddingscreenshot 0 in the code in step 2. The screenshot is shown below:

Result of screenshot of using system with NavigationBar

So if we want to save or crop the results of the screenshot, we must remove the border and find out the real content area, that is, the content between * opaque pixels and * one opaque pixel. Then we can crop the resulting area in step 3. The code is as follows:

Int [] pixel=new int [width]; bitmap.getPixels (pixel,0,width, 0Widthjee 1); int leftPadding=0;int rightPadding=width;for (int ionomy 0 / 0 / 0 / 10 / 10 / 2) {if (pixel [I]! = 0) {rightPadding=i; break;}} bitmap=Bitmap.createBitmap (bitmap,leftPadding, 0, rightPadding-leftPadding, height)

The screenshot after processing is as follows:

Get the content part of the screenshot result

You might think that since rowPaddingwidth 0 caused the border to appear, and the border is only on the right, why not just truncate the width of the rowPadding on the right? In fact, it is because if you do not adjust the windowHeight, there will be a box on the left, so the above method is used.

This is the end of the introduction on "how to achieve screenshots of rectangular areas in Android". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Area screenshot rectangle content result border function code location screen knowledge system height processing adjustment different pixel right that is button Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Shulou Tech Info Shulou Information Xiaomi