We need to get the physical dimensions of the Android phone or pad screen to facilitate the design of the interface or the implementation of other functions. Let's share some of the most common ways to help Android:
Get screen Height:
/**
* Get screen height
* @param context
* @return
* by Hankkin at:2015-10-07 21:15:59/public
static int getscreenwidth {
WindowManager wm = (WindowManager) context
. Getsystemservice ( Context.window_service);
Displaymetrics outmetrics = new Displaymetrics ();
Wm.getdefaultdisplay (). Getmetrics (outmetrics);
return outmetrics.widthpixels;
}
Get screen width:
/**
* Get screen width
* @param context
* @return
* by Hankkin at:2015-10-07 21:16:13/public
static int getscreenheight {
WindowManager wm = (WindowManager) context
. Getsystemservice ( Context.window_service);
Displaymetrics outmetrics = new Displaymetrics ();
Wm.getdefaultdisplay (). Getmetrics (outmetrics);
return outmetrics.heightpixels;
}
Get screen density:
/** *
Get screen density
* @param context
* @return
* by Hankkin at:2015-10-07 21:16:29
/Public Static float getscreendensity {return
context.getresources (). Getdisplaymetrics (). density;
}
Dip turn px:
/**
* dip to px pixel
* @param context
* @param px
* @return
* by Hankkin at:2015-10-07 21:16:43 *
* public
static int dip2px (context, float px) {
final float scale = getscreendensity (context);
return (int) (PX * scale + 0.5);
To get the status bar height:
/**
* Gets the height of the status bar
* @param context
* @return
* by Hankkin at:2015-10-07 21:16:43
/Public static int Getstatusheight (context context) {
int statusheight =-1;
try {
class<?> clazz = Class.forName ("Com.android.internal.r$dimen");
Object object = Clazz.newinstance ();
int height = integer.parseint (Clazz.getfield ("Status_bar_height")
. Get (object). toString ());
Statusheight = Context.getresources (). getdimensionpixelsize (height);
} catch (Exception e) {
e.printstacktrace ();
}
return statusheight;
}
Get screen Current screenshot:
/** * Get current screen capture, including status bar * @param activity * @return * by Hankkin at:2015-10-07 21:16:43/public static Bitmap Snapshotwith
StatusBar (activity activity) {View view = Activity.getwindow (). Getdecorview (); view.setdrawingcacheenabled (true);
View.builddrawingcache ();
Bitmap bmp = View.getdrawingcache ();
int width = getscreenwidth (activity);
int height = getscreenheight (activity);
Bitmap BP = null;
bp = bitmap.createbitmap (BMP, 0, 0, width, height);
View.destroydrawingcache ();
return BP; /** * Gets the current screen capture, does not contain status bar * @param activity * @return * by Hankkin at:2015-10-07 21:16:43/public static Bitmap Snapshotwi Thoutstatusbar (activity activity) {View view = Activity.getwindow (). Getdecorview (); View.setdrawingcacheenabled (True
);
View.builddrawingcache ();
Bitmap bmp = View.getdrawingcache ();
Rect frame = new Rect ();
Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
int statusbarheight = Frame.top;
int width = getscreenwidth (activity); int height = getscreenheight (activity);
Bitmap BP = null;
bp = bitmap.createbitmap (BMP, 0, statusbarheight, width, height-statusbarheight);
View.destroydrawingcache ();
return BP; }
The above is described in this article for the introduction of Android to obtain commonly used auxiliary methods (get screen height, width, density, notice bar height, screenshots), I hope for everyone is also help, more information to log on cloud community website to learn more information.