Overview:
The first thing to know about the big picture in Android is that it can be divided into two directions, one to the bottom and the other to the top. Because the landlord lower code than the weak, at present, just stay in the a+b level. So, this blog simply intercepts the screen on the application layer. Note that the two methods discussed above are two concepts in the game, and the interception of the screen in the game can be interpreted as an illusion. What kind of an illusion? No screenshot! Because when playing the game, is generally full screen, this time just save the memory of the image can be saved.
For the application level, it is activity-based. It's just that it's based on activity, not that it must come from the activity. Is that not a contradiction in that sentence? How do you understand it? For example, I want to cut a picture now, then I have to have a content. It's like saying I want to take a screenshot of the currently running program, so the activity that is currently at the top of the screen is this content. I'd like to cut a picture. I can be an activity, a service, or a broadcast.
Class Code:
Below you can look at the code of the class that has been encapsulated, as follows:
public class Screenshot {//Get screenshot of specified activity, save to PNG file private static Bitmap Takescreenshot (activity activity) {// View is what you need viewview view = Activity.getwindow (). Getdecorview (); view.setdrawingcacheenabled (true); View.builddrawingcache (); Bitmap Bitmap = View.getdrawingcache ();//Gets the status bar height Rect frame = new Rect (); Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame); int statusbarheight = Frame.top; System.out.println (statusbarheight);//Get screen length and height int width = activity.getwindowmanager (). Getdefaultdisplay (). getwidth (); int height = Activity.getwindowmanager (). Getdefaultdisplay (). GetHeight ();//Remove title bar Bitmap B = Bitmap.createbitmap ( Bitmap, 0, statusbarheight, width, height-statusbarheight); View.destroydrawingcache (); return b;} Save to Sdcardprivate static void Savepic (Bitmap B, String strFileName) {FileOutputStream fos = null;try {fos = new FILEOUTP Utstream (strFileName); if (null! = FOS) {b.compress (Bitmap.CompressFormat.PNG, N, 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 left in relative detail, so there is no explanation. Said it is only the number of words, do not have to.
Use demonstration:
Screenshot.shoot (Mainactivity.this);
To run the legend:
< Next update another method ... >
Android takes a screenshot of the current activity as a benchmark