Android takes a screenshot of the current activity as a benchmark

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.