Android takes screenshots based on the current Activity and androidactivity
Overview:
First, we need to know that the aspect of intercepting large images in Android can be divided into two directions: one is taking the bottom layer and the other is taking the top layer. Because the underlying code of the landlord is relatively weak, it is currently only at the + B level. Therefore, this blog only captures the screen at the application layer. Note: The two methods discussed above are two concepts in the game. The screenshots in the game can be understood as an illusion. What is an illusion? No screenshots! This is because full screen is usually used for playing games. In this case, you only need to save the images already saved in the memory.
For the application layer, it is based on Activity. It is only based on Activity, not necessarily by Activity. Isn't this a contradiction? How can this problem be understood? For example, if I want to cut a picture, I have to have one. As if I want to cut a graph of the currently running program, the Activity at the top of the screen is the content. In this example, I want to capture a picture, which can be Activity, Service, or broadcast.
Class Code:
The code of the encapsulated classes is as follows:
Public class ScreenShot {// obtain the ScreenShot of the specified Activity and save it to the png file private static Bitmap takeScreenShot (Activity activity) {// View is the ViewView view you need = activity. getWindow (). getDecorView (); view. setDrawingCacheEnabled (true); view. buildDrawingCache (); Bitmap bitmap = view. getDrawingCache (); // get the Rect frame of the status bar = new Rect (); activity. getWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); int statusBarHeight = fr Ame. top; System. out. println (statusBarHeight); // get the screen length and height int width = activity. getWindowManager (). getdefadisplay display (). getWidth (); int height = activity. getWindowManager (). getdefadisplay display (). getHeight (); // remove the title bar Bitmap B = Bitmap. createBitmap (bitmap, 0, statusBarHeight, width, height-statusBarHeight); view. destroyDrawingCache (); return B;} // save it to sdcardprivate static void savePic (Bitmap B, String strFileN Ame) {FileOutputStream fos = null; try {fos = new FileOutputStream (strFileName); if (null! = Fos) {B. compress (Bitmap. compressFormat. PNG, 90, fos); fos. flush (); fos. close () ;}} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} public static void shoot (Activity activity) {ScreenShot. savePic (ScreenShot. takeScreenShot (activity), "sdcard/apic/image" + System. currentTimeMillis () + ". png ");}}
The above comments are also relatively detailed, so we will not explain them too much here. It's just a matter of words.
Example:
ScreenShot.shoot(MainActivity.this);
Running legend:
<Update another method later...>