PackageCom.fredric.util;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.util.DisplayMetrics;ImportAndroid.view.View;ImportAndroid.view.WindowManager;/*-* Screen interaction class Encapsulation*/ Public classScreenutils {//Get screen width Public Static intGetscreenwidth (Context context) {WindowManager WM=(WindowManager) context. Getsystemservice (Context.window_service); //Displaymetrics provides general information about the realityDisplaymetrics display =NewDisplaymetrics (); //Place the current window information in displayWm.getdefaultdisplay (). Getmetrics (display); //returns absolute pixels returnDisplay.widthpixels; } //Get screen height Public Static intGetscreenheight (Context context) {WindowManager WM=(WindowManager) context. Getsystemservice (Context.window_service); Displaymetrics Display=NewDisplaymetrics (); Wm.getdefaultdisplay (). Getmetrics (display); returnDisplay.heightpixels; } //Screen Cutting Public StaticBitmap getsnapshot (activity activity) {//Decorview is the topmost view of windowView view =Activity.getwindow (). Getdecorview (); //the contents of the view component are saved to bitmap via the cache mechanism//setdrawingcacheenabled Open Cache//Getdrawingcache Get the cache picture for view//Builddrawingcache is not called, if the cache is not established when Getdrawingcache is called//Destorydrawingcache Destroy cache,setdrawingcacheenabled (false) can also destroy cache//cache to update you must first destroy the old cacheView.setdrawingcacheenabled (true); Bitmap BP=NULL; intwidth =getscreenwidth (activity); intHeight =getscreenheight (activity); BP= Bitmap.createbitmap (View.getdrawingcache (), 0, 0, width, height); View.destroydrawingcache (); returnBP; }}
Public Code Reference (displaymetrics)