Copy Code code as follows:
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.Rect;
Import Android.util.Log;
Import Android.view.View;
public class Screenshot {
Gets the screenshot of the specified activity and saves it to the PNG file
private static Bitmap Takescreenshot (activity activity) {
View is the view you need to capture.
View view = Activity.getwindow (). Getdecorview ();
View.setdrawingcacheenabled (TRUE);
View.builddrawingcache ();
Bitmap B1 = View.getdrawingcache ();
Get status bar height
Rect frame = new Rect ();
Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
int statusbarheight = Frame.top;
LOG.I ("TAG", "" + 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 (B1, 0, 25, 320, 455);
Bitmap B = Bitmap.createbitmap (b1, 0, statusbarheight, width, height
-Statusbarheight);
View.destroydrawingcache ();
return b;
}
Save to SDcard
private static void Savepic (Bitmap B, String strFileName) {
FileOutputStream fos = null;
try {
FOS = new FileOutputStream (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 ();
}
}
Program entry
public static void shoot (activity a) {
Screenshot.savepic (Screenshot.takescreenshot (a), "sdcard/xx.png");
}
}
It should be noted that the shoot method can only be invoked after the view has been loaded.
or in
Copy Code code as follows:
@Override
public void Onwindowfocuschanged (Boolean hasfocus) {
TODO auto-generated Method Stub
Super.onwindowfocuschanged (Hasfocus);
Screenshot.shoot (this);
Called in